#!/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=syb1
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 Sybase 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 [ "+$SYBASE" = "+" ]
then
  echo "Error: SYBASE 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=$SYBASE/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 Sybase 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 SYB_APPL" >> ebmsetup
echo "setenv RUNBDIR $CURR_DIR/InstalledRTSFS" >> ebmsetup
echo "setenv RUNBPATH $CURR_DIR/InstalledRTSFS" >> ebmsetup
echo "" >> ebmsetup
echo "#The following are Sybase specific" >> ebmsetup
echo "setenv SYBASE $SYBASE" >> ebmsetup
echo "setenv PATH $SYBASE/bin:\$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

#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

7
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 jobsyb -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 Sybase test %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
exit $STATUS
