#!/bin/bash
#
# Time-stamp: <Friday 16 February 2001 / submit>
#
# CS3 Individual Programming Project
#
# Submit student files.  Uses support programs accept and hassub.
#
# Ian Stark
# Samuel Lindley
#
##############################################################################

### Configuration

## Establish version of this program

version='$Revision: 1.6 $'
version=${version/\$Revision: /}
version=${version/ \$/}

# the directory to copy files from
SRC_DIR=$HOME/ipp2

# directory where support programs live
BIN_DIR=/home/cs3/ipp/bin

# check for existing submission 
HAS_SUBMITTED=$BIN_DIR/hassub

# accept command
ACCEPT=$BIN_DIR/accept

# subject of the confirmation email
SUBJECT="Files submitted (late) for ipp2"


###

### Functions

# ask the user whether to continue or not ($1) and act accordingly
continueQuery() {
echo -en "${1}? "

# anything begining with "y" or "Y" is treated as yes, anything else as no
read DO_SUBMIT

# convert to canonical form: "y" = yes, not "y" = no
DO_SUBMIT=`echo $DO_SUBMIT | grep "^[yY]" | sed 's/\(^[yY].*\)/y/g'`

if [ -z $DO_SUBMIT ]
then
	DO_SUBMIT="n"
fi

if [ ! $DO_SUBMIT = "y" ]
then
	echo "Submission cancelled"
        exit 1
fi
}
###

# Welcome message
echo "CS3 Individual Programming Project, Phase 2"
echo "Submission program, Version: ${version}"

# check that the user has set the correct permissions (i.e. to prevent
# other users reading their files)

# abort the whole process here, if this test fails

# $SRC_DIR must be a directory which is readable only by the user
LS_SRC_DIR=`ls -dl $SRC_DIR | grep "^dr.x------"`
if [ -z "$LS_SRC_DIR" ] 
then 
	echo "Error with $SRC_DIR"
        echo "Either the directory does not exist, or others can read it." 
        echo "(chmod 700 $SRC_DIR will set the correct permissions)" 
	exit 1
fi

# Bad choice of variable names (TEXTFILES and FILES)
# Clarification: 
#	TEXTFILES - newline delimited list of text files
#	FILES - space delimited list of text files

# get a list of all text files in $SRC_DIR
cd $SRC_DIR	# necessary in order to chop off directory prefixes
LS=`ls`
TEXTFILES=""
if [ "$LS" ]
then
	TEXTFILES=`file $LS | grep ^.*:.*text$ | sed 's/:.*//g'`
fi

# test to see if list of text files is non-empty - abort if it is
if [ -z "$TEXTFILES" ]
then
	echo "You have no text files to submit."
	echo "You must place your files in $SRC_DIR, and they must be ASCII text."
	exit 1
fi

# convert \n delimited list to " " delimited list (for tar)
FILES=""
for f in $TEXTFILES
do
	FILES="$FILES $f"
done

# confirm that the user wants to submit files
continueQuery "This is a late submission.\nDo you want to submit these files:\n\n$TEXTFILES\n"

# check for existing submission
if $HAS_SUBMITTED
then
	continueQuery "Overwrite your existing submission"
fi

# the email message string
MESSAGE="The following files have been submitted:\n\n${TEXTFILES}\n\nThis is a late submission"

# submit files
if tar -cf - $FILES | $ACCEPT
then
	echo -e "$MESSAGE" | mail -s "$SUBJECT" `whoami`
	echo
	echo "Done."
        exit 0
else
	echo "Submission failed, reason unknown."
        exit 1
fi

# End of file
