#!/bin/csh -f

set i=1
set max_test_nr=32
set total=0
set max_total=0
set grade_file=grade.txt
set score=(1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 2 2 5 5 2 2 5 5 2 2 5 5 10 5)
set runtime=(78772 77316 69876)

if (-e ${grade_file}) then
    rm -f ${grade_file}
endif
echo "" > ${grade_file}
echo "**************" >> ${grade_file}
echo " Score for P3" >> ${grade_file}
echo "**************" >> ${grade_file}
echo "" >> ${grade_file}
echo "" >> ${grade_file}

#cp -f /afs/cs.wisc.edu/p/course/cs537-remzi/ta/p2/myprog .
setenv LD_LIBRARY_PATH .

while (${i} <= `expr ${max_test_nr} - 3` )
  
    ./test.csh $i
    if ($status != 0) then
       echo "Test ${i}: 0 / $score[$i]" >> ${grade_file}
    else
       echo "Test ${i}: $score[$i] / $score[$i]" >> ${grade_file}
       set total=`expr ${total} + $score[$i]`
    endif
    set max_total=`expr ${max_total} + $score[$i]`
    set i=`expr ${i} + 1`
end


while (${i} <= ${max_test_nr} )
    set indl_max_score=`echo "scale=2; 25.0/3.0" | bc` 
    ./test.csh $i
    if ($status != 0) then
       echo "Test ${i}: 0 / $indl_max_score" >> ${grade_file}
    else
       set obsvd_runtime=`cat out | cut -d ">" -f2  | cut -d " " -f2 | cut -d "." -f1`
       set index=`expr $i - 29`
       set indl_runtime=`expr $runtime[$index] \* 3 / 2`
       set max_runtime=`expr ${indl_runtime} \* 3`
       if ( $obsvd_runtime <= $indl_runtime ) then
           echo "Test ${i}: $indl_max_score / $indl_max_score" >> ${grade_file}
           set total=`echo "$total + $indl_max_score" | bc`
       else
           if ( $obsvd_runtime >= $max_runtime) then
                echo "Test ${i}: 0 / $indl_max_score" >> ${grade_file}
           else
                set test_score=`echo "scale=2; $indl_max_score * ($max_runtime - $obsvd_runtime) / ($max_runtime - $indl_runtime )" | bc`
                echo "Test ${i}: $test_score / $indl_max_score" >> ${grade_file}
                set total=`echo "$total + $test_score" | bc`
           endif
      endif
    endif

    rm -f out
    set max_total=`echo "${max_total} + $indl_max_score" | bc`
    set i=`expr ${i} + 1`
end
#rm -f myprog

set max_total=`echo "scale=20; a=${max_total}; scale=0; b=a/1; if (a<b+0.5) b else b+1" | bc -l;`
set total=`echo "scale=20; a=${total}; scale=0; b=a/1; if (a<b+0.5) b else b+1" | bc -l;`

echo "" >> ${grade_file}
echo "" >> ${grade_file}
echo "------------------" >> ${grade_file}
echo "Total  : ${total} / ${max_total} " >> ${grade_file}
echo "------------------" >> ${grade_file}
