#!/bin/sh

PROLOG=pl
PROLOG_WITH_ORACLE=pl-ora

PATH=/usr/5bin:/usr/bin:/bin:$PATH
export PATH

USAGE="Usage: check_timedb -o -e user/passwd file(s)\n\
               \t-o uses ORACLE to do comparison\n\
               \t-e uses the emulator to do comparison (default)\n\
               \t!! database 'user/passwd' is erased\n\
               \t!! verify \$TIMEDB_HOME, and \$ORACLE_EMULATOR"

if test $# -lt 2
then
  echo $USAGE
  exit
fi

############### option & argument handling

emulator=false
oracle=false

while getopts oeh opt
do
  case $opt in 
    e) emulator=true;;
    o) oracle=true;;
    h) echo $USAGE; exit
  esac
done
shift `expr $OPTIND - 1`

if test $oracle = false
then
  prolog=$PROLOG
  backendOpt=-e
else
  prolog=$PROLOG_WITH_ORACLE
  backendOpt=-o
fi

login=$1
shift 1
whichfiles=$*

if test $whichfiles = all
then
  files=demo??
else
  files=$whichfiles
fi

start_dir=$PWD

############### verifying test file(s)

for f in $files
do
  cd $TIMEDB_HOME
  initdb $backendOpt $login
  cd $start_dir
  date
  echo "Differences found running '$f' against the reference file:"
  echo "-------------------------------------------------------------"
  grep '^[^#]' $f >tmp.in
  $prolog -A0 -- -w >/dev/null <<EOF
     consult('$TIMEDB_HOME/timeDB.pl').
     atsql2.
     open '$login';
     batch 'tmp.in' '$f.out';
     quit;
EOF
  sed -e 's/[ .]//g'\
      -e '/^-$/d'\
      -e '/^$/d'         $f.out > tmp.out
  sed -e 's/[ .]//g'\
      -e '/^-$/d'\
      -e '/^$/d'         $f.ref > tmp.ref
  diff tmp.out tmp.ref
  echo "End of list of differences found when checking file '$f'"
  echo "-----------------------------------------------------------"
  date
done

rm tmp.out tmp.ref tmp.in

exit 0

