#!/bin/csh
#
#  Run some anldp jobs and compare the
#  results to existing outputs.
#
#  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

set DIR=$1

if ($#argv >= 2) then
   set ANLDP=$2
else
   set ANLDP=../../bin/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=out
endif

cd $DIR
set errors=0
foreach i (*.in)
    echo "Running $DIR/$i ..."
    $ANLDP -m1000 < $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=`cat *.$OLD_OUT | grep "CPU time"| awk '{sum += $4} END {print sum}'`
  set time_new=`cat *.$NEW_OUT | grep "CPU time"| 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 $DIR/*.$NEW_OUT."
echo ""

cd ..
