#!/bin/ksh
#
# NAME:         ctgassist
#               Script to wrapper calls of the executable ctgassistant.jar
#
# Licensed Materials - Property of IBM  
#  
# "Restricted Materials of IBM"  
#  
# 5724-I81,5725-B65,5655-ZA1 
#  
# (C) Copyright IBM Corp. 2014  
#  
# US Government Users Restricted Rights - Use, duplication or  
# disclosure restricted by GSA ADP Schedule Contract with  
# IBM Corp.  
# 
# Status: Version 10 Release 1 
#
#######################################################################
#
# Useful function for debugging
#
#######################################################################
#
function debug {
# to switch on set environment variable CTGDEBUG to any non-empty value
   if [[ "$CTGDEBUG" != "" ]]
   then
      if [[ "$1" = "" ]]
      then
         echo "DEBUG: enter data to continue" 1>&2
         read INPUT
      else
         echo "$1" 1>&2
      fi
   fi
}

#######################################################################
#
# Find out where the script is
#
#######################################################################
#
# Using the name we were called with,
#   check at least once for symbolic dereferencing...
#   on all platforms "->" does not need exceptions "\", but
#   on some (zOS, sol and lnx390) cannot have "\>".
# Set FILENAME = absolute file name
# Set DIRNAME = absolute directory name
#
#######################################################################
#
debug "make absolute - $0"
FILENAME=$0
symbolic=1
while [[ $symbolic -eq 1 ]]
do
   symbolic=0
   PATHNAME=$(ls -l $FILENAME 2> /dev/null | grep " -> /")
   if [[ $? -eq 0 ]]
   then
      debug "Symbolic link is absolute with grep  -> /"
      symbolic=1
      FILENAME=$(echo $PATHNAME | awk ' { print $NF }')
   else
      PATHNAME=$(ls -l $FILENAME 2> /dev/null | grep " -> ")
      if [[ $? -eq 0 ]]
      then
        debug "Symbolic link is relative with grep  -> "
        symbolic=1
        FILENAME=$(dirname $FILENAME)/$(echo $PATHNAME | awk ' { print $NF }')
      fi
   fi
done
if [[ "$(echo "$FILENAME" | cut -c1)" = "." ]]
then
   debug "make absolute: (relative) FILENAME=$FILENAME"
   FILENAME=${PWD}$(echo $FILENAME | cut -c2-9999 )
fi
if [[ "$(echo "$FILENAME" | cut -c1)" != "/" ]]
then
   debug "make absolute: (relative) FILENAME=$FILENAME"
   FILENAME=${PWD}/${FILENAME}
fi
DIRNAME=$(dirname $FILENAME)
debug "make absolute: DIRNAME=$DIRNAME, FILENAME=$FILENAME"
SCRIPT=$FILENAME
SCRIPT_DIR=$DIRNAME
debug "SCRIPT = $SCRIPT, SCRIPT_DIR = $SCRIPT_DIR"

#######################################################################
#
# Find the executable ctgassistant.jar
#
#######################################################################
# Try SDK directory structure
jarname=$SCRIPT_DIR/classes/ctgassistant.jar
if [[ ! -f $jarname ]]
then
   debug "Could not find ctgassistant.jar with SDK directory, trying product directory"
    
   # Assume product directory structure
   CTG_INSTALL_DIR=$(dirname $SCRIPT_DIR)   
   jarname=$CTG_INSTALL_DIR/classes/ctgassistant.jar
fi

#######################################################################
#
# Define the java executable
#
#######################################################################
#
if [[ -z "$CTG_JAVA" ]]
then 
   if [[ -n "$CTG_INSTALL_DIR" ]]
   then 
      # On Multiplatform use the CICS TG installed JVM 
      JVMVRM=`ls -d $CTG_INSTALL_DIR/jvm* 2>/dev/null | sort| tail -1`
      if [[ -n "$JVMVRM" ]]
      then
         CTG_JAVA="$JVMVRM/bin/java"
      fi
   fi
   if [[ -f "$CTG_JAVA" ]]
   then 
      debug "Using Java installed by CICS TG: $CTG_JAVA"
   else
      CTG_JAVA=`which java`
      debug "Using Java as found on the PATH: $CTG_JAVA"
   fi
else 
   debug "Using Java defined by variable CTG_JAVA: $CTG_JAVA"
fi

#######################################################################
#
# Check for debug switch
#
#######################################################################
#
if [[ "$1" = "-X" ]]
then
   debugSwitch=-Ddebug
   shift 1 
fi

#######################################################################
#
# Invoke ctgassistant.jar
# If too many arguments were specified, print help
#
#######################################################################
#
if [[ "z$2" = "z" ]]
then
   debug "$CTG_JAVA $debugSwitch -jar $jarname $1"
   $CTG_JAVA $debugSwitch -jar $jarname $1
else
   $CTG_JAVA $debugSwitch -jar $jarname -?
fi
