#!/bin/sh

###Script to compile and run the assignments

PATH_TO_TEMPLATE=/home/mark2660/marking

echo "Enter template filename : \c"
read TEMPLATE_NAME

echo "Taking assignment directory as $PWD"
ASSIGN_DIR=$PWD


if [ -f $PATH_TO_TEMPLATE/$TEMPLATE_NAME ] ; then
	echo " Setting Path --> $PATH_TO_TEMPLATE"
	echo " Setting Path --> $TEMPLATE_NAME"

else 
	echo " Echo template file doesn't exist ..."
	exit
fi

sleep 2

if [ -f $ASSIGN_DIR/success.log ] ;then
    
    /usr/bin/rm success.log 

else

    /usr/bin/rm failure.log

fi


for i in `ls`
do

 if [ -d $i ] ; then  

  cd $i
  echo
  
  if [ -f $i/marks ]; then
      echo " Deleting old marks file in directory `basename $i` " >> ../success.log
      /usr/bin/rm marks
  
  echo
  echo " Clean-Up finished .... Proceeding ... "
  echo

  fi


  echo " Putting a marks file into `basename $i`'s directory"
  cp $PATH_TO_TEMPLATE/$TEMPLATE_NAME marks
  echo " Success in inserting mark file for $i" >> ../success.log

  if [ -f $i/marks ] ; then 
    echo " Insert of mark file in $i failed " >> ../failure.log
  fi

 cd ..
 fi

done


 echo
 echo "\t Summary:  "
 echo "\t =======   "
 echo
 echo "\t 1) Success Log: "
 echo

 if [ -f $ASSIGN_DIR/success.log ] ; then
    more  $ASSIGN_DIR/success.log
    sleep 2

 elif [ -f $ASSIGN_DIR/failure.log ] ;then
    echo "\t 2) Failure Log: "
    more $ASSIGN_DIR/failure.log
    sleep 2
 
 else
    echo "Nothing to display ... "

 fi
