# CPSC 1011 Lab 6 Grading Script
# Written by Lauren Dukes (lcairco@clemson.edu)

#set up the output csv file to hold the grades
rm -r -f gradelab6.csv
rm -r -f *.txt

echo "student_id, upper, lower, switch, total, gradingcomment" >> gradelab6.csv

#find all the files in the directory, recursively, that are named questions.txt
for file in $(find . -type f -name "lab6.c" )
#for each of those files
do
        #extract out the student's name from the path
        path=${file%/*}
        studentname=${path##*/}

	gcc $file

	./a.out < tests/tc1.txt > tc1out.txt

	./a.out < tests/tc2.txt > tc2out.txt

	#count the number of capital As
	up=$(cat tc2out.txt | sed 's/^.*:/:/' | grep A -o | wc -l)
	
	#if there are two, then uppercase works
	if [ "$up" = "2" ]
	then
		upper=1
	else
		upper=0
	fi

	#count the number of little as in second test case
	lo=$(cat tc1out.txt | sed 's/^.*:/:/' | grep a -o | wc -l)

	#if there are two, then uppercase works
	if [ "$lo" = "2" ]
	then
		lower=1
	else
		lower=0
	fi

	#check for swap
	#if there are two big As in first test case and two little as in second test case, 
	#swap worked
	tc1=$(cat tc1out.txt | sed 's/^.*:/:/' | grep A -o | wc -l)
	tc2=$(cat tc2out.txt | sed 's/^.*:/:/' | grep a -o | wc -l)
	sum=$(($tc1+$tc2))

	if [ "$sum" = "4" ]
	then
		switch=1
	else
		switch=0
		echo "sum was "$sum
	fi 	

        #write out the grading results for the file as a CSV

        #write out the student's name
        echo -n $studentname"," >> gradelab6.csv

        #write out the grading criteria and go to next line
        echo -n $(($upper*25))"," >> gradelab6.csv
        echo -n $(($lower*25))"," >> gradelab6.csv
        echo -n $(($switch*25))"," >> gradelab6.csv
		
        finalgrade=$((($upper+$lower+$switch+1)*25))

        echo -n $finalgrade",">> gradelab6.csv


	echo -n "Name: "$studentname >>${studentname}.txt
        echo -n  " upper: "$(($upper*25))" " >> ${studentname}.txt
        echo -n  " lower: "$(($lower*25))" " >> ${studentname}.txt
        echo -n  " switch: "$(($switch*25))" " >> ${studentname}.txt
	echo -n  " attendance optional due to snow: 25" >> ${studentname}.txt
        echo -n  " total: "$finalgrade" " >> ${studentname}.txt

	cat ${studentname}.txt >> gradelab6.csv
	echo "" >> gradelab6.csv
     
        #let you know you're moving on to the next file
        echo "Done grading for "+$studentname+"."
done

