# // THIS PRODUCT CONTAINS RESTRICTED MATERIALS OF IBM
# // 5724-I63, 5724-H88, 5655-N01, 5733-W61 (C) COPYRIGHT International Business Machines Corp., 1997,2006
# // 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 setup common variables for other scripts.
# //      It will call the instance specific setupCmdLine if there is a -instance
# //      parameter and the instance exists.

#####################################################################################
#  Functions                                                                        #
#####################################################################################
set_conglomerates()
{
  COMMON_JAVA_FLAGS="${JAVA_VERSION}\
   ${JAVA_ENDORSED_DIRS}\
   ${JAVA_ORB_PROPERTIES}\
   ${OS400_USE_J9}\
   ${IPV_ARGS}\
   -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}\
   -classpath ${WAS_CLASSPATH}"
}

specify_profile()
{
  WAS_OS400_INSTANCE=$1
  WAS_PROFILE_NAME=$1
  PROFILE_SPECIFIED=1
}

rearg_match_arg_with_value()
{
  local matched=1
  case $1 in
    -instance)    dspmsg ${WAS_OS400_MSGCAT_PATH} 12 "CWNATV00W: The '%1$s' parameter has been deprecated and replaced by the '%2$s' parameter.\n" "-instance" "-profileName"
                  specify_profile $2 ;;
    -profileName) specify_profile $2 ;;
    *)            matched=0 ;;
  esac
  return $matched
}

rearg_match_boolean_arg()
{
  return 0
}


#####################################################################################
#  Paths and variables that vary by product, edition, and/or option                 #
#####################################################################################
WAS_PROD_LIB=QWAS61
WAS_LIB=$(WAS_PRODUCT_LIB)
WAS_HOME=$(WAS_ROOT)
WAS_INSTALL_ROOT=${WAS_HOME}
WAS_USERDATA_ROOT=$(WAS_USERDATA)
export WAS_USERDATA_ROOT
WAS_SERVICE_ROOT=${WAS_USERDATA_ROOT}/service
WAS_LOGGING="-Djava.util.logging.manager=com.ibm.ws.bootstrap.WsLogManager -Djava.util.logging.configureByServer=true"

#####################################################################################
#  Global paths and miscellaneous values                                            #
#####################################################################################
INSTANCE_USE_J9=$(use.j9)
if [ "${INSTANCE_USE_J9}" = "true" ]; then
## We are using J9 ##
  JAVA_HOME=$(j9path)
  export JAVA_HOME
fi
PROFILE_REGISTRY_LOC=${WAS_USERDATA_ROOT}/profileRegistry/fsdb
REGISTRY_PARM="-registry ${WAS_USERDATA_ROOT}/profileRegistry/profileregistry.xml"
DEFAULT_PROFILE_SCRIPT=${PROFILE_REGISTRY_LOC}/_was_profile_default/default
WAS_PROFILE_REGISTRY_ROOT=${WAS_USERDATA_ROOT}/profileRegistry
ITP_LOC=${WAS_INSTALL_ROOT}/deploytool/itp
DERBY_HOME=${WAS_HOME}/derby
COMPUTERNAME=$(/usr/bin/hostname)
OSGI_INSTALL=-Dosgi.install.area=${WAS_HOME}
OSGI_CFG=-Dosgi.configuration.area=${WAS_HOME}/configuration
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}")}

#####################################################################################
#  Properties for the JVM - classic vs. J9                                          #
#####################################################################################
WAS_OS400_JAVA_VERSION=${WAS_OS400_JAVA_VERSION:=1.5}
WAS_OS400_ENDORSED_DIRS=${WAS_OS400_ENDORSED_DIRS:=${WAS_INSTALL_ROOT}/java/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

#####################################################################################
#  Java parameters                                                                  #
#####################################################################################
if [ "${INSTANCE_USE_J9}" = "false" ]; then
## We are using Classic iSeries JDK 5.0 ##
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:\
/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:\
/QIBM/ProdData/HTTP/Public/jt400/lib"
# jt400.jar is in /QIBM/ProdData/HTTP/Public/jt400/lib and jt400Native.jar is in the WAS_CLASSPATH.

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:\
/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:\
/QIBM/ProdData/HTTP/Public/jt400/lib"

fi

#####################################################################################
#  Determine profile/instance name and invoke profile-specific setupCmdLine or      #
#  WAS_USER_SCRIPT if specified.                                                    #
#####################################################################################
WAS_PROFILE_NAME=
WAS_OS400_INSTANCE=
WAS_PROFILE_FSDB_SCRIPT=
PROFILE_SPECIFIED=0

. ${WAS_HOME}/bin/.rearg

#Init INV_PRF_SPECIFIED, NO_DFT_PRF_EXISTS and WAS_USER_SCRIPT_FILE_NOT_EXISTS variables
NO_DFT_PRF_EXISTS=false
INV_PRF_SPECIFIED=false
WAS_USER_SCRIPT_FILE_NOT_EXISTS=false

if [ "${WAS_PROFILE_NAME:=}" != "" ]; then
    WAS_PROFILE_FSDB_SCRIPT=${PROFILE_REGISTRY_LOC}/${WAS_PROFILE_NAME}
    if [ ! -f ${WAS_PROFILE_FSDB_SCRIPT} ]; then
       INV_PRF_SPECIFIED=true
    fi
elif [ "${WAS_USER_SCRIPT:=}" = "" ] && [ "${USER_INSTALL_ROOT:=}" = "" ]; then
    WAS_PROFILE_FSDB_SCRIPT=${DEFAULT_PROFILE_SCRIPT}
    if [ ! -f ${WAS_PROFILE_FSDB_SCRIPT} ]; then
       NO_DFT_PRF_EXISTS=true
    fi
fi

if [ "${WAS_PROFILE_FSDB_SCRIPT:=}" != "" ] && [ -f ${WAS_PROFILE_FSDB_SCRIPT} ]; then
    . ${WAS_PROFILE_FSDB_SCRIPT}
elif [ ${PROFILE_SPECIFIED} -eq 1 ]; then
   if [ "$(basename $0 | tr A-Z a-z)" != "wasprofile" ] && [ "$(basename $0 | tr A-Z a-z)" != "manageprofiles" ] && [ "$(basename $0 | tr A-Z a-z)" != "enablejvm" ]; then
     #Do nothing
   else
     WAS_USER_SCRIPT=
   fi
fi

if [ "${WAS_USER_SCRIPT:=}" != "" ]; then
    if [ -f ${WAS_USER_SCRIPT} ]; then
        . ${WAS_USER_SCRIPT}
    elif [ "${INV_PRF_SPECIFIED:=}" == "false" ]; then
        WAS_USER_SCRIPT_FILE_NOT_EXISTS=true
    fi
fi

#####################################################################################
# If profile has not been specified set it now based on profile name set in
# profile's setupCmdLine. If it has been specified, make sure the case is correct.
#####################################################################################
if [ ${PROFILE_SPECIFIED} -eq 1 ]; then
    if [ "${WAS_PROFILE_NAME_FROM_SETUPCMDLINE:=}" != "" ]; then
        set -- "$@" -profileName ${WAS_PROFILE_NAME_FROM_SETUPCMDLINE}
    else
        set -- "$@" -profileName ${WAS_PROFILE_NAME}
    fi
fi
if [ "${WAS_PROFILE_NAME_FROM_SETUPCMDLINE:=}" != "" ]; then
    specify_profile ${WAS_PROFILE_NAME_FROM_SETUPCMDLINE}
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 5.0 ##
  JAVA_VERSION="-Djava.version=${WAS_OS400_JAVA_VERSION}"
  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"
else
## We are using J9 ##
  OS400_USE_J9="-Dos400.use.j9=$(use.j9)"
fi

if [ "${INSTANCE_USE_J9}" = "false" ]; then
## We are using Classic iSeries JDK 5.0 ##
  export PATH=${WAS_INSTALL_ROOT}/bin:/usr/bin:
else
## We are using J9 ##
  export PATH=${WAS_INSTALL_ROOT}/bin:${JAVA_HOME}/bin:/usr/bin:
fi

#####################################################################################
#  Java parameters                                                                  #
#  Note: CLASSPATH and WAS_EXT_DIRS are set above (prior to profile's setupCmdLine  #
#  being called) and then again in the profile's setupCmdLine. So profile handles   #
#  CLASSPATH and WAS_EXT_DIRS if necessary                                          #
#####################################################################################

if [ "${INSTANCE_USE_J9}" = "false" ]; then
## We are using Classic iSeries JDK 5.0 ##
JAVA_EXT_DIRS="\
${WAS_INSTALL_ROOT}/java/ext:\
${WAS_INSTALL_ROOT}/java/endorsed:\
/QIBM/ProdData/Java400/jdk15/lib/ext:\
/QIBM/UserData/Java400/ext"
else
## We are using J9 ##
JAVA_EXT_DIRS="\
${WAS_INSTALL_ROOT}/java/extj9:\
${JAVA_HOME}/jre/lib/ext:\
/QIBM/UserData/Java400/ext"
fi

#####################################################################################
#  Set IPv6 vs. IPv4 specific values after the profile's setupCmdLine has been      #
#  invoked                                                                          #
#####################################################################################
if [ "${INSTANCE_USE_IPV6}" = "true" ]; then
  ## We are using IPv6 ##
  IPV_ARGS="-Djava.net.preferIPv4Stack=false"
elif [ "${INSTANCE_USE_IPV6}" = "false" ]; then
  ## We are using IPv4 ##
  IPV_ARGS="-Djava.net.preferIPv4Stack=true"
else
  ## Unknown: let the JVM use its default value
  IPV_ARGS=""
fi

#####################################################################################
#  Set conglomerate variable                                                        #
#####################################################################################
set_conglomerates