#!/bin/bash

# This script was written for RH254, Unit 13 (Web Server Additional Config),
# Section 1 (name-based virtual hosts)
# run it on serverX after following the practice exercise
# it takes no args
# david.duffey@treeotech.com

STATION=$(hostname -s | sed s/^server//)

function FAIL () {
	echo "$*"
	echo Grading did not pass. Please fix your web server and try again!
	exit 1
}

function GOOD () {
	echo "Good, $*"
}

if [ server$STATION != $(hostname -s) ]
then
	FAIL Please run this from your serverX, not $STATION
fi

if [ "$(getenforce)" != "Enforcing" ]
then
	FAIL SELinux should be enabled
fi


(links -dump http://www$STATION/ | grep www | grep -v server &> /dev/null) && GOOD www$STATION works || FAIL www$STATION does not serve up the correct content
(links -dump http://www$STATION.example.com/ | grep www | grep -v server &> /dev/null) && GOOD www$STATION.example.com works || FAIL www$STATION.example does not serve up the correct content
(links -dump http://server$STATION/ | grep server | grep -v www &> /dev/null) && GOOD server$STATION works || FAIL server$STATION does not serve up the correct content
(links -dump http://server$STATION.example.com/ | grep server | grep -v www &> /dev/null) && GOOD server$STATION.example.com works || FAIL server$STATION.example does not serve up the correct content

echo Grading PASSED!
exit 0
