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

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

echo "student_id, innerproduct1, innerproduct2, reverse1, reverse2, noprint, total, gradingcomment" >> gradelab8.csv

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

	gcc main.c $path/*.c
	return_value=$?

	#if it compiles
	if [ $return_value = 0 ]
	then
		#run the two test cases
		timeout 1 ./a.out < lab8tests/tc1.txt > tc1out.txt
		timeout 1 ./a.out < lab8tests/tc2.txt > tc2out.txt

		#check the two inner products and reverses
		ip1=$(cat tc1out.txt | grep 70 | wc -l)
		ip2=$(cat tc2out.txt | grep 217 | wc -l)
		rv1=$(cat tc1out.txt | grep "4 3 2 1" | wc -l)
		rv2=$(cat tc2out.txt | grep "6 5 4 3 2 1" | wc -l)
		
		#if they printed out anything besides what we specified in the main file
		#the line count will not be 2
		lc=$(cat tc1out.txt | wc -l)
		if [ $lc -eq "2" ]
		then
			noprint=1
		else
			noprint=0
		fi 
		note=""
	#no credit for a non-compiling file
	else
		rv1=0
		rv2=0
		ip1=0
		ip2=0
		noprint=0
		note="did not compile"
	fi

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

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

        #write out the grading criteria and go to next line
        echo -n $(($ip1*17))"," >> gradelab8.csv
        echo -n $(($ip2*17))"," >> gradelab8.csv
        echo -n $(($rv1*17))"," >> gradelab8.csv
        echo -n $(($rv2*17))"," >> gradelab8.csv
        echo -n $(($noprint*7))"," >> gradelab8.csv
		
        finalgrade=$((($ip1+$ip2+$rv1+$rv2)*17+($noprint)*7+25))

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

	echo -n "Name: "$studentname >>${studentname}.txt
        echo -n  " inner product 1: "$(($ip1*17))" " >> ${studentname}.txt
        echo -n  " inner product 2: "$(($ip2*17))" " >> ${studentname}.txt
        echo -n  " reverse 1: "$(($rv1*17))" " >> ${studentname}.txt
	echo -n  " reverse 2: "$(($rv2*17))" " >> ${studentname}.txt
	echo -n  " no print in functions: "$(($noprint*7))" " >> ${studentname}.txt
        echo -n  " total: "$finalgrade" " >> ${studentname}.txt
	echo -n "note: "$note >> ${studentname}.txt

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

