#
#  Run some anldp jobs and compare the
#  results $1/*.out to $1/*.sparc2.
#
#  If $2 is given, it is assumed to be the anldp use.
#  If $3 is given, it is the filename extension for output files.
#  If $4 is given, it is the filename extension for old output files.
#

if ($#argv == 0) then
    echo "usage: Run_anldp directory [anldp | anldp new | anldp new old]"
    exit 1
endif

if ($#argv >= 2) then
   set ANLDP=$2
else
   set ANLDP=../../anldp
endif

if ($#argv >= 3) then
   set NEW_OUT=$3
else
   set NEW_OUT=out
endif

if ($#argv >= 4) then
   set OLD_OUT=$4
else
   set OLD_OUT=PII400
endif

if ($1 == propositional) then
    set inputs="p8.in q10.in"
    set parms=
else if ($1 == sorted) then
    set inputs="jobs.in"
    set parms="-z"
else
    echo "bad directory name"
    exit 1
endif

cd $1
set errors=0
foreach i ($inputs)
    echo "Running $1/$i ..."
    $ANLDP $parms < $i > $i:r.$NEW_OUT
    if (-e $i:r.$OLD_OUT) then
      set unit_old=`grep "Unit assign" $i:r.$OLD_OUT | awk '{print $3}' | tail -1`
      set unit_new=`grep "Unit assign" $i:r.$NEW_OUT | awk '{print $3}' | tail -1`
      if ($unit_new != $unit_old) then
        echo "There is a problem: $NEW_OUT assigned $unit_new, $OLD_OUT assigned $unit_old"
        set errors=1
      else
        echo "Okay, it seems to have run correctly."
      endif
    else
      echo "Okay, but there's no file for comparison."
      set errors=1
    endif
end

echo ""

if (! $errors) then
  set time_old=`grep "^user CPU time" *.$OLD_OUT | awk '{sum += $4} END {print sum}'`
  set time_new=`grep "^user CPU time" *.$NEW_OUT | awk '{sum += $4} END {print sum}'`
  set speedup=`awk "END {print $time_old / $time_new}" /dev/null`
  echo "time_new=$time_new, time_old=$time_old."
  echo "The $NEW_OUT times are about $speedup times as fast as the $OLD_OUT times."
endif

echo "The output files have been left in $1/*.$NEW_OUT."
echo ""

cd ..
