#!/usr/bin/qsh
# // THIS PRODUCT CONTAINS RESTRICTED MATERIALS OF IBM
# // 5724-I63, 5724-H88, 5655-N01, 5733-W61, 5733-W70, 5733W80, 5733W85 (C) COPYRIGHT International Business Machines Corp., 1997,2011
# // 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_ENDORSED_DIRS}\
   ${JAVA_ORB_PROPERTIES}\
   -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}\
   ${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}"
}

#####################################################################################
#  This function is called from the rearg_match_arg_with_value() function below.    #
#  The rearg_match_arg_with_value() function was called when the script             #
#  ${WAS_HOME}/bin/.rearg was sourced below to handle input parameters.             #
#####################################################################################
specify_profile()
{
  WAS_OS400_INSTANCE=$1
  WAS_PROFILE_NAME=$1
  PROFILE_SPECIFIED=1
}

#####################################################################################
#  The rearg_match_arg_with_value() function was called when the script             #
#  ${WAS_HOME}/bin/.rearg was sourced below to handle input parameters.             #
#  This method handles '-XXXX value' pairs of input parameters.                     #
#####################################################################################
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
}

#####################################################################################
#  The rearg_match_boolean_arg() function was called when the script                #
#  ${WAS_HOME}/bin/.rearg was sourced below to handle input parameters.             #
#  This fuction handles '-XXXX' parameters without values as boolean switches.      #     
#####################################################################################
rearg_match_boolean_arg()
{
  return 0
}

setWASHome()
{
    if [ "${REPLACE_WAS_HOME:=}" != "" ] && [ -d ${REPLACE_WAS_HOME} ]
    then
        : #Do_nothing
    else
    
    WAS_HOME=${REPLACE_WAS_HOME}
        CUR_DIR=`pwd`
        WAS_DIR=`dirname ${0}`/../
        cd "${WAS_DIR}"
        WAS_HOME=`pwd`
        cd "${CUR_DIR}"
    fi
}
#####################################################################################
#  Paths and variables that vary by product, edition, and/or option                 #
#####################################################################################
WAS_ROOT=
if [ "${WAS_ROOT}" == "" ];then
	setWASHome
else
	WAS_HOME=${WAS_ROOT}
fi
. ${WAS_HOME}/properties/os400.properties
WAS_INSTALL_ROOT=${WAS_HOME}
WAS_PROD_LIB=QWAS9
WAS_LIB=${WAS_PRODUCT_LIB}
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                                            #
#####################################################################################
# JAVA_HOME=$(javahomepath)
. ${WAS_HOME}/bin/sdk/_setupSdk

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                                                           #
#####################################################################################
WAS_OS400_ENDORSED_DIRS=${WAS_OS400_ENDORSED_DIRS:=${WAS_INSTALL_ROOT}/java/endorsed}
WAS_OS400_ENDORSED_J9_DIRS=${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 JAVA_HOME
export PATH=${WAS_INSTALL_ROOT}/bin:${JAVA_HOME}/bin:/usr/bin:

#####################################################################################
#  Unset variables for Runtime Exec support during config actions                   #
#####################################################################################
UNSET_JAVA_PROPERTIES=false
if [ "${UNSET_JAVA_PROPERTIES}" = "true" ]; then
unset LIBPATH
unset PASE_LIBPATH
unset PASE_JAVA_HOME
fi

#####################################################################################
#  Java parameters                                                                  #
#####################################################################################
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"

#####################################################################################
#  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

#####################################################################################
#  The ${WAS_HOME}/bin/.rearg script is sourced here to process input parameters.   #
#  It will call two functions defined in this script:                               #
#    * rearg_match_boolean_arg()                                                    #
#        This fuction handles '-XXXX' parameters without values as boolean switches.#
#    * rearg_match_arg_with_value()                                                 #
#        This fuction handles '-XXXX value' parameters pairs.                       #     
#####################################################################################
. ${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" ]; 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 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. #
#####################################################################################
JAVA_ENDORSED_DIRS="-Djava.endorsed.dirs=${WAS_HOME}/endorsed_apis:${WAS_OS400_ENDORSED_J9_DIRS}"
LIBPATH="${JAVA_NATIVE_LIB_DIR}:${LIBPATH}"
export LIBPATH

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