#!/bin/bash
#
# by: Robert Locke (20101130)
# based on: Brian Butler
#
# Grade the second file management lab
# this script is to be installed and run on serverX.example.com
# output is logged to syslog

# Set environment and declare global variables
. /usr/local/lib/labtool.shlib
trap on_exit EXIT
LOG_FACILITY=local0
LOG_PRIORITY=info
LOG_TAG=fm2Grade
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"

# Check existence of folder and its contents
newfolder=bowe-labs
oldfolder=chemistry
oldparent=/home/bowe
parentdir=/home/student
echo
echo -n "Checking for existence of ${newfolder}... "
[ -d ${parentdir}/${newfolder} ] && echo "PASSED." || exit 1
echo
echo -n "Checking contents of ${newfolder}... "
success=1
for i in ${oldparent}/${oldfolder}/* ; do
  [ -f ${parentdir}/${newfolder}/$(basename ${i}) ] || success=0
done
[ ${success} -eq 1 ] && echo "PASSED." || exit 1

# Check music collection changes
otheruser=mark
file1=call-me.mp3
file2=roxanne.mp3
oldfolder=mozart
newfolder1=blondie
newfolder2=the-police
removed=playlist.txt
echo
echo -n "Checking ${file1} has been moved... "
[ -f /home/${otheruser}/mp3/${oldfolder}/${file1} ] && exit 1
[ -f /home/${otheruser}/mp3/${newfolder1}/${file1} ] && echo "PASSED." || exit 1
echo
echo -n "Checking ${file2} has been moved... "
[ -f /home/${otheruser}/mp3/${oldfolder}/${file2} ] && exit 1
[ -f /home/${otheruser}/mp3/${newfolder2}/${file2} ] && echo "PASSED." || exit 1
echo
echo -n "Checking removal of $(basename ${removed})... "
[ -f /home/${otheruser}/mp3/${removed} ] && exit 1 || echo "PASSED."

exit 0
