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

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

echo "student_id, part1, part2a, part2b, not following instructions, total, gradingcomment" >> gradelab11.csv

#find all student directories
for file in $(find . -type f -name "part1.c" )
#for each of those files
do
	note=""
        #extract out the student's name from the path
        path=${file%/*}
        studentname=${path##*/}

	gcc $path/part1.c -o part1
	return_value1=$?
	gcc $path/part2.c -o part2
	return_value2=$?	
	
	if [ $return_value1 = 0 ]
	then
		timeout 2s ./part1 6 x 8 > tc1out.txt

		P1=$(cat tc1out.txt | grep 48\.000\s* | wc -l)

		penalty=0

		if [ $P1 = "0" ]
		then
			cat tc1out.txt
			echo "Is there a 48 there?"
			read P1
			if [ $P1 = "1" ]
			then
				echo "Format looks okay? "
				read format
			
				if [ $format = "1" ]
				then
					penalty=0
				else
					penalty=-25
				fi
			fi
		fi
	else
		P1=0
		penalty=0
		note="part 1 did not compile"
	fi

	if [ $return_value2 = 0 ]
	then
		timeout 2s ./part2 6 x 8 + 1 > tc2aout.txt
		timeout 2s ./part2 1 + 2 + 3 + 4 + 5 - 4 - 3 - 2 - 1 x 5 > tc2bout.txt

		P2a=$(cat tc2aout.txt | grep 49\.000\s* | wc -l)
		P2b=$(cat tc2bout.txt | grep 25\.000\s* | wc -l)

		if [ $P2a = "0" ]
		then
			cat tc2aout.txt
			echo "Is there a 49 there?"
			read P2a
			if [ $penalty = "0" ]
			then
				if [ $P2a = "1" ]
					then
					echo "Format looks okay? "
					read format
			
					if [ $format = "1" ]
					then
						penalty=0
					else
						penalty=-25
					fi
				fi
			fi
		fi
		if [ $P2b = "0" ]
		then
			cat tc2bout.txt
			echo "Is there a 25 there?"
			read P2b

			if [ $P2b = "1" ]
			then
				echo "Format looks okay? "
				read format
		
				if [ $format = "1" ]
				then
					penalty=0
				else
					penalty=-25
				fi
			fi
		fi

	else
		P2a=0
		P2b=0
		penalty=0
		note=$note" part 2 did not compile"
	fi

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

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

        #write out the grading criteria and go to next line
        echo -n $(($P1*25))"," >> gradelab11.csv
        echo -n $(($P2a*25))"," >> gradelab11.csv
        echo -n $(($P2b*25))"," >> gradelab11.csv
	echo -n $(($penalty))"," >> gradelab11.csv
		
        finalgrade=$((($P1+$P2a+$P2b)*25+$penalty+25))

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

	echo -n "Name: "$studentname >>${studentname}.txt
        echo -n  " part 1: "$(($P1*25))" " >> ${studentname}.txt
        echo -n  " part 2 test 1: "$(($P2a*25))" " >> ${studentname}.txt
        echo -n  " part 2 test 2: "$(($P2b*25))" " >> ${studentname}.txt
	echo -n  " not following instructions penalty (wrong format string): "$(($penalty))" " >> ${studentname}.txt
        echo -n  " total: "$finalgrade" " >> ${studentname}.txt
	echo -n "note: "$note >> ${studentname}.txt

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

