# 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" >> gradelab3.csv

#find all the files in the directory, recursively, that are named lab3.c
for file in $(find . -type f -name "lab3.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

	width=$(sed 's/[[:alnum:]]*:* *//g' < tc1_out.txt | awk '{ if (length($0) > max) max = length($0) } END { print max }')
	echo "Width of rectangle: "$width
	height=$(sed 's/[[:alnum:]]*:* *//g' < tc1_out.txt | sed '/^$/d' | wc -l)
	echo "Height of rectangle: "$height
	
	if [ \( $width -eq 9 -a $height -eq 12 \) -o \( $width -eq 12  -a $height -eq 9 \) ]
	then
		widthcomp=1
		heightcomp=1
	else
		if [ $width -eq 9 -o $width -eq 12 ]
		then
			widthcomp=1
		else
			widthcomp=0
		fi
		if [ $height -eq 12 -o $height -eq 9 ]
		then
			heightcomp=1
		else
			heightcomp=0
		fi
	fi
	echo "Width passed? "$widthcomp
	echo "Height passed? "$heightcomp

	#check for decimal, octal, and hex outputs
	dec=$(cat tc1_out.txt | sed "s/[^A-F0-9]*//g" | sed "s/[E]*//g" | tr '\n' ' ' | grep "108")
	if [ "$dec" != "" ]
	then
		decimal=1
	else
		decimal=0
	fi
	echo "Was the decimal output 108? "$decimal

	dec=$(cat tc1_out.txt | sed "s/\s[^A-Fc0-9]\*\s//g" | sed "s/[Ea-z]\*//g" | tr '\n' ' ' | grep "6C")
	dec2=$(cat tc1_out.txt | sed "s/\s[^A-Fc0-9]\*\s//g" | sed "s/[Ea-z]\*//g" | tr '\n' ' ' | grep "6c")
	if [ "$dec" != "" -o "$dec2" != "" ]
	then
		hex=1
	else
		hex=0
	fi
	echo "Was the hex output 6C? "$hex

	dec=$(cat tc1_out.txt | sed "s/[^A-F0-9]*//g" | sed "s/[E]*//g" | tr '\n' ' ' | grep "154")
	if [ "$dec" != "" ]
	then
		octal=1
	else
		octal=0
	fi
	echo "Was the octal output 154? "$octal

	#print redirected output
	cat tc1_out.txt
        
	echo -n "Was there a nice user prompt to enter two integer values? (1 for yes, 0 for no, decimals for partial credit)"
	read prompt

	echo -n "Were the numerical outputs tabular?"
	read tabular

	#show the .c file so you can look over it
        cat $file

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

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

	#write out the grading criteria and go to next line
	echo -n $(($prompt*5))"," >> gradelab3.csv
	echo -n $(($tabular*5))"," >> gradelab3.csv
	echo -n $(($decimal*5))"," >> gradelab3.csv
	echo -n $(($octal*5))"," >> gradelab3.csv
	echo -n $(($hex*5))"," >> gradelab3.csv
	echo -n $(($widthcomp*25))"," >> gradelab3.csv
	echo -n $(($heightcomp*25))"," >> gradelab3.csv

	finalgrade=$((($prompt+$decimal+$octal+$hex+$tabular)*5+25*($widthcomp+$heightcomp)))

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

