#!/bin/sh

# This script takes the lab number as a command line argument
# and outpout to stdout all the folders with submissions
# also finds the total submissions and non-submissions   
# it also records times 

lab=$1
basedir=/afs/andrew/course/15/123/handin/
baseinstrmail="guna@andrew.cmu.edu"
subj="15123S09 SL1"
echo "0" > "notsub.txt"

message="you have not submitted Skills lab1.\nPlease respond to this message if this is an error or if you'd still like to\nsubmit the lab\n\nMessage generated by auto grader"

if [ -f "notsubmitted.txt" ]
then
  rm "notsubmitted.txt"
fi
cat "all.txt" |
while read id
do
  userdir=$basedir$lab"/"$id/$2
  files=`ls $userdir | wc -l`
  if [ $files -gt 0 ]
   then

     #fs sa $userdir $id all

     countsubmitted=`expr $countsubmitted + 1`
     echo $countsubmitted > sub.txt 
     echo $userdir `ls $userdir | wc -l`
     basemail=$id"@andrew.cmu.edu"
     #echo $message | mailx -s "$subj" "$baseinstrmail" < "grades.txt"


   else
     echo $id >> "notsubmitted.txt"
     countNotsubmitted=`expr $countNotsubmitted + 1`
     echo $countNotsubmitted > notsub.txt 
  fi
done   

cntsub=`cat sub.txt`
cntnotsub=`cat notsub.txt`

day=`date | cut -d" " -f1,4`

printf "%s " $lab >> log.txt 
printf "%s " $day >> log.txt 
printf "%s " $cntsub >> log.txt 
printf "%s \n" $cntnotsub >> log.txt


printf "The number submitted is %s \n" $cntsub
printf "number not submitted is %s \n" $cntnotsub

if [ -f "sub.txt" ]
then
  rm "sub.txt"
fi
if [ -f "notsub.txt" ]
then
  rm "notsub.txt"
fi
#echo $message | mailx -s "$subj" "$baseinstrmail" 

#ls $basedir | wc -l   

#echo $basedir