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

. /usr/share/debian-edu-config/testsuite-lib.sh

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

netstat_check ipp tcp "cupsd" || exit 1

# * Ignore SSL certificate checking as the name do not match the server
#   name
# * Wait for 10 seconds
WGETOPTS="--no-check-certificate --timeout=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 wget -O - $WGETOPTS $url > /dev/null 2>&1 ; then
	    echo "success: $0: URL '$url' is working."
	else
	    echo "error: $0: URL '$url' is not working."
	fi
    done
done

exit 0
