#!/bin/csh -f
#
# SXTEST - test SX
#
# Source Version: 2.0
# Software Release #92-0043
#
# include "cpyright.h"
#

#############################################################################XS

set System = `../manager/system-id`
set BinDir = ../dev/$System/bin
set ERROR  = "FALSE"
set FAIL   = "NO"
set status = 0

source ../manager/env-$System

set FrontEnd = ""
set DPE = `which pact-dpe`
if ("$DPE" != "") then
   if (-e "$DPE[1]") then
      source $DPE
   endif
endif

set SX = "$FrontEnd $BinDir/sx"

# (Template of test on size of DIFF compare (i.e. 12 char date differs)
#   set Files = `find . -name "sxpdb.res.diff" -size 12c -print`
#   if ($#Files == 0) then....  compare fails!


rm -f sxpdb.res  sxpdba.res  sxpdbi.res  sxhash.res  sxpgs.ps
rm -f sxpdb.res.diff  sxpdba.res.diff  sxpdbi.res.diff  sxhash.res.diff  sxpgs.ps.diff

if (-e ../dev/$System/include/sx.h) then

   echo ""
   echo "SX TEST..."

   rm -f sxtest.debug
   echo `../manager/pwhich pact` >& sxtest.debug
   echo `../manager/pwhich cc` >>& sxtest.debug
   echo "BinDir = $BinDir" >>& sxtest.debug

   echo ""
   echo -n "                    SX PDB Test ....."
   $SX -r -s -l sxpdb.scm > sxpdb.res
   diff sxpdb.res sxpdb.res.ref > sxpdb.res.diff
   set DiffStat = $status

   if ($DiffStat != 0) then
      echo ""
      echo "                    SX PDB Test ............... FAILED"
      set ERROR = "TRUE"
   else
      echo ".......... PASSED"
      rm -f sxpdb.res sxpdb.res.diff
   endif

   echo ""
   echo -n "                    SX PDB Attribute Test ....."
   $SX -r -s -l sxpdba.scm > sxpdba.res
   diff sxpdba.res sxpdba.res.ref > sxpdba.res.diff
   set DiffStat = $status

   if ($DiffStat != 0) then
      echo ""
      echo "                    SX PDB Attribute Test ..... FAILED"
      set ERROR = "TRUE"
   else
      echo " PASSED"
      rm -f sxpdba.res sxpdba.res.diff
   endif

   set sys = `uname -m`

   if ("$sys[1]" != "CRAY") then
      echo ""
      echo -n "                    SX PDB VIF Test ..........."
      $SX -p -q -l sxpdbi.scm > sxpdbi.res
      diff sxpdbi.res sxpdbi.res.ref > sxpdbi.res.diff
      set DiffStat = $status

      if ($DiffStat != 0) then
         cp sxpdbi.res sxpdbi.res.crash
         echo ""
         echo "                    SX PDB VIF Test ........... FAILED"
         set ERROR = "TRUE"
      else
         echo " PASSED"
         rm -f sxpdbi.res sxpdbi.res.diff
      endif
   endif

   echo ""
   echo -n "                    SX HASH Test ....."
   $SX -r -s -l sxhash.scm > sxhash.res.tmp
   sed -e '/diskaddr =/d' -e '/size =/d' -e '/alignment =/d' -e '/member_offs =/d' \
       -e '/cast_offs =/d' -e '/number =/d' -e '/order/d' -e '/format/d' \
       -e 's/#([^A-Za-z)]*)//g' \
       sxhash.res.tmp > sxhash.res
   diff sxhash.res sxhash.res.ref > sxhash.res.diff
   set DiffStat = $status

   if ($DiffStat != 0) then
      echo ""
      echo "                    SX HASH Test .............. FAILED"
      set ERROR = "TRUE"
   else
      echo "......... PASSED"
      rm -f sxhash.res.tmp sxhash.res sxhash.res.diff
   endif

   echo ""
   echo -n "                    SX PGS Test ....."
   $SX -r -s -l sxpgs.scm > sxpgs.ps

   ps-diff sxpgs.ps sxpgs.ps.ref > /dev/null
   set DiffStat = $status

   if ($DiffStat != 0) then
      echo ""
      echo "                    SX PGS Test ............... FAILED"
      set ERROR = "TRUE"
   else
      echo ".......... PASSED"
      rm -f sxpgs.ps
   endif

   echo ""
   echo -n "                    PDBVIEW Master Test ....."
   rm -f pdbvtest.log pdbvtest.pdb pdbvtest.asc pdbvtest.err pdbvtest.tmp
   $SX -p -l ./pdbvtest.scm >& pdbvtest.tmp
   if ($status != 0) then
      echo ""
      echo "                         Executable crashed"
      set ERROR = "TRUE"
   else
      grep -i "error" pdbvtest.tmp > pdbvtest.err
      set Files = `find . -name "pdbvtest.err" -size 0c -print`
      if ($#Files == 0) then
         echo ""
         set ERROR = "TRUE"
      else
         awk '(($1 != "%%CreationDate:") && !(($4 ~ /..:..:../) && ($5 ~ /199[0-9]/))) {print}' pdbvtest.ps > pdbvtest.ps.tmp
         mv pdbvtest.ps.tmp pdbvtest.ps

         ps-diff pdbvtest.ps pdbvtest.ps.ref > /dev/null
         set DiffStat = $status
   
         if ($DiffStat != 0) then
            echo ".. FAILED"
            echo "                         PS files differ"
            set ERROR = "TRUE"
	 else
            echo ".. PASSED"
            rm -f pdbvtest.log pdbvtest.pdb pdbvtest.asc pdbvtest.err pdbvtest.tmp
         endif
      endif
   endif

   echo ""
   if ($ERROR == "TRUE") then
      echo ""
      echo "Debug:"
      cat sxtest.debug
      echo ""
      echo "SX TEST...FAILED"
   else
      echo "SX TEST...PASSED"
   endif

   echo ""
   echo " --------------------------------------------------------"

else
   echo ""
   echo "SX Code Not Available"
   
endif

set xstatus = 0
if ($ERROR == "TRUE") then
   set xstatus = 1
endif

exit($xstatus)
