#!/bin/bash

# Check that we are root (by hand)
if [[ "$EUID" -gt "0" ]] ; then
    echo 'This script must be run as root!'
    exit 1
fi

# Need to download labtool.shlib
wget -q -O /usr/local/lib/labtool.shlib http://instructor.example.com/pub/gls/ullib/labtool.shlib || exit 1

. /usr/local/lib/labtool.shlib
trap on_exit EXIT

# uncomment to enable debug mode
#DEBUG=true

# Did they set the hostname correctly?
check_host "desktop"

# Are we using dhclient?
echo
echo -n "Testing that we are using DHCP... "
ps -ef | grep 'dhclient.*eth0' | grep -v grep &>/dev/null && echo "PASSED." || exit 1

# Is the root password redhat?
# FIXME: Test password

# Check partitions:
# /boot physical, 200M
fsmount="/boot"
echo
echo "Checking for mounted filesystem ${fsmount}... "
df -h ${fsmount} &>/dev/null && echo "PASSED." || exit 1
fspart=$(grep "${fsmount} " /etc/mtab | cut -d' ' -f1)
# FIXME: Is it physical?
echo
echo "Checking size of filesystem at ${fsmount}... "
fssize=$(df -T ${fsmount} | tr -s ' ' | tail -n 1 | cut -d' ' -f3)
[ ${fssize} -gt 180000 -a ${fssize} -lt 220000 ] && echo "PASSED." || exit 1

# /home physical, 1024M, encrypted
fsmount="/home"
echo
echo "Checking for mounted filesystem ${fsmount}... "
df -h ${fsmount} &>/dev/null && echo "PASSED." || exit 1
fspart=$(grep "${fsmount} " /etc/mtab | cut -d' ' -f1)
# FIXME: Is it physical?
echo
echo "Checking for encryption of ${fsmount}... "
cryptsetup status ${fspart} &>/dev/null && echo "PASSED." || exit 1
echo
echo "Checking size of filesystem at ${fsmount}... "
fssize=$(df -T ${fsmount} | tr -s ' ' | tail -n 1 | cut -d' ' -f3)
[ ${fssize} -gt 950000 -a ${fssize} -lt 1100000 ] && echo "PASSED." || exit 1

# physical volume, 50G
# FIXME: Check for physical volume

# /, logical volume, 20G
fsmount="/"
echo
echo "Checking for mounted filesystem ${fsmount}... "
df -h ${fsmount} &>/dev/null && echo "PASSED." || exit 1
fspart=$(grep "${fsmount} " /etc/mtab | cut -d' ' -f1)
# FIXME: Is it logical?
echo
echo "Checking size of filesystem at ${fsmount}... "
fssize=$(df -T ${fsmount} | tr -s ' ' | tail -n 1 | cut -d' ' -f3)
[ ${fssize} -gt 19950000 -a ${fssize} -lt 21000000 ] && echo "PASSED." || exit 1

# swap, logical volume, 2G
echo
echo "Checking for one swap space... "
[ $(tail -n +2 /proc/swaps | wc -l) -eq 1 ] && echo "PASSED." || exit 1
echo
echo "Checking for size of swap space... "
swapsize=$(tail -n +2 /proc/swaps | cut -f2)
[ ${swapsize} -gt 1950000 -a ${swapsize} -lt 2100000 ] && echo "PASSED." || exit 1

# Is vsftpd installed
echo
echo -n "Testing that vsftpd is installed... "
rpm -q vsftpd &>/dev/null && echo "PASSED." || exit 1

# Is there a user student, password student
echo
echo -n "Testing that user student was created... "
id student &>/dev/null && echo "PASSED." || exit 1
# FIXME: Test password

exit 0
