#!/bin/bash
# Perform the following tests:
# - dhcpd - done by connect script
# - dns: ping schoolserver, ping 
# - httpd
# - idmgr
# - squid
# - dansguardian
# - moodle

# Define colors for results
NORMAL=$(tput sgr0)
GREEN=$(tput setaf 2; tput bold)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)

function red() {
    echo -e "$RED$*$NORMAL"
}

function green() {
    echo -e "$GREEN$*$NORMAL"
}

function yellow() {
    echo -e "$YELLOW$*$NORMAL"
}

# dhcpd - verify
function ip_range() {
echo -n "[XSCE] Test got address from server ..."
if `ifconfig | grep -q 172.18`
  then
      green OK
  else
      red FAILED!
fi
}

# - dns: ping schoolserver, translate.google.com
function test_dns() {
echo -n "[XSCE] Test resolve schoolserver ..."
if `ping -c 1 schoolserver | grep unknown`
  then
      red FAILED!
  else
      green OK
fi
echo -n "[XSCE] Test resolve translate.google.com ..."
if `ping -c 1 translate.google.com | grep unknown`
  then
      red FAILED!
  else
      green OK
fi
}

# - httpd: access home page
function test_httpd() {
echo -n "[XSCE] Test schoolserver http access..."
if `curl -Is http://schoolserver.lan | grep -is "Location: http://schoolserver.lan/xs-portal" > /dev/null`
then
    green OK
    else
    red FAILED!
fi
}


# - idmgr

function test_idmgr() {
echo -n "[XSCE] Test idmgr registration..."
su - olpc -c "python /home/olpc/testing/idmgr-test.py >/home/olpc/testing/idmgr-test.result"
rc=`cat /home/olpc/testing/idmgr-test.result`

if [ "$rc" == "OK" ]
then
    green OK
    else
    red FAILED!
fi
}

function test_registration() {
echo -n "[XSCE] Test if xo is registered ..."
if `wget -qO - http://schoolserver.lan:5000/  | grep -is "SHF00000000" > /dev/null`
then
    green OK
    else
    red FAILED!
fi

}

# squid
function test_squid_cache() {
echo -n "[XSCE] Test squid proxy settings..."
if `curl -Is http://one.laptop.org//sites/default/files/charlotte2.jpg | grep X-Cache | grep -q schoolserver`
then
    green OK
    else
    red FAILED!
fi

echo -n "[XSCE] Test squid proxy caching settings..."
if `curl -Is http://one.laptop.org//sites/default/files/charlotte2.jpg | grep X-Cache | grep schoolserver | grep -q HIT`
then
    green OK
    else
    red FAILED!
fi
}

# - dansguardian
function test_dansguardian() {
echo -n "[XSCE] Test dansguardian settings..."
if `wget -qO - http://en.wikipedia.org/wiki/Pornography | grep -is dansguardian > /dev/null`
then
    green OK
    else
    red FAILED!
fi
}

# - moodle
function test_moodle() {
echo -n "[XSCE] Test schoolserver moodle access..."
if `curl -Is http://schoolserver.lan/moodle/ | grep -is "moodle/login" > /dev/null`
then
    green OK
    else
    red FAILED!
fi
}

# - ejabberd
function test_ejabberd() {
echo -n "[XSCE] Test ejabberd collaboration..."

if `curl -Is http://schoolserver.lan:5280/admin | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi
}

# ajenti

function test_ajenti() {
echo -n "[XSCE] Test ajenti access..."
if `curl -Is http://schoolserver.lan:5000 | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi
}

function iiab_presence() {
echo -n "[IIAB] Test main iiab page..."
if `curl -Is http://schoolserver.lan/iiab/ | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi

echo -n "[IIAB] Test wikipedia page..."
if `curl -Is http://schoolserver.lan/iiab/zim/wikipedia_gn_all_01_2013/A/Pirane.html | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi

echo -n "[IIAB] Test khan akademy video..."
if `curl -Is http://schoolserver/iiab/video/khanvideo/1/1/2/3.webm | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi

echo -n "[IIAB] Test map link..."
if `curl -Is http://schoolserver/iiab/maps/tile/6/31/29.png | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi

echo -n "[IIAB] Test book search..."
if `curl -Is http://schoolserver/iiab/books/search?q=moby+dick | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi

echo -n "[IIAB] Test book download..."
if `curl -Is http://schoolserver/iiab/books/epub/2701.epub | grep -is "HTTP/1.1 200 OK" > /dev/null`
then
    green OK
    else
    red FAILED!
fi

}

# Main

echo 
echo "Starting Tests"
ip_range
test_dns
test_httpd
test_idmgr
test_registration
test_squid_cache
test_dansguardian
test_moodle
test_ajenti

echo 
echo "IIAB Tests"
#iiab_presence
