#!/bin/sh -e
#
# Test if the CUPS server is working.

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

if [ -f /etc/cups/cupsd.conf ] ; then
    :
else
    echo "error: $0: /etc/cups/cupsd.conf is missing.  Is cupsys installed?"
    exit 1
fi

if pidof cupsd > /dev/null ; then
    echo "success: $0: cupsd is running."
else
    echo "error: $0: cupsd is not running."
    exit 1
fi

port=ipp
proto=tcp

if netstat -a --numeric-hosts 2>&1 | grep ":$port " | grep -q "^$proto" ; then
    echo "success: $0: cupsd server is listening on $port/$proto."
else
    echo "error: $0: cupsd server is not listening on $port/$proto."
    exit 1
fi

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

unset http_proxy || true
unset ftp_proxy  || true

servers=localhost

# Only the main server, CUPS should listen on the www interface too
if echo "$PROFILE" | egrep -q 'Main-Server' ; then
    servers="www $servers"
fi

for server in $servers ; do
    for url in "http://$server:631/" "https://$server:631/" ; do
	if HEAD $HEADOPTS $url 2>&1 | grep -q '200 OK' ; then
	    echo "success: $0: URL '$url' is working."
	else
	    echo "error: $0: URL '$url' is not working."
	fi
    done
done

exit 0
