#!/bin/bash -e
#
# Test if the web server (apache) works.

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

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

# Only networked profiles should have the https certificates
if echo "$PROFILE" | grep -Eq 'Main-Server|Workstation|Roaming-Workstation|LTSP-Server|Minimal' ; then
    :
else
    exit 0
fi

# Only Main-Server profile provide webserver
if echo "$PROFILE" | grep -q 'Main-Server' ; then
    :
else
    exit 0
fi

server=www

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

unset http_proxy || true
unset https_proxy || true
unset ftp_proxy  || true

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

if [ ! -x /usr/bin/HEAD ] ; then
	echo "error: $0: Unable to find /usr/bin/HEAD."
	exit 1
else
    url=https://$server/
    if HEAD $HEADOPTS $url 2>&1 | grep -q '200 OK' ; then
	echo "success: $0: Apache is listening on '$url'."
    else
	echo "error: $0: Apache is not listening on '$url'."
    fi

    for url in "https://$server/munin/" "https://$server/sitesummary/" \
	"https://$server/debian-edu-doc/" ; 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
fi
