#!/bin/bash

# lab-grade-website

# This script is for grading the virtual web servers portion of rh254 lab X.

# It does the following:
# determines the hostname of the user's serverX and wwwX
# verifies that the sites at wwwX.example.com and serverX.example.com are different
# verifies that wwwX.example.com/private requires a password
# verifies that wwwX.example.com/private allows user:forrest pass:trees
# verifies that wwwX.example.com/cgi-bin/special.cgi exits and doesn't give an error

# Set environment and declare global variables
. /usr/local/lib/labtool.shlib
trap on_exit EXIT
LOG_FACILITY=local0
LOG_PRIORITY=info
LOG_TAG=website
DEBUG=false
ERROR_MESSAGE="Error running script. Contact your instructor if you continue to see this message."
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
PACKAGES=( elinks wget )

# Make sure we have the packages we need
check_packages

# Check to make sure we are running on the correct host
check_host "desktop"

IPA=$GLS_ENROLLMENT

# determines the hostname of the user's serverX and wwwX
if  links -dump http://www${IPA}.example.com &>/dev/null; then
    echo "http://www${IPA}.example.com: (exits) PASS"	    
else
    echo "http://www${IPA}.example.com: (exits) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi

if  links -dump http://server${IPA}.example.com &>/dev/null; then
    echo "http://server${IPA}.example.com: (exits) PASS"	    
else
    echo "http://server${IPA}.example.com: (exits) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi

# verifies that the sites at wwwX.example.com and serverX.example.com are different
if [ -f /tmp/www${IPA} ];then
   rm -f /tmp/www${IPA}
fi

if [ -f /tmp/server${IPA} ];then
   rm -f /tmp/server${IPA}
fi

links -dump http://www${IPA}.example.com > /tmp/www${IPA}
links -dump http://server${IPA}.example.com > /tmp/server${IPA}

if  diff /tmp/www${IPA} /tmp/server${IPA} &>/dev/null ; then
    echo "http://www${IPA}.example.com and http://server${IPA}.example.com: (content different) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit	    
else
    echo "http://www${IPA}.example.com and http://server${IPA}.example.com: (content different) PASS"

fi

# verifies that wwwX.example.com/private allows user:forrest pass:trees
if  wget --http-user=forrest --http-password=trees http://www${IPA}.example.com/private -O - &>/dev/null; then
    echo "http://www${IPA}.example.com/private: (auth) PASS"	    
else
    echo "http://www${IPA}.example.com/private: (auth) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi

# verifies that wwwX.example.com/cgi-bin/special.cgi exits and doesn't give an error

if  links -dump http://www${IPA}.example.com/cgi-bin/speical.cgi &>/dev/null; then
    echo "http://www${IPA}.example.com: (cgi-bin) PASS"	    
else
    echo "http://www${IPA}.example.com: (cgi-bin) FAIL"
    echo ""
    echo "Please check your configuration and try again."
    exit
fi
