#!/usr/bin/ksh
Usage="Usage: $0 [-t testcase1,testcase2,...] dir-name notification wait-time"

############################################################################
# script name: RunIUT
#
# Description of the script:
# --------------------------
#    Executes the basic path test and the integrated unit
#    tests in the following manner:
#
#       if Build(executables) was successful
#          if testcases are given
#             run all the given IUT(runAlliut)
#          else
#             run all the pre-selected IUTs(runAlliut)
#          fi
#          if notify switch is on
#             notify developer
#          fi
#       else
#          exit 1
#
# Switches:
# ---------
#
#    -t testcase1,testcase2,...
#
# Input parameters:
# -----------------
#    $1(dir-name): a directory that contains the PD source code.
#    $2(notification): should the developers be notified of the result?(yes/no)
#                      default: "no".
#    $3(wait-time): total time in minutes this script should wait for the
#                   file to exist.
#                   default: 120
#
# Assumptions:
# ------------
#    This script gets integrated unit test case names from $PD_DEV/pdtest/tc
#    directory. All of the test case names must end with ".tc".
#       e.g.) pdcreate.tc
#    The name of the integrated unit test case that must be run every day
#    must end with ".tc.s".
#       e.g.) pdcreate.tc.s
#
############################################################################

typeset testCaseList=""
while getopts "t:" opt; do	# Parse command line switches. JJB,22Aug95.
    case $opt in
      t) testCaseList="$( echo $OPTARG | tr ',' ' ' )";;
      /?) exit 2;;
    esac
done
shift OPTIND-1                  # Remove options from command line.
umask 000

if test "$1" = "?"
then
  echo
  echo $Usage
  echo ""
  echo "   -t testcase1,testcase2,... //HP,22Aug95"
  echo ""
  echo "         run only specific testcase(s)"
  echo ""
  echo "         e.g. $ RunIUT -t jobs.tc"
  echo ""
  echo "   dir-name:"
  echo ""
  echo "         dir-name is the name of a Palladium root directory that"
  echo "         contains the source code to be built. (default:  /pd_dev)."
  echo
  echo "   notification:   should the developers be notified of the result"
  echo "                   - 'yes'"
  echo "                   - 'no'"
  echo "                  (default: no )"
  echo
  echo "   wait-time"
  echo "               - if wait-flag = 'yes'  this indicates the total"
  echo "                 time in minutes this script should wait for the the"
  echo "                 the file to exist"
  echo "                ( default 120 minutes)"
  echo
  echo "   runAllIut"
  echo "               - if runAllIut = 'yes'  this indicates the all"
  echo "                 IUT must be run. (yes/no)"
  echo "                ( default: no)"
  echo
  echo " "
  exit 1
fi

#--------------------------------------------------------------------------
# set input parameters

export OSname=$(uname)		# Operating System Name (AIX|HP-UX). JJB.

if test "$1" = ""
then
  if [ "$OSname" = "HP-UX" ] 
  then
     PD_DEV=${PD_DEV:=/pd/$LOGNAME} # Use curr user's dir. JJB.
  else
     PD_DEV="/pd_dev"
  fi
else
  PD_DEV=$1
fi

if [[ ( $2 = "yes") || ( $2 = "no" )  ]]
then
  NOTIFY=$2
else
  NOTIFY="no"
fi

if test "$3" = ""
then
   wttime=0
else
   wttime=$3
fi

if [[ ( $4 = "yes") || ( $4 = "no" )  ]]
then
  ALLIUT=$4
else
  ALLIUT="no"
fi

#--------------------------------------------------------------------------
# variable initialization
UT_RC=0    # unit test return code
IUT_RC=0   # integrated unit test return code

export PD_DEV
export PD_SERVER_LOG_CERR=yes 
export PD_DIR=${PD_DIR:=$PD_DEV}
export PD_PATH=$PD_DIR/bin
export PD_LBIN=${PD_LBIN:=$PD_DIR/bin}
export RTPATH=${RTPATH:=$PD_DEV/pdtest}
export PD_AUTO_DELETE=no
export PDPRINTER=vail
export TCPATH=$RTPATH/tc
export STEPPATH=$RTPATH/step
export RESULTS_DIR="$RTPATH/iutResults"
export PDPRNPATH=$PD_DIR/lib/model
export SUMFILE="$RESULTS_DIR/iutSummary.out"
export PREFIX=`hostname`-$LOGNAME-iut
export PD_OIDS_PATH=${PD_OIDS_PATH:=$PD_DIR/oids}
export USER=${USER:=$LOGNAME}
export TCPIP=$(nslookup $(hostname) | sed "1,4d; s/,//g" |awk '{print $2}')

# Start the null printer. Ignore the error is it's already running

rm -f $RTPATH/bin/pdnullptr
/usr/ccs/bin/cc $RTPATH/bin/pdnullptr.c -o $RTPATH/bin/pdnullptr
$RTPATH/bin/pdnullptr > /dev/null &

# we do not want to say yes interactively
export PD_CONFIRM_DELETE=no

if [ $OSname = HP-UX ]; then
    export ClientDaemonName=pdclientd
    export DCEloginName=adm_user
    export DCEpassword=pddev
    export pdlsLocation=$PD_PATH/pdls # Used in startspl script.
else
    export ClientDaemonName=psmd
    export DCEloginName=cell_admin
    export DCEpassword=pass
    export pdlsLocation=/usr/lpp/pd/bin/pdls
fi

# $RESULTS_DIR has all of the output files from IUT scripts.
if [[ ! -d "$RESULTS_DIR" ]]
then
   mkdir $RESULTS_DIR
fi

# $RESULTS_DIR/recap has summary files for each test case and the error 
# files.
if [[ ! -d $RESULTS_DIR/recap ]]
then
   mkdir $RESULTS_DIR/recap
fi

rm -f $RESULTS_DIR/recap/* # Added -f switch. JJB.

echo " Summary file      :: $SUMFILE"
echo " results directory :: $RESULTS_DIR/recap"

# copy the previous runs to ./prevRun directory.
if [[ ! -d $RESULTS_DIR/prevRun ]]
then
   mkdir $RESULTS_DIR/prevRun
fi

# prevRun directory has the previous runs
# clean up the $RESULTS_DIR for the new files
rm -f $RESULTS_DIR/prevRun/*                   # Added -f switch. JJB.
mv -f $RESULTS_DIR/*.out $RESULTS_DIR/prevRun/ # Added -f switch. JJB.

if [[ $PD_ENV != "basic" ]]
then
	echo "using extended environment"
else
	echo "using basic environment"
fi

#-------------------------------------------------------------------------
# START WAIT-TIME BLOCK.
#-------------------------------------------------------------------------



if [ $wttime -ne 0 ]; then
   integer wait=`expr $wttime / 5 `

   integer i=0
   while ((i < wait))
   do
      echo "Wait time:" $wttime  "Wait Count:" $i
      ((i+=1))

      # wait for the flag files (BuildAllexe.Complete)
      if test -f $PD_DEV/utils/BuildAllexe.Complete
      then
         ((i+=wait))
      else
         sleep 300
      fi
   done

   if [[ -f $PD_DEV/utils/BuildAllexe.Complete ]]
   then
      echo "Flag file(BuildAllexe.Complete) exists"
   else
      echo "Flag file(BuildAllexe.Complete) does NOT EXIST"
      echo "Building PD executables for client, spooler, supervisor,"
      echo "and pdclientd FAILED!!"
      exit 1
   fi

   integer wait=`expr $wttime / 5 `

   integer i=0
   while ((i < wait))
   do
      echo "Wait time:" $wttime  "Wait Count:" $i
      ((i+=1))

      # wait for the flag files (BuildAllut.Complete)
      if test -f $PD_DEV/utils/BuildAllut.Complete
      then
         ((i+=wait))
      else
         sleep 300
      fi
   done

   if [[ -f $PD_DEV/utils/BuildAllut.Complete ]]
   then
      echo "Flag file(BuildAllut.Complete) exists."
      #  wall "Starting the unit test cases for: " $PD_DEV
      rm $PD_DEV/utils/BuildAllut.Complete
   else
      echo "Flag file(BuildAllut.Complete) does NOT exist."
   fi
fi
#-------------------------------------------------------------------------
# END WAIT-TIME BLOCK.
#-------------------------------------------------------------------------

[ $(uname) != HP-UX ] && echo "Detailed output of IUT run can be found in $PD_DEV/utils/RunIUT.out." | tee -a $SUMFILE

#--------------------------------------------------------------------------
# run the basic path test
cd $RTPATH

HASFAILED="no"

if [ "$testCaseList" = "" ]    # we need to get all test cases
then
#  BROOT=/pd/$LOGNAME/doc
#  testCaseList=`grep TESTCASE $BROOT/TestsI   |\
#                grep -v "\- TESTCASE"         |\
#                grep -v "NA@HP"               |\
#                awk '{print $1}'              |\
#                awk -F':' '{print $2}'`

# setup the all IUT script list
  testCaseList="
		basicPathTest.TC
		cleanNshutNow.tc
		disNen.tc
		dsfbase.TC
		dsfcanpa.tc
		dsfmisc.tc
		enq_gateway.TC
		errorlog.TC
		jobs.tc
		ivos.tc
		nls.tc
		nrjbehrs1.tc
		pauNres.s.tc
		testLp.tc
		testLs.tc
		testQ.tc
		testQ2.tc
		tracelog.TC"

# running Env IUT first
  export TCTESTSCRIPTS="
		setUpEnv.TC
		tearDownEnv.TC
		setUpLogEnv.TC
		tearDownLogEnv.TC"

  export TESTSCRIPTS=""

  reportfile=$RESULTS_DIR/envsetup.report
  errorfile=$RESULTS_DIR/envsetup.error
  echo "Report File :: $reportfile"
  echo "Error  file :: $errorfile"

  STARTTIME=`date`
  # log into DCE and run each test case
  echo "\n------------------------------------------------"
  echo "     Start ( Env Setup ) TEST"
  echo "     $STARTTIME"
  echo "------------------------------------------------"

  if [[ $PD_ENV != "basic" ]]
  then
    (dce_login $DCEloginName $DCEpassword -e $RTPATH/bin/runAlliut | \
     tee $reportfile ) 1>$errorfile 2>&1
  else
    $RTPATH/bin/runAlliut >$reportfile 2>$errorfile
  fi
  tail -1 $reportfile | grep "SUCCESS" 2>/dev/null 1>&2
  IUT_RC=$?
  
  if [[ $IUT_RC -ne 0 ]]
  then
     echo "\n Env Setup FAILED!!\n"
     echo "\n Env Setup FAILED!! -- rc = 1\n" >> $SUMFILE
     HASFAILED="yes"
  else
     echo "\n Env Setup passed.\n"
     echo "\n Env Setup passed. --            rc = 0\n" >> $SUMFILE
  fi
  ENDTIME=`date`
  echo "\n------------------------------------------------"
  echo "     End ( Env Setup ) TEST"
  echo "     $ENDTIME"
  echo "------------------------------------------------"

fi

echo " The following test case are set to be run:"
echo "$testCaseList"

# each IUT is wrapped around by setUpBasicEnv.TC 
# tearDownDefaultEnv.TC in runAlliut

for testcase in $testCaseList
do
  STARTTIME=`date`
  # log into DCE and run each test case
  echo "\n------------------------------------------------"
  echo "     Start ($testcase) TEST"
  echo "     $STARTTIME"
  echo "------------------------------------------------"

  # match requirement in pdtest/runAlliut
  export TESTSCRIPTS="$testcase"  
  export TCTESTSCRIPTS=""

  reportfile=$RESULTS_DIR/$testcase.report
  errorfile=$RESULTS_DIR/$testcase.error
  echo "Report File :: $reportfile"
  echo "Error  file :: $errorfile"
  if [[ $PD_ENV != "basic" ]]
  then
    (dce_login $DCEloginName $DCEpassword -e $RTPATH/bin/runAlliut | \
     tee $reportfile ) 1>$errorfile 2>&1
  else
    $RTPATH/bin/runAlliut >$reportfile 2>$errorfile
  fi
  tail -1 $reportfile | grep "SUCCESS" 2>/dev/null 1>&2

  IUT_RC=$?
  
  if [[ $IUT_RC -ne 0 ]]
  then
     echo "\n$testcase FAILED!!\n"
     echo "\n$testcase FAILED!! -- rc = 1\n" >> $SUMFILE
     HASFAILED="yes"
  else
     echo "\n$testcase passed.\n"
     echo "\n$testcase passed. --            rc = 0\n" >> $SUMFILE
  fi
  ENDTIME=`date`
  echo "\n------------------------------------------------"
  echo "     End ($testcase) TEST"
  echo "     $ENDTIME"
  echo "------------------------------------------------"
done

if [[ $HASFAILED = "yes" ]]
then
  if [[ $NOTIFY = "yes" ]]
  then
    DATE=`date`
    msg="IUT Results - Dir:$PD_DEV  Date:$DATE"
    $PD_DEV/utils/NotifyDev "$msg" $SUMFILE
  fi
  echo "End of RunIUT.\n"
  exit 1
fi

echo "End of RunIUT.\n"
exit 0


#-----------------------------------------------------------------------
#    Version      Date     Time    Owner   Comment
# ------------- -------- -------- -------- ------------------------------

# V1.2          06/22/94 09:11:41 kok      Update for naming error
# V1.4          07/18/94 19:24:44 nrjbehrs IUT fixes
# V1.5          07/19/94 14:33:16 nrjbehrs IUT fixes
# V1.6          07/21/94 17:24:14 nrjbehrs IUT cleanup
# V1.7          07/22/94 11:19:26 nrjbehrs IUT cleanup
# V1.8          08/15/94 15:00:44 nrjbehrs IUT cleanup
# V1.9          08/18/94 13:11:33 nrjbehrs IUT cleanup
# V1.10         09/28/94 16:42:43 kok      Add psf_printx.TC to execution
# V1.11         09/29/94 14:45:53 kok      Update IUT
# V1.12         10/03/94 15:14:38 kok      remove log and trace tc
# V1.12         10/03/94 15:18:54 kok      remove log and trace tc
# V1.14         11/15/94 15:34:04 nrjbehrs fix PD_DIR
# V1.15         11/15/94 15:49:56 nrjbehrs unset PDBASE
# V1.16         11/21/94 11:20:08 kok      Add cptodayold flag
# V1.17         11/22/94 08:32:36 kok      remove cptodayold flag
# V1.18         02/25/95 13:25:01 kok      remove rm of build flag file and add exit when build not good
# V1.19         04/12/95 10:45:21 kok      add testattrfiles testcase
# V1.20         04/13/95 07:52:15 kok      correct error when adding attr test case


