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

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

echo "student_id, lengths1, lengths2, concat, not following instructions, total, gradingcomment" >> gradelab10.csv

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

	gcc $path/*.c
	return_value=$?

	if [ $return_value = 0 ]
	then
		timeout 2s ./a.out < tests/tc1.txt > tc1out.txt

		L1=$(cat tc1out.txt | grep "len:   3" | wc -l)
		L2=$(cat tc1out.txt | grep "len:   4" | wc -l)
		C1=$(cat tc1out.txt | grep "catfish" | wc -l)
		penalty=0

		if [ $L1 = "0" ]
		then
			cat tc1out.txt
			echo "Does the length for the first string look ok? "
			read L1
			penalty=-25
		fi
		if [ $L2 = "0" ]
		then
			cat tc1out.txt
			echo "Does the length for the second string look ok? "
			read L2
		fi
		if [ $C1 = "0" ]
		then
			cat tc1out.txt  
			echo "Does the concatenated string look ok? "
			read C1
		fi
	else
		L1=0
		L2=0
		C1=0
		penalty=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"," >> gradelab10.csv

        #write out the grading criteria and go to next line
        echo -n $(($L1*25))"," >> gradelab10.csv
        echo -n $(($L2*25))"," >> gradelab10.csv
        echo -n $(($C1*25))"," >> gradelab10.csv
	echo -n $(($penalty))"," >> gradelab10.csv
		
        finalgrade=$((($L1+$L2+$C1)*25+$penalty+25))

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

	rm ${studentname}.txt
	echo -n "Name: "$studentname >>${studentname}.txt
        echo -n  " length string 1: "$(($L1*25))" " >> ${studentname}.txt
        echo -n  " length string 2: "$(($L2*25))" " >> ${studentname}.txt
        echo -n  " concatenation: "$(($C1*25))" " >> ${studentname}.txt
	echo -n  " not following instructions penalty: "$(($penalty))" " >> ${studentname}.txt
        echo -n  " total: "$finalgrade" " >> ${studentname}.txt
	echo -n "note: "$note >> ${studentname}.txt

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

