#!/usr/bin/qsh
#
# //  DESCRIPTION:
# //      Script file used to change the ports used by
# //      by an application server or to enable an application server
# //      to use embedded JMS.

. $(/usr/bin/dirname $0)/setupCmdLine

# Save the input parameters
INPUT_PARMS="$@"

################################################################
# Function called if -portblock option was passed an invalid   #
# value -- must be greater than 1023 and less than 65519       #
################################################################ 
portblock_value_error()
{     
  dspmsg ${WAS_OS400_MSGCAT_PATH} 22 "CWNATV13E: The specified -portblock value of $1 is not within the range of supported port numbers (1024 - 65518).\n" "$1"
  exit 1 
}
################################################################
# Function called to validate the -portblock option value.     #
# First 1023 ports are reserved, and the max value is 65535    #
################################################################ 
testRange ()
{
  if [ "$1" -lt 1024  ] || [ "$1" -gt 65518 ]; then
     portblock_value_error $1
  fi
}
################################################################
# Function called by the .rearg script for all parameter/value #
# pairs as it parses the $@ parameter list. Focus on -portblock#
################################################################ 
rearg_match_arg_with_value()
{
  local matched=0
  case $1 in
    -portblock) testRange $2 ;;
    *)     matched=0 ;;
  esac
  return $matched
}

# Issue error message if we are unable to determine valid profile
CMD_NAME=`basename $0`

if [ "${INV_PRF_SPECIFIED:=}" != "" ] && [ "$INV_PRF_SPECIFIED" = "true" ]
then
    ${JAVA_HOME}/bin/java -cp "${WAS_HOME}/lib/commandlineutils.jar" com.ibm.ws.install.commandline.utils.CommandLineUtils -specifiedProfileNotExists -profileName $WAS_PROFILE_NAME
    exit 1
elif [ "${WAS_USER_SCRIPT_FILE_NOT_EXISTS:=}" != "" ] && [ "$WAS_USER_SCRIPT_FILE_NOT_EXISTS" = "true" ]
then
    ${JAVA_HOME}/bin/java -cp "${WAS_HOME}/lib/commandlineutils.jar" com.ibm.ws.install.commandline.utils.CommandLineUtils -wasUserScriptFileNotExists -wasUserScript $WAS_USER_SCRIPT
    exit 1
elif [ "${NO_DFT_PRF_EXISTS:=}" != "" ] && [ "$NO_DFT_PRF_EXISTS" = "true" ]
then
    ${JAVA_HOME}/bin/java -cp "${WAS_HOME}/lib/commandlineutils.jar" com.ibm.ws.install.commandline.utils.CommandLineUtils -noDefaultProfile -commandName $CMD_NAME
    exit 1
fi

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

# Restore the input parameters
set -- ${INPUT_PARMS}

${JAVA_HOME}/bin/java ${JAVA_PARM} \
  com.ibm.wsspi.bootstrap.WSPreLauncher -nosplash -application \
  com.ibm.ws.bootstrap.WSLauncher \
  com.ibm.ws.admin.services.ChangeWASServer \
  -configroot ${CONFIG_ROOT} -cell ${WAS_CELL} -node ${WAS_NODE} "$@"
