#!/bin/ksh
#
#Run this test by specifing the path-name where the BATCH Node is installed
#
CURR_DIR=`pwd`;export CURR_DIR
SKILLED=yes;export SKILLED
DEMO_NAME=ora1
MYNAME=`basename $0`
MESSAGE="cleaning up"

catch_sign()
{
  trap catch_sign 2
  trap catch_sign 3
  trap catch_sign 15
  trap catch_sign 1
  echo "\n$MYNAME: received BREAK, $MESSAGE ...\c"
  MESSAGE="please wait"
  cd $CURR_DIR
  echo ".\c"
  abtjob $num >/dev/null 2>&1
  echo ".\c"
  insjob $num -w >/dev/null 2>&1
  echo ".\c"
  ebmsys -d$DEMO_NAME >/dev/null 2>&1
  echo ".\c"
  if [ -f $ROOTPSG/.install.$DEMO_NAME ]
  then
    echo ".\c"
    cp $ROOTPSG/.install.$DEMO_NAME $ROOTPSG/.install
    echo ".\c"
    chmod 755 $ROOTPSG/.install
    echo ".\c"
    rm $ROOTPSG/.install.$DEMO_NAME
    echo ".\c"
  fi
  echo ".\c"
  dltact ${DEMO_NAME} >/dev/null 2>&1
  echo ".\c"
  make clean > /dev/null 2>&1
  echo ".\c"
  rm -rf $CURR_DIR/sysoutdir $CURR_DIR/formdir >/dev/null 2>&1
  echo ".\c"
  rm -rf $CURR_DIR/InstalledRTSFS
  echo ".\c"
  rm -f $CURR_DIR/ebmsetup
  echo ".\c"
  rm -f $CURR_DIR/File_Map
  echo "."
  echo "$MYNAME: ended"
  exit 2
}

trap catch_sign 1
trap catch_sign 2
trap catch_sign 3
trap catch_sign 15


echo "\n%%%%%%%%%%%% Starting the Oracle test %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
if [ $# -eq 0 ]
then
  echo "Error: specify the directory where the BATCH Node is installed. $MYNAME aborted"
  exit 1
fi
if [ $# -eq 2 ]
then
  ACTION=$2
else
# default
  ACTION=LOAD_AND_UNLOAD_TEST
fi

if [ ! -f $1/batchenv ]
then
  echo "Error: invalid BATCH Node installed dir. $MYNAME aborted"
  exit 1
fi
. $1/batchenv

#
# { Begin action "LOAD_TEST" or "LOAD_AND_UNLOAD_TEST"
#
if [ "$ACTION" = "LOAD_TEST" -o "$ACTION" = "LOAD_AND_UNLOAD_TEST" ]
then

#Checking if all variables are set
if [ "+$ROOTPSG" = "+" ]
then
  echo "Error: BATCH environment variables not set. $MYNAME aborted"
  exit 1
fi
if [ "+$ORACLE_HOME" = "+" ]
then
  echo "Error: ORACLE_HOME not set. $MYNAME aborted"
  exit 1
fi
if [ "+$ORACLE_SID" = "+" ]
then
  echo "Error: ORACLE_SID not set. $MYNAME aborted"
  exit 1
fi
if [ "+$COBDIR" = "+" ]
then
  echo "Error: COBDIR not set. $MYNAME aborted"
  exit 1
fi

#Setting additional variables
FILEMAP=$CURR_DIR/File_Map;export FILEMAP
PROCLIB=$PUBLIC/proc;export PROCLIB
PATH=$ORACLE_HOME/bin:$COBDIR/bin:$PATH;export PATH
LD_LIBRARY_PATH=$COBDIR/coblib:$LD_LIBRARY_PATH;export LD_LIBRARY_PATH
LIBPATH=$COBDIR/coblib:$LIBPATH;export LIBPATH

#Checking if the $DEMO_NAME BATCH subsystem is already defined
ebmsys -t | grep " $DEMO_NAME "
if [ $? -eq 0 ]
then
  echo "Error: The BATCH subsystem $DEMO_NAME is already defined. $MYNAME aborted"
  exit 1
fi

#Checking if the $DEMO_NAME activity is already defined
infact $DEMO_NAME | grep "class="
if [ $? -eq 0 ]
then
  echo "Error: The BATCH activity $DEMO_NAME is already defined. $MYNAME aborted"
  exit 1
fi

#Creating the sysoutdir and formdir directories
mkdir sysoutdir
mkdir formdir

#Updating the BATCH .install file
echo "\n%%%%%%%%%%%% Updating the BATCH .install file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
cp $ROOTPSG/.install $ROOTPSG/.install.$DEMO_NAME
if [ $? -ne 0 ]
then
  echo "Error: creating the $ROOTPSG/.install.$DEMO_NAME file. $MYNAME aborted"
  rm -rf sysoutdir formdir
  exit 1
fi
chmod 755 $ROOTPSG/.install.$DEMO_NAME
chmod 755 $ROOTPSG/.install
if [ $? -ne 0 ]
then
  echo "Error: updating the $ROOTPSG/.install file. $MYNAME aborted"
  rm $ROOTPSG/.install.$DEMO_NAME
  rm -rf sysoutdir formdir
  exit 1
fi

echo "$DEMO_NAME=$CURR_DIR/ebmsetup" >> $ROOTPSG/.install
if [ $? -ne 0 ]
then
  echo "Error: updating the $ROOTPSG/.install file"
  cp $ROOTPSG/.install.$DEMO_NAME $ROOTPSG/.install
  rm $ROOTPSG/.install.$DEMO_NAME
  chmod 755 $ROOTPSG/.install
  rm -rf sysoutdir formdir
  exit 1
fi

#Creating the File_Map
echo "\n%%%%%%%%%%%% Creating the File_Map file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
cp $PUBLIC/File_Map $FILEMAP
chmod 644 $FILEMAP
echo "USR1.JOBLIB.DEMO;__LIB;FS;$CURR_DIR;;0;" >> $FILEMAP

#Creating the BATCH subsystem setup file
echo "\n%%%%%%%%%%%% Creating the BATCH subsystem setup file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
cp ebmsetup.in ebmsetup
chmod 644 ebmsetup
echo "" >> ebmsetup
echo "#The following are Oracle demo specific" >> ebmsetup
echo "setenv CURR_DIR $CURR_DIR" >> ebmsetup
echo "setenv SYSOUTDIR $CURR_DIR/sysoutdir" >> ebmsetup
echo "setenv FORMS $CURR_DIR/formdir" >> ebmsetup
echo "setenv FILEMAP $CURR_DIR/File_Map" >> ebmsetup
echo "setenv RTS_RDBMS ORA_APPL" >> ebmsetup
echo "setenv RUNBDIR $CURR_DIR/InstalledRTSFS" >> ebmsetup
echo "setenv RUNBPATH $CURR_DIR/InstalledRTSFS" >> ebmsetup
echo "" >> ebmsetup
echo "#The following are Oracle specific" >> ebmsetup
echo "setenv ORACLE_HOME $ORACLE_HOME" >> ebmsetup
echo "setenv ORACLE_SID $ORACLE_SID" >> ebmsetup
echo "setenv PATH $ORACLE_HOME/bin:\$PATH" >> ebmsetup
if [ ! -z "$TWO_TASK" ]
then
  echo "setenv TWO_TASK $TWO_TASK" >> ebmsetup
fi
echo "" >> ebmsetup
echo "#The following are COBOL Micro Focus specific" >> ebmsetup
echo "setenv COBDIR $COBDIR" >> ebmsetup
echo "setenv LD_LIBRARY_PATH $COBDIR/coblib:$LD_LIBRARY_PATH" >> ebmsetup
echo "setenv LIBPATH $COBDIR/coblib:$LIBPATH" >> ebmsetup

#Creating the COBOL executable and the BATCH job
echo "\n%%%%%%%%%%%% Compiling the COBOL program and the BATCH job %%%%%%%%%%%%%%%%%%%%%%%\n"
make clean 2>&1
make all 2>&1
if [ $? -ne 0 ]
then
  echo "Error: creating the COBOL executable and the BATCH job. $MYNAME aborted"
  cp $ROOTPSG/.install.$DEMO_NAME $ROOTPSG/.install
  rm $ROOTPSG/.install.$DEMO_NAME
  chmod 755 $ROOTPSG/.install
  rm -rf sysoutdir formdir
  rm -rf $CURR_DIR/InstalledRTSFS
  rm -f ebmsetup
  rm -f $FILEMAP
  exit 1
fi

#Creating the BATCH Runtime System
echo "\n%%%%%%%%%%%% Creating the BATCH Runtime System %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
if [ -d $CURR_DIR/InstalledRTSFS ]
then
  rm -rf $CURR_DIR/InstalledRTSFS
fi
cd $PACK/RTSFS
./inst_rtsfs << !
1
$CURR_DIR
y

2
b
y

x
!
cd $CURR_DIR

#Creating the BATCH subsystem
echo "\n%%%%%%%%%%%% Creating the BATCH subsystem $DEMO_NAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
ebmsys -c$DEMO_NAME
if [ $? -ne 0 ]
then
  echo "Error: creating the BATCH subsystem $DEMO_NAME. $MYNAME aborted"
  cp $ROOTPSG/.install.$DEMO_NAME $ROOTPSG/.install
  rm $ROOTPSG/.install.$DEMO_NAME
  chmod 755 $ROOTPSG/.install
  rm -rf sysoutdir formdir
  rm -rf $CURR_DIR/InstalledRTSFS
  rm -f ebmsetup
  rm -f $FILEMAP
  exit 1
fi

#Creating an activity on class z
echo "\n%%%%%%%%%%%% Creating the BATCH activity $DEMO_NAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
crtact $DEMO_NAME -cz
if [ $? -ne 0 ]
then
  echo "Error: creating activity $DEMO_NAME. $MYNAME aborted"
  ebmsys -d$DEMO_NAME
  cp $ROOTPSG/.install.$DEMO_NAME $ROOTPSG/.install
  rm $ROOTPSG/.install.$DEMO_NAME
  chmod 755 $ROOTPSG/.install
  rm -rf sysoutdir formdir
  rm -rf $CURR_DIR/InstalledRTSFS
  rm -f ebmsetup
  rm -f $FILEMAP
  exit 1
fi

#Submitting the job
echo "\n%%%%%%%%%%%% Submitting the BATCH job %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
num=`subjob jobora -k$DEMO_NAME -j -cz`
insjob $num >/dev/null 2>&1
if [ $? -ne 0 ]
then
  echo "Error: submitting the job: $num. $MYNAME aborted"
  ebmsys -d$DEMO_NAME
  cp $ROOTPSG/.install.$DEMO_NAME $ROOTPSG/.install
  rm $ROOTPSG/.install.$DEMO_NAME
  chmod 755 $ROOTPSG/.install
  dltact $DEMO_NAME
  rm -rf sysoutdir formdir
  rm -rf $CURR_DIR/InstalledRTSFS
  rm -f ebmsetup
  rm -f $FILEMAP
  exit 1
fi
lstjob $num

#Waiting on its termination
echo "\n%%%%%%%%%%%% Waiting until the job terminates %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
insjob $num -w
STATUS=$?

#Printing the job output
echo "\n%%%%%%%%%%%% Printing the job output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
lststs -j$num -c
echo "\n%%%%%%%%%%%% End of the job output %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"

if [ $STATUS -ne 0 ]
then
  echo "Error job is aborted"
  echo "\n%%%%%%%%%%%% Error: the job is aborted %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
else
  echo "\n%%%%%%%%%%%% The job completed succesfully %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
fi

fi
#
# } End action "LOAD_TEST" or "LOAD_AND_UNLOAD_TEST"
#

#
# { Begin action "UNLOAD_TEST" or "LOAD_AND_UNLOAD_TEST"
#
if [ "$ACTION" = "UNLOAD_TEST" -o "$ACTION" = "LOAD_AND_UNLOAD_TEST" ]
then

#Deleting the BATCH subsystem
echo "\n%%%%%%%%%%%% Deleting the BATCH subsystem $DEMO_NAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
ebmsys -d$DEMO_NAME

#Restoring the BATCH .install file
echo "\n%%%%%%%%%%%% Restoring the BATCH .install file %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
cp $ROOTPSG/.install.$DEMO_NAME $ROOTPSG/.install
rm $ROOTPSG/.install.$DEMO_NAME
chmod 755 $ROOTPSG/.install

#Deleting the BATCH activity
echo "\n%%%%%%%%%%%% Deleting the BATCH activity $DEMO_NAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
dltact $DEMO_NAME

#Deleting the executables
make clean 2>&1

#Deleting the sysoutdir and formdir directories
rm -rf sysoutdir formdir
rm -rf $CURR_DIR/InstalledRTSFS
rm -f ebmsetup
rm -f $CURR_DIR/File_Map

fi
#
# } End action "UNLOAD_TEST" or "LOAD_AND_UNLOAD_TEST"
#

echo "\n%%%%%%%%%%%% End of the Oracle test %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
exit $STATUS
