#!/bin/sh -e
#
# Test if the proxy server (squid) works.

if test -r /etc/skolelinux/config ; then
    mv /etc/skolelinux/config /etc/debian-edu/config
fi

if test -r /etc/debian-edu/config ; then
    . /etc/debian-edu/config
fi

# Only networked profiles use squid
if echo "$PROFILE" | egrep -q 'Main-Server|Workstation|Thin-Client-Server' ; then
    :
else
    exit 0
fi

server=webcache

# Test ownership of the squid cache directory
if test -d /var/spool/squid ; then 
    SQUIDOWNER=$(find /var/spool/squid -maxdepth 0 -printf %u)
    if [ "$SQUIDOWNER" = "proxy" ] ;  then 
        echo "success: $0: SquidOwner is $SQUIDOWNER"
    else
        echo "error: $0: SquidOwner is $SQUIDOWNER"
        RESULT=1
    fi
fi

# Test if HTTP proxy cache is reachable
if ping -c 3 $server > /dev/null 2>&1 ; then
    echo "success: $0: Host '$server' is pingable."
else
    echo "error: $0: Host '$server' is not pingable."
    RESULT=1
fi

# Wait for 10 seconds
HEADOPTS="-t 10"

if echo "$PROFILE" | egrep -q 'Main-Server' ; then
    if pidof squid > /dev/null ; then
        echo "success: $0: squid is running."
    else
        echo "error: $0: squid is not running."
        exit 1
    fi
fi

# Use the proxy
http_proxy=http://$server:3128/
ftp_proxy=$http_proxy
export http_proxy ftp_proxy

url=http://www.intern/

if /usr/bin/HEAD $HEADOPTS $url > /dev/null 2>&1 ; then
    echo "success: $0: Valid response from '$url' using proxy '$http_proxy'."
else
    echo "error: $0: Unable to connect to '$url' using proxy '$http_proxy'."
fi
