#!/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=db21
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"
  sleep 1
  echo ".\c"
#db2 -svt << !
db2 -svt >/dev/null 2>&1 << !
DELETE from ACTREC;
DELETE from ACCTOCRS;
commit;
connect reset;
terminate;
!
  echo ".\c"
  insjob $num -w >/dev/null 2>&1
  echo ".\c"
  ebmsys -d$DEMO_NAME >/dev/null 2>&1
  echo ".\c"
#db2 -svt << !
db2 -svt >/dev/null 2>&1 << !
DROP table ACTREC;
DROP table ACCTOCRS;
connect reset;
terminate;
!
  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"
#db2 -svt << !
db2 -svt >/dev/null 2>&1 << !
drop database unikix;
terminate;
!
  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 DB2/6000 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

#Checking if all variables are set
if [ "+$ROOTPSG" = "+" ]
then
  echo "Error: batch environment variables not set. $MYNAME aborted"
  exit 1
fi
if [ "+$DB2INSTANCE" = "+" ]
then
  echo "Error: DB2INSTANCE not set. $MYNAME aborted"
  exit 1
fi
if [ "+$DB2DBDFT" = "+" ]
then
  echo "Error: DB2DBDFT not set. $MYNAME aborted"
  exit 1
fi
if [ "+$INSTHOME" = "+" ]
then
  echo "Error: INSTHOME 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=$INSTHOME/sqllib/bin:$INSTHOME/sqllib/adm:$INSTHOME/sqllib:$COBDIR/bin:$PATH;export PATH
LD_LIBRARY_PATH=$COBDIR/coblib:$LD_LIBRARY_PATH;export LD_LIBRARY_PATH
LIBPATH=$COBDIR/coblib:$LIBPATH;export LIBPATH
COBCPY=$COBDIR/cpylib;export COBCPY
COBOPT=$COBDIR/src/sql/cobopt.sql;export COBOPT

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

#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 DB2/6000 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 DB2_APPL" >> ebmsetup
echo "setenv RUNBDIR $CURR_DIR/InstalledRTSFS" >> ebmsetup
echo "setenv RUNBPATH $CURR_DIR/InstalledRTSFS" >> ebmsetup
echo "" >> ebmsetup
echo "#The following are DB2/6000 specific" >> ebmsetup
echo "setenv DB2INSTANCE $DB2INSTANCE" >> ebmsetup
echo "setenv DB2DBDFT $DB2DBDFT" >> ebmsetup
echo "setenv INSTHOME $INSTHOME" >> ebmsetup
echo "setenv PATH \$INSTHOME/sqllib/bin:\$INSTHOME/sqllib/adm:\$INSTHOME/sqllib:\$PATH" >> ebmsetup
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
echo "setenv COBCPY $COBDIR/cpylib" >> ebmsetup
echo "setenv COBOPT $COBDIR/src/sql/cobopt.sql" >> ebmsetup

#Creating the database
echo "%%%%%%%%%%%%%%%%%%%% Loading the DDL statements %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
db2 -svtf createdemo.sql

#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
  db2 -svtf dropdemo.sql
  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

5
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
  db2 -svtf dropdemo.sql
  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
  db2 -svtf dropdemo.sql
  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 jobdb2 -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
  db2 -svtf dropdemo.sql
  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
#Dropping the database
echo "%%%%%%%%%%%%%%%%%%%% Unloading the Tables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
db2 -svtf dropdemo.sql

#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 DB2/6000 test %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
exit $STATUS
