#!/bin/sh

#=======================================================================
#=== Storage Protect B/A client GUI startup script                  ===
#=== Modification to this file are not supported.                    ===
#=== Copyright IBM Corporation 2021                                  ===
#=======================================================================

#=======================================================================
#=== Definition of internal environment variables		     ===
#=======================================================================
DSMDIR1=/opt/tivoli/tsm/client/ba/bin
DSMDIR2=/usr/tivoli/tsm/client/ba/bin

#=======================================================================
#=== Check Java Runtime Environment
#=== Note: the current full version minimum is IBM JRE "1.7.0"
#=== For more details, see: https://www.ibm.com/support/pages/node/660813
#=== https://supportcontent.ibm.com/support/pages/how-use-ibm-spectrum-protect-gui-linux-os-where-installed-java-level-not-following-client-requirements
#=======================================================================
JRE_VERSION_SUPP=1
JRE_VERSION_SUPP_HIGHER=yes
JRE_RELEASE_SUPP=7
JRE_RELEASE_SUPP_HIGHER=yes
JRE_LEVEL_SUPP=0
JRE_LEVEL_SUPP_HIGHER=yes

if [ "$JRE_RELEASE_SUPP_HIGHER" = "yes" ]; then
   JRE_FULL_VERSION_SUPP="$JRE_VERSION_SUPP.$JRE_RELEASE_SUPP"
elif  [ "$JRE_LEVEL_SUPP_HIGHER" = "yes" ]; then
   JRE_FULL_VERSION_SUPP="$JRE_VERSION_SUPP.$JRE_RELEASE_SUPP.x where x >=  $JRE_LEVEL_SUPP"
else
   JRE_FULL_VERSION_SUPP="$JRE_VERSION_SUPP.$JRE_RELEASE_SUPP.$JRE_LEVEL_SUPP"
fi

JavaWrongVersion () {
   echo "ERROR:	 The Java Runtime Environment (JRE) version $JRE_FULL_VERSION_FOUND
       found in your environment is not supported!
       Please install the required JRE version $JRE_FULL_VERSION_SUPP"
   exit 1
}

JavaNotFound () {
   echo "ERROR: Java Runtime Environment (JRE) is not found!
       If you have already installed JRE, please be sure
       the \"java\" executable is set in the system PATH,
       otherwise install the required JRE version $JRE_FULL_VERSION_SUPP"
   exit 1
}

### Search for the Java Runtime Environment ###
JAVAPATH=$(which java 2>/dev/null)
[ $? -ne 0 ] && JavaNotFound

### Get JRE version, release and level number from the system ###
JRE_FULL_VERSION_TMP=$(java -version 2>&1 | awk '/version/ {print $3}')
JRE_FULL_VERSION_TMP=${JRE_FULL_VERSION_TMP#\"}
JRE_FULL_VERSION_TMP=${JRE_FULL_VERSION_TMP%\"}
JRE_VERSION_FOUND=${JRE_FULL_VERSION_TMP%%.*}
JRE_RELEASE_FOUND=${JRE_FULL_VERSION_TMP#*.}
JRE_RELEASE_FOUND=${JRE_RELEASE_FOUND%.*}
JRE_LEVEL_FOUND=${JRE_FULL_VERSION_TMP#*.}
JRE_LEVEL_FOUND=${JRE_LEVEL_FOUND#*.}
JRE_LEVEL_FOUND=${JRE_LEVEL_FOUND%*.}
JRE_FULL_VERSION_FOUND=$JRE_FULL_VERSION_TMP

### Check Java runtime is supported ###
if [ "$JRE_VERSION_FOUND" -lt "$JRE_VERSION_SUPP" ]; then
   JavaWrongVersion
elif [ "$JRE_VERSION_FOUND" -gt "$JRE_VERSION_SUPP" ]; then
   [ "$JRE_VERSION_SUPP_HIGHER" = "no" ] && JavaWrongVersion
elif [ "$JRE_RELEASE_FOUND" -gt "$JRE_RELEASE_SUPP" ]; then
   [ "$JRE_RELEASE_SUPP_HIGHER" = "no" ] && JavaWrongVersion
elif [ $(expr "$JRE_LEVEL_FOUND" \> "$JRE_LEVEL_SUPP") -eq 1 ]; then
   [ "$JRE_LEVEL_SUPP_HIGHER" = "no" ] && JavaWrongVersion
elif [ $(expr "$JRE_LEVEL_SUPP" \> "$JRE_LEVEL_FOUND") -eq 1 ]; then
   JavaWrongVersion
fi

### Set DSM_LOG to current dir ###
if [ -z "$DSM_LOG" ]
then
   DSM_LOG=$PWD 
   export DSM_LOG
fi

#=======================================================================
#=== Check the parameters					     ===
#=======================================================================

### Check if the program is present in the current directory
### first, otherwise go to the installation directory.
if [ ! -f ./dsm.jar ]
then
   if [ -f "${DSM_DIR}/dsm.jar" ]
   then
      cd $DSM_DIR

   elif [ -f "${DSMDIR1}/dsm.jar" ]
   then
      cd $DSMDIR1

   elif [ -f "${DSMDIR2}/dsm.jar" ]
   then
      cd $DSMDIR2
   fi
fi

if [ $# -ge 1 ]
then
   ### Parse command line arguments
   JAVA_XARGS=
      OPTIONS=
  TESTOPTIONS=
      for arg
      do
         case $arg in
            -X*) JAVA_XARGS="$arg"
                  ;;
             -*) OPTIONS="$OPTIONS$arg "
                  ;;
         runbvt) TESTOPTIONS="$TESTOPTIONS$arg "
                  ;;
      targetfs*) TESTOPTIONS="$TESTOPTIONS$arg "
                  ;;
              *) echo "Error: $arg is not an option"
                 exit 1
                  ;;
         esac
      done
fi

#=======================================================================
#=== Start the JavaGUI Application				     ===
#=======================================================================
java -DDSM_LANG=$LANG	-DDSM_CONFIG=$DSM_CONFIG -DDSM_DIR=$DSM_DIR \
     -DDSM_LOG=$DSM_LOG -DDSM_OPTIONS="$OPTIONS" -DDSM_ROOT=$PWD    \
     ${JAVA_XARGS} -jar dsm.jar $TESTOPTIONS

