#!/bin/sh

webCall() {
    curl -w "%{onerror}%{stderr}\nStatus: %{http_code}\n" \
        -s \
        --fail-with-body \
        http://localhost/phpwebcounter/phpwebcounter.php
}

WEB_CONTENTS="<SPAN><IMG src='/phpwebcounter/images/basic1/0.png' alt='0' /><IMG src='/phpwebcounter/images/basic1/0.png' alt='0' /><IMG src='/phpwebcounter/images/basic1/0.png' alt='0' /><IMG src='/phpwebcounter/images/basic1/0.png' alt='0' /><IMG src='/phpwebcounter/images/basic1/0.png' alt='0' /><IMG src='/phpwebcounter/images/basic1/%s.png' alt='%s' /></SPAN><br><a href=\"http://phpwebcounter.sourceforge.net\"><font size=1px>PHP Web Counter</a></font size><br>"

test_increment() {
    assertFalse 'No hit file should exist' "[ -f /var/lib/phpwebcounter/hits_webcounter.0 ]"
    assertEquals 'The HTML contents should be the same' "$(printf "$WEB_CONTENTS" "0" "0")" "$(webCall)"
    assertTrue 'The hit file should exist' "[ -f /var/lib/phpwebcounter/hits_webcounter.0 ]"
    assertEquals 'The hit file should be at 1' '1' "$(cat /var/lib/phpwebcounter/hits_webcounter.0)"
    assertEquals 'The HTML contents should be the same' "$(printf "$WEB_CONTENTS" "1" "1")" "$(webCall)"
    assertEquals 'The hit file should be at 2' '2' "$(cat /var/lib/phpwebcounter/hits_webcounter.0)"
    assertEquals 'The HTML contents should be the same' "$(printf "$WEB_CONTENTS" "2" "2")" "$(webCall)"
    assertEquals 'The hit file should be at 3' '3' "$(cat /var/lib/phpwebcounter/hits_webcounter.0)"
}

oneTimeSetUp() {
    # Stop the service
    sudo service apache2 stop
    # Copy the example
    sudo cp /usr/share/doc/phpwebcounter/examples/phpwebcounter.apache2 /etc/apache2/conf-available/phpwebcounter.conf
    # Check configs
    sudo apache2ctl -t 2>&1
    # Enable the service
    sudo a2enconf phpwebcounter
    # Check configs
    sudo apache2ctl -t 2>&1
    # Start the service
    sudo service apache2 start
}

oneTimeTearDown() {
    # Stop the service
    sudo service apache2 stop
}

# Load and run shUnit2.
. shunit2
