#!/bin/bash
#
# by : Bowe Strickland (20101130)
# based on : Robert Locke (20101130)
# in turn based on: Brian Butler
#
# Grade the NFS lab
# this script is to be installed and run on serverX.example.com
# output is logged to syslog
#set -x

# Set environment and declare global variables
. /usr/local/lib/labtool.shlib
trap on_exit EXIT
LOG_FACILITY=local0
LOG_PRIORITY=info
LOG_TAG=ProPlay
DEBUG=false
ERROR_MESSAGE="FAILED."
PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
userInput="no"
set -o pipefail

# Check to make sure we are running as root
check_root

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

RODIR=/sales/archives
RWDIR=/sales/current
ROSRC=/share/archives
RWSRC=/share/current

# check that ro directory exists
echo 
echo "checking that $RODIR exists..."
[ -d $RODIR ] && echo "PASSED." || exit 1

# check that rw directory exists
echo 
echo "checking that $RWDIR exists..."
[ -d $RWDIR ] && echo "PASSED." || exit 1

# is ro nfs?
echo 
echo "checking that $RODIR is nfs..."
awk '$3 ~ /nfs/ {print $2}' /proc/mounts  | grep -q $RODIR && echo "PASSED." || exit 1

# is rw nfs?
echo 
echo "checking that $RWDIR is nfs..."
awk '$3 ~ /nfs/ {print $2}' /proc/mounts  | grep -q $RWDIR && echo "PASSED." || exit 1

# is ro /sales/archve?
echo 
echo "checking that $RODIR is mounting $ROSRC..."
awk '$3 ~ /nfs/' /proc/mounts  | grep $RODIR | grep -q $ROSRC && echo "PASSED." || exit 1

# is rw /sales/current?
echo 
echo "checking that $RWDIR is mounting $RWSRC..."
awk '$3 ~ /nfs/' /proc/mounts  | grep $RWDIR | grep -q $RWSRC && echo "PASSED." || exit 1

# is rw writable?
echo 
echo "checking that $RWDIR is writable..."
F=$RWDIR/dasflad.$$
(touch $F &>/dev/null && rm $F) && echo "PASSED." || exit 1

# is ro not writable?
echo 
echo "checking that $RODIR is not writable..."
F=$RODIR/dasfladw.$$
(touch $F &>/dev/null && rm $F) && exit 1 || echo "PASSED." 

exit 0
