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

server=webcache

# 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 pidof squid > /dev/null ; then
    echo "success: $0: squid is running."
else
    echo "error: $0: squid is not running."
    exit 1
fi

if [ ! -x /usr/bin/HEAD ] ; then
	echo "error: $0: Unable to find /usr/bin/HEAD."
	exit 1
else
    url=http://$server:3128/
    if HEAD $HEADOPTS $url 2>&1 | grep -q Squid ; then
	echo "success: $0: Squid proxy is listening on '$url'."
    else
	echo "error: $0: Squid proxy is not listening on '$url'."
    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
