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

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

echo "student_id, ukraine, russia, ireland, uk_height, ru_height, ir_height, total, gradingcomment" >> gradelab7.csv

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

	gcc $path/*.c

	./a.out < tests/tc1.txt > ukraine.ppm

	./a.out < tests/tc2.txt > russia.ppm

	./a.out < tests/tc3.txt > ireland.ppm

	display ukraine.ppm
	echo "Is this the ukranian flag (blue top, yellow bottom)? "
	read uk

	h1=$((identify ukraine.ppm | grep 300x200 | wc -l))
	
	if [ "$h1" -gte "1" ]
	then
		ukheight=1
	else
		ukheight=0
	fi

	display russia.ppm
	echo "Is this the russian flag (blue top, red bottom)? "
	read ru

	h2=$((identify russia.ppm | grep 300x200 | wc -l))

	if [ "$h2" -gte "1" ]
	then
		ruheight=1
	else
		ruheight=0
	fi
	
	display ireland.ppm
	echo "Is this the irish flag (green, white, red vertical stripe)? "
	read ir

	h3=$((identify ireland.ppm | grep 200x100 | wc -l))

	if [ "$h3" -gte "1" ]
	then
		irheight=1
	else
		irheight=0
	fi

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

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

        #write out the grading criteria and go to next line
        echo -n $(($uk*20))"," >> gradelab7.csv
        echo -n $(($ru*20))"," >> gradelab7.csv
        echo -n $(($ir*20))"," >> gradelab7.csv
        echo -n $(($ukheight*5))"," >> gradelab7.csv
        echo -n $(($ruheight*5))"," >> gradelab7.csv
        echo -n $(($irheight*5))"," >> gradelab7.csv
		
        finalgrade=$((($uk+$ru+$ir)*20+($ukheight+$irheight+$ruheight)*5+25))

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


	echo -n "Name: "$studentname >>${studentname}.txt
        echo -n  " ukraine: "$(($uk*20))" " >> ${studentname}.txt
        echo -n  " ukraineheight: "$(($ukheight*5))" " >> ${studentname}.txt
        echo -n  " russia: "$(($ru*20))" " >> ${studentname}.txt
	echo -n  " russiaheight: "$(($ruheight*5))" " >> ${studentname}.txt
	echo -n  " ireland: "$(($ir*20))" " >> ${studentname}.txt
	echo -n  " irelandheight: "$(($irheight*5))" " >> ${studentname}.txt
        echo -n  " total: "$finalgrade" " >> ${studentname}.txt

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

