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

#set up the output csv file to hold the grades
echo "student_id, prompt, tabular, decimal, octal, hex, width, height, total" >> gradeassign1.csv

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

        #print out user name
        echo "Grading for "+$studentname

        #compile the file
        gcc $file -lm -std=c99
        #run it, with redirected input for test case 1; redirect output
        ./a.out < tc1.txt > tc1_out.txt

        #check for the number of flaps
        nflaps=$(cat tc1_out.txt | sed "s/[^0-9.]*//g" | grep "2")
        if [ "$nflaps" != "" ]
        then
                nflaps1=1
        else
                nflaps1=0
        fi
        echo "Did the number flaps pass first test case? "$nflaps1
		
		#check for the time
        nseconds=$(cat tc1_out.txt | sed "s/[^0-9.]*//g" | grep "1.67")
	altnseconds=$(cat tc1_out.txt | sed "s/[^0-9.]*//g" | grep "1.33")
        if [ "$nseconds" != "" -o "$altnseconds" != "" ]
        then
                nseconds1=1
        else
                nseconds1=0
        fi
        echo "Did the number seconds pass the first test case? "$nseconds1
		
	cat tc1_out.txt
	echo "\n"
		
	echo -n "Nice user prompts?"
	read prompts
		
	echo -n "EC2 completed?"
	read ec2
		
        #run it, with redirected input for test case 1; redirect output
        ./a.out < tc2.txt > tc2_out.txt

        #check for the number of flaps
        nflaps=$(cat tc2_out.txt | sed "s/[^0-9.]*//g" | grep "3")
        if [ "$nflaps" != "" ]
        then
                nflaps2=1
        else
                nflaps2=0
        fi
        echo "Did the number flaps pass first test case? "$nflaps2
		
		#check for the time
        nseconds=$(cat tc2_out.txt | sed "s/[^0-9.]*//g" | grep "0.10")
		altnseconds=$(cat tc2_out.txt | sed "s/[^0-9.]*//g" | grep "0.11")
        if [ "$nseconds" != "" -o "$altnseconds" != "" ]
        then
                nseconds2=1
        else
                nseconds2=0
        fi
        echo "Did the number seconds pass the first test case? "$nseconds2		
		
        #print redirected output
        cat tc2_out.txt
		
		echo -n "Was EC1 completed? "
		read ec1
		
		echo -n "Was flap formatted correctly? "
		read flapformat
		
		echo -n "Was seconds formatted correctly? "
		read secondsformat

        #show the .c file so you can look over it
        cat $file
		
		echo -n "Algorithm? "
		read algorithm
		
		echo -n "Comments? "
		read comments
		
		echo -n "Indentations? "
		read indentations

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

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

        #write out the grading criteria and go to next line
        echo -n $(($algorithm*20))"," >> gradeassign1.csv
        echo -n $(($comments*10))"," >> gradeassign1.csv
        echo -n $(($indentations*5))"," >> gradeassign1.csv
        echo -n $(($prompts*15))"," >> gradeassign1.csv
        echo -n $(($nflaps1*12))"," >> gradeassign1.csv
	echo -n $(($nseconds1*12))"," >> gradeassign1.csv
        echo -n $(($nflaps2*13))"," >> gradeassign1.csv
	echo -n $(($nseconds2*13))"," >> gradeassign1.csv
	echo -n $(($flapformat * 10))",">>gradeassign1.csv
	echo -n $(($secondsformat * 10))",">>gradeassign1.csv
		
        finalgrade=$((($nflaps1+$nseconds1)*12+($nflaps2+$nseconds2)*13+$algorithm*20+($comments+$flapformat+$secondsformat)*10+$indentations*5+$prompts*15))

        echo -n $finalgrade",">> gradeassign1.csv
	echo -n $(($ec1*10))",">>gradeassign1.csv
	echo $(($ec2*10)) >> gradeassign1.csv

	echo "Name: "$studentname >>${studentname}.txt
	echo  "Algorithm: "$(($algorithm*20))"," >> ${studentname}.txt
        echo  "Comments: "$(($comments*10))"," >> ${studentname}.txt
        echo  "Indentations: " $(($indentations*5))"," >> ${studentname}.txt
        echo  "User prompts: "$(($prompts*15))"," >> ${studentname}.txt
        echo  "Flaps, test case 1: "$(($nflaps1*12))"," >> ${studentname}.txt
        echo  "Seconds, test case 1: "$(($nseconds1*12))"," >> ${studentname}.txt  
        echo  "Flaps, test case 2: "$(($nflaps2*13))"," >> ${studentname}.txt
        echo  "Seconds, test case 2: "$(($nseconds2*13))"," >> ${studentname}.txt
        echo  "Flaps formatting: "$(($flapformat * 10))",">>${studentname}.txt
        echo  "Seconds formatting: "$(($secondsformat * 10))",">>${studentname}.txt
	echo  "EC1: "$(($ec1*10))",">>${studentname}.txt
	echo  "EC2: "$(($ec2*10))",">>${studentname}.txt


        #let you know you're moving on to the next file
        echo "Done grading for "+$studentname+"."
        #wait for keyboard input
        read dummy
done

