#! /bin/csh

set logins = `awk '{ print $1 }' classlist`
set emails = `awk '{ print $2 }' classlist`

set rpath = `pwd`
set testpath = "$rpath/solns/p3/test"

cd $testpath

set normaltests = `ls test?.txt test??.txt test_evil*.txt test_float*.txt`
set errortests = `ls test_error*.txt`

cd $rpath

touch p3_grades

set i = 1

foreach x ($logins)

  set gradefile = "$rpath/du/$x/P3/$x.hw3.gr"
  rm $gradefile
  touch $gradefile

  cd du/$x/P3

  echo "Grading $x"
  echo `pwd`

  make

  cd src

  set grade = 0

  foreach t ($normaltests)

    java -classpath . edu.du.cs.comp3351.p3.Interpreter < $testpath/$t > ../$t.out

    set test_passed = `diff $testpath/$t.out ../$t.out | wc -l`

    if ($test_passed == 0) then

      @ grade = $grade + 1

      echo "Test $t passed\!" >> $gradefile

    else
      echo "Test $t failed\!" >> $gradefile
    endif

  end

  foreach t ($errortests)

    java -classpath . edu.du.cs.comp3351.p3.Interpreter < $testpath/$t >& ../$t.out

    set line_col = `awk '{ print $1 }' ../$t.out | head -n 1`
    set line_col1 = `awk '{ print $1 }' $testpath/$t.out`

    if($line_col == $line_col1) then
      @ grade = $grade + 1

      echo "Test $t passed\!" >> $gradefile

    else
      echo "Test $t failed\!" >> $gradefile
    endif
  end

  cd ../../../..
  echo "$x $emails[$i] $grade" >> p3_grades
  @ i = $i + 1
end
