#!/usr/bin/qsh
# // THIS PRODUCT CONTAINS RESTRICTED MATERIALS OF IBM
# // 5724-J08, 5724-I63, 5724-H88, 5724-H89, 5655-N02, 5733-W70 (C) COPYRIGHT International Business Machines Corp., 2009
# // All Rights Reserved * Licensed Materials - Property of IBM
# // US Government Users Restricted Rights - Use, duplication or disclosure
# // restricted by GSA ADP Schedule Contract with IBM Corp.
# //
# //  DESCRIPTION:
# //     Script file used to change the JVM used to run the EnableJvm.class.
# //     Either because the -runAsTargetJvm switch was passed, or if the enableJvm 
# //     script is being called in a post-IBM i 6.1 release and the controlling profile  
# //     is configured to use the Classic jvm which is no longer supported.
# //
############################################################################################

############################################################################################
# Variable used to change the JVM which will eventually be used to run the EnableJvm.class #
############################################################################################
JVM_CLASSIC_PATH=/QIBM/ProdData/Java400/jdk6
JVM_J9_32_PATH=/QOpenSys/QIBM/ProdData/JavaVM/jdk60/32bit
JVM_J9_64_PATH=/QOpenSys/QIBM/ProdData/JavaVM/jdk60/64bit
CLASSIC_SUPPORTED=true
STD64_SUPPORTED=true
SUPPORTED_JVM_ARGUMENTS='-jvm < classic | std32 | std64 >'
TARGET_JVM=${JAVA_HOME}
RUN_MODE=normal
RESET_JAVA_PATH=false
WAS_OS400_ENCODING=${WAS_OS400_ENCODING:=$(/QSYS.LIB/${WAS_LIB}.LIB/QWASSCRGLU.PGM "WAS_GETJAVAENC" "${WAS_INSTALL_ROOT}")}
WAS_OS400_MSGCAT_PATH=${WAS_OS400_MSGCAT_PATH:=$(/QSYS.LIB/${WAS_LIB}.LIB/QWASSCRGLU.PGM "WAS_GETCATPATH" "${WAS_INSTALL_ROOT}")}

######################################################################
# The following checks the IBM i release we are running in.  If this #
# is a release beyond IBM i6.1 the Classic JVM is no longer supported#
# and the CLASSIC_SUPPORTED varaiable that was initialized to 'true' #
# will be set to a value of 'false'. Used for later processing.      #
######################################################################
if [ "${QWAS_OS_VERSION}" = "" ] ; then 
   QWAS_OS_VERSION=${QSH_VERSION}
fi  

if [ "${QWAS_OS_VERSION:0:4}" = "V5R4" ] ; then
    SUPPORTED_JVM_ARGUMENTS='-jvm < classic | std32 >'
    CLASSIC_SUPPORTED=true
    STD64_SUPPORTED=false
elif [ "${QWAS_OS_VERSION:0:4}" = "V6R1" ] ; then     
    SUPPORTED_JVM_ARGUMENTS='-jvm < classic | std32 | std64 >'
    CLASSIC_SUPPORTED=true 
    STD64_SUPPORTED=true     
else # V7R1M0 or later does not support the classic jvm 
    SUPPORTED_JVM_ARGUMENTS='-jvm < std32 | std64 >'
    CLASSIC_SUPPORTED=false 
    STD64_SUPPORTED=true            
fi

###################################################################### 
#  Function to set java invocation variables                         #
######################################################################
set_java_paths()
{
  COMMON_JAVA_FLAGS="${JAVA_ENDORSED_DIRS}\
   ${JAVA_ORB_PROPERTIES}\
   ${OS400_USE_J9}\
   -Djava.version=1.6\
   -Dwas.standalone=true\
   -Dwas.install.library=${WAS_LIB}\
   -Dwas.install.root=${WAS_INSTALL_ROOT}\
   -Dchar.encoding=${WAS_OS400_ENCODING}\
   -Dwas.profile.registry=${WAS_USERDATA_ROOT}/profileRegistry\
   -Dwas.profile.registry.logs=${WAS_PROFILE_REGISTRY_ROOT}/logs\
   -Dwas.profile.registry.root=${WAS_PROFILE_REGISTRY_ROOT}\
   -Dcom.ibm.wsspi.bootstrap.script=$(basename $0)\
   ${JVM_CACHE_SETTINGS}\
   -Dwas.userdata.path=${WAS_USERDATA_ROOT}\
   -Xms16M\
   ${WAS_ADDL_JVM_ARGS}"

  JAVA_FLAGS="${COMMON_JAVA_FLAGS}\
   -Xbootclasspath/p:${WAS_BOOTCLASSPATH}\
   -Djava.ext.dirs=${JAVA_EXT_DIRS}\
   ${WAS_OS400_SECURITY_ENCODING}\
   ${WAS_OS400_VALIDATION_LIST}\
   ${WAS_OS400_SECURITY_DEBUG}"

  # NOTE: server.root is deprecated but still used
  JAVA_PARM="${JAVA_FLAGS}\
   ${OSGI_INSTALL}\
   ${OSGI_CFG}\
   -Dws.ext.dirs=${WAS_EXT_DIRS}\
   -Duser.install.root=${USER_INSTALL_ROOT}\
   -Dserver.root=${USER_INSTALL_ROOT}\
   -Dwas.cell.name=${WAS_CELL}\
   -Dwas.node.name=${WAS_NODE}\
   -Dinstance.name=${WAS_OS400_INSTANCE}\
   ${CLIENTSAS}\
   ${CLIENTSSL}\
   ${CLIENTSOAP}\
   -Dsecurity.overridePropertiesFile=true\
   -Djava.security.properties=${SECURITY_CFG}\
   -Djava.util.prefs.userRoot=${HOME}/.qwas_client\
   -classpath ${WAS_CLASSPATH}"
}

################################################################
# Function called if the -jvm argument value is invalid.       #
# Prints error & valid values and exits without change the jvm #
################################################################ 
dash_jvm_parm_error()
{  
   if [ "$1" = "classic" ]; then     
     dspmsg ${WAS_OS400_MSGCAT_PATH} 18 "CWNATV09E: The Java Developer Kit for i5/OS ('Classic JDK') is no longer supported.\n"
   else
     dspmsg ${WAS_OS400_MSGCAT_PATH} 19 "CWNATV10E: '-jvm' argument value of '$1' is not supported.\n" "$1"
   fi 
     dspmsg ${WAS_OS400_MSGCAT_PATH} 20 "CWNATV11E: Supported -jvm argument values for ${QWAS_OS_VERSION} are: $2.\n" ${QWAS_OS_VERSION} "$2"
   exit 1 
}

################################################################
# Function called if the -runAsTargetJvm option is passed on   #
# the call to the enableJvm script.                            #
################################################################ 
set_run_as()
{
RUN_MODE=runAs
}

################################################################
# Function called by the .rearg script for all single -options #
# as it parses the $@ parameter/options list that was passed.  #
################################################################ 
rearg_match_boolean_arg()
{
  local matched=1
  case $1 in
    -runAsTargetJvm)   set_run_as ;; 
    *)                 matched=0  ;;
  esac
  return $matched
}

################################################################
# Function called by the rearg_match_arg_with_value() below    #
# TARGET_JVM is set to the value passed with the -jvm parameter#
################################################################ 
set_target_jvm ()
{
  if [ "$(basename $1 | tr A-Z a-z)" = "std32" ]; then
     TARGET_JVM=std32
  elif [ "$(basename $1 | tr A-Z a-z)" = "std64" ]; then
     TARGET_JVM=std64
  elif [ "$(basename $1 | tr A-Z a-z)" = "classic" ]; then    
     TARGET_JVM=classic
  else 
     TARGET_JVM=$1
     dash_jvm_parm_error "${TARGET_JVM}" "${SUPPORTED_JVM_ARGUMENTS}"
  fi
}

################################################################
# Function called by the .rearg script for all parameter/value #
# pairs as it parses the $@ parameter list. Looking for -jvm   #
################################################################ 
rearg_match_arg_with_value()
{
  local matched=0
  case $1 in
    -jvm)  set_target_jvm $2 ;;
    *)     matched=0 ;;
  esac
  return $matched
}

######################################################################
# Source the utility script that is used to parse the parameter list #
# passed to this script.                                             #
# It will call the two functions (defined above ):                   #
#  1. rearg_match_arg_with_value()                                   #
#  2. rearg_match_boolean_arg()                                      #
# To allow this script to process its parameters/options             #                 
###################################################################### 
. ${WAS_HOME}/bin/.rearg

######################################################################
# The -runAsTargetJvm option was specified on the call so the        #
# EnableJvm.class will be run in a jvm that is specified by the      #
# -jvm parameter. Can be J9 in either 32 or 64 bit, or Classic.      #
######################################################################
if [ "${RUN_MODE}" = "runAs" ] ; then  
  RESET_JAVA_PATH=true
  if [ "${TARGET_JVM}" = "std32" ]; then
    INSTANCE_USE_J9=true
    JAVA_HOME=${JVM_J9_32_PATH}
  elif [ "${TARGET_JVM}" = "std64" ]; then
     if [ "${STD64_SUPPORTED}" = "false" ]; then 
        dash_jvm_parm_error "${TARGET_JVM}" "${SUPPORTED_JVM_ARGUMENTS}"
     else
         INSTANCE_USE_J9=true
         JAVA_HOME=${JVM_J9_64_PATH}
     fi 
  elif [ "${TARGET_JVM}" = "classic" ]; then
     if [ "${CLASSIC_SUPPORTED}" = "false" ]; then 
        dash_jvm_parm_error "${TARGET_JVM}" "${SUPPORTED_JVM_ARGUMENTS}"
     else
        INSTANCE_USE_J9=false
        JAVA_HOME=${JVM_CLASSIC_PATH}  
     fi 
  fi
######################################################################
# The following 'else' leg switches to use a J9 jvm even though the  #
# -runAsTargetJvm option is not passed.  This is because the profile #
# being used is configured to use the Classic jvm which is not       #
# supported in IBM i releases after IBM i6.1                         #
######################################################################
elif [ "${RUN_MODE}" = "normal" ]; then 
   if [ "${CLASSIC_SUPPORTED}" = "false" ]; then 
      if [ "${TARGET_JVM}" = "classic" ]; then 
        # -jvm classic post V6R1 is not supported - post error message and exit
        dash_jvm_parm_error "${TARGET_JVM}" "${SUPPORTED_JVM_ARGUMENTS}"  
      elif [ "${INSTANCE_USE_J9}" = "false" ]; then 
        # The environment is set to use Classic post V6R1. Switch to one of the J9s
        # Does not matter which is used
        RESET_JAVA_PATH=true
        INSTANCE_USE_J9=true
        if [ -d ${JVM_J9_32_PATH} ]; then
           JAVA_HOME=${JVM_J9_32_PATH}
        elif [ -d ${JVM_J9_64_PATH} ]; then
           JAVA_HOME=${JVM_J9_64_PATH}
        else 
           # No java product is currnently installed on the system ....
           # This is one of those 'should never happen' situations
           dspmsg ${WAS_OS400_MSGCAT_PATH} 18 "CWNATV09E: The Java Developer Kit for i5/OS ('Classic JDK') is no longer supported.\n"           
           dspmsg ${WAS_OS400_MSGCAT_PATH} 21 "CWNATV12E: To proceed, install either Java 2 Standard Edition (J2SE) 32-bit for IBM i or Java 2 Standard Edition (J2SE) 64-bit for IBM i.\n"                      
           exit 1
        fi
      fi
   fi
fi 

#####################################################################################
# If RESET_JAVA_PATHS is 'true' it means that JAVA_HOME and INSTANCE_USE_J9 have    #
# been changed because either the -runAsTargetJvm has been passed and/or the        #
# OS Level is one in which the Classic jvm is no longer supported and the profile   #
# used to run the EnableJvm.class is currently cofigured to use the Classic jvm.    #
# We need to rest the java classpaths from previous values set by setupCmdLine      #
#####################################################################################
if [ "${RESET_JAVA_PATH}" = "true" ]; then  
export JAVA_HOME

#####################################################################################
#  Properties for the JVM - classic vs. J9                                          #
#####################################################################################
WAS_OS400_ENDORSED_DIRS=${WAS_OS400_ENDORSED_DIRS:=${WAS_INSTALL_ROOT}/java/endorsed}
WAS_OS400_ENDORSED_J9_DIRS=${WAS_INSTALL_ROOT}/java/endorsedj9:${JAVA_HOME}/jre/lib/endorsed}
WAS_OS400_ORB_PROPERTIES=${WAS_OS400_ORB_PROPERTIES:=${WAS_INSTALL_ROOT}/properties/orb.properties}
WAS_OS400_FILE_CREATE_AUTH=${WAS_OS400_FILE_CREATE_AUTH:=RX}
WAS_OS400_DIR_CREATE_AUTH=${WAS_OS400_DIR_CREATE_AUTH:=none}

#####################################################################################
#  Variables to set up the Qshell environment from which the scripts execute        #
#####################################################################################
export QIBM_MULTI_THREADED=Y
export PATH=${WAS_INSTALL_ROOT}/bin:${JAVA_HOME}/bin:/usr/bin:

#####################################################################################
#  Java parameters                                                                  #
#####################################################################################
if [ "${INSTANCE_USE_J9}" = "false" ]; then
## We are using Classic iSeries JDK ##
WAS_CLASSPATH="\
${WAS_INSTALL_ROOT}/properties:\
${WAS_INSTALL_ROOT}/lib/startup.jar:\
${WAS_INSTALL_ROOT}/lib/bootstrap.jar:\
${WAS_INSTALL_ROOT}/java/tools/tools.jar:\
${WAS_INSTALL_ROOT}/lib/j2ee.jar:\
${WAS_INSTALL_ROOT}/lib/lmproxy.jar:\
${WAS_INSTALL_ROOT}/lib/urlprotocols.jar:\
${WAS_INSTALL_ROOT}/lib/mail-impl.jar:\
${WAS_INSTALL_ROOT}/lib/activation-impl.jar:\
/QIBM/ProdData/OS400/jt400/lib/jt400Native.jar"

WAS_EXT_DIRS="\
${WAS_INSTALL_ROOT}/java/tools:\
${WAS_INSTALL_ROOT}/classes:\
${WAS_INSTALL_ROOT}/lib:\
${WAS_INSTALL_ROOT}/installedChannels:\
${WAS_INSTALL_ROOT}/lib/ext:\
${WAS_INSTALL_ROOT}/web/help:\
${ITP_LOC}/plugins/com.ibm.etools.ejbdeploy/runtime"

else
## We are using J9 ##
WAS_CLASSPATH="\
${WAS_INSTALL_ROOT}/properties:\
${WAS_INSTALL_ROOT}/lib/startup.jar:\
${WAS_INSTALL_ROOT}/lib/bootstrap.jar:\
${JAVA_HOME}/lib/tools.jar:\
${WAS_INSTALL_ROOT}/lib/j2ee.jar:\
${WAS_INSTALL_ROOT}/lib/lmproxy.jar:\
${WAS_INSTALL_ROOT}/lib/urlprotocols.jar:\
${WAS_INSTALL_ROOT}/lib/mail-impl.jar:\
${WAS_INSTALL_ROOT}/lib/activation-impl.jar:\
/QIBM/ProdData/OS400/jt400/lib/jt400Native.jar"

WAS_EXT_DIRS="\
${JAVA_HOME}/lib:\
${WAS_INSTALL_ROOT}/classes:\
${WAS_INSTALL_ROOT}/lib:\
${WAS_INSTALL_ROOT}/installedChannels:\
${WAS_INSTALL_ROOT}/lib/ext:\
${WAS_INSTALL_ROOT}/web/help:\
${ITP_LOC}/plugins/com.ibm.etools.ejbdeploy/runtime"

fi
#####################################################################################
#  Set J9 vs. classic specific values after the profile's setupCmdLine has been     #
#  invoked in case just the profile is enabled to use specific JVM as opposed to the#
#  whole install                                                                    #
#####################################################################################
if [ "${INSTANCE_USE_J9}" = "false" ]; then
## We are using Classic iSeries JDK ##
  JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=${WAS_OS400_ENDORSED_DIRS}"
  JAVA_ORB_PROPERTIES="-Dos400.websphere.orb.properties=${WAS_OS400_ORB_PROPERTIES}"
  JVM_CACHE_SETTINGS="-Dos400.define.class.cache.file=${WAS_INSTALL_ROOT}/classes/cache/QDefineClassCache.jar\
   -Dos400.define.class.cache.maxpgms=40000\
   -Dos400.define.class.cache.hours=9999"
    OS400_USE_J9="-Dos400.use.j9=false\
   -Dos400.disable.static.programs=1\
   -Dibm.cds.suppresserrors=true"
else
## We are using J9 ##
  JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=${WAS_OS400_ENDORSED_J9_DIRS}"
  OS400_USE_J9="-Dos400.use.j9=true"
fi

#####################################################################################
#  Set the JAVA_EXT_DIRS based on the new target jvm                                #
#####################################################################################
if [ "${INSTANCE_USE_J9}" = "false" ]; then
## We are using Classic iSeries JDK 6.0 ##
JAVA_EXT_DIRS="\
${WAS_INSTALL_ROOT}/java/ext:\
${WAS_INSTALL_ROOT}/java/endorsed:\
${JAVA_HOME}/lib/ext:\
/QIBM/UserData/Java400/ext"
else
## We are using J9 ##
JAVA_EXT_DIRS="\
${WAS_INSTALL_ROOT}/java/extj9:\
${WAS_INSTALL_ROOT}/java/endorsedj9:\
${JAVA_HOME}/jre/lib/ext:\
/QIBM/UserData/Java400/ext"
fi

#####################################################################################
#  Set java paths variable  (Function defined near the top of this script)          #
#####################################################################################
set_java_paths

fi
