#!/usr/bin/qsh
#
# //  DESCRIPTION:
# //      Script file used to regenerate the Webserver
# //      plugin configuration file for a WebSphere
# //      Application Server instance.


#if they used -wasprofile, convert it to -profileName
PROFILE_PARAM=0
PROFILE_NAME=

specify_gen_profile()
{
   PROFILE_PARAM=1
   PROFILE_NAME=$1
}

rearg_match_arg_with_value()
{
  local matched=1
  case $1 in
    -wasprofile)  specify_gen_profile $2 ;;
    -profileName) specify_gen_profile $2 ;;
    *)            matched=0 ;;
  esac
  return $matched
}

rearg_match_boolean_arg()
{
  return 0
}


USAGE ()
{
echo
echo "==================================================================================="
echo "The GenPluginCfg.bat file has two possible ways to generate the plugin."
echo "1.  It can invoke the traditional way for the cell or specific web server.  "
echo "     This invocation will run the Generation within its own "
echo "     JVM and therefore can be prone to access problems to all jars."
echo " "
echo "      genplugincfg -cell.name BaseApplicationServerCell -node.name webserverNode"
echo "        -webserver.name webserverName"
echo
echo "2.  The new way will call a jython script that in turn will generate the plugin "
echo "     via wsadmin calls.  This new way ensures that the generation is running"
echo "     within the WebSphere JVM which will give full access to all jars.  "
echo " "
echo "      GenPluginCfg.sh -dmgr.root /opt/wasroot/profiles/dmgr -node.name node1 "
echo "       -cell.name ndcell -webserver.name webserver1 -userid user -password password" 
echo "       -debug yes -propagate yes    "
echo "==================================================================================="
exit 1
}


CHECK_FOR_JYTHON ()
{
# This checks if the required 4 arguments are passed so that the Jython scipt
# can be called instead of using the old way.
  ArgCount=0
  GETDR="NO"
  GETNN="NO"
  GETCN="NO"
  GETWN="NO"
  GETID="NO"
  GETPWD="NO"
  PROPCHOICE="NO"
  LOGCHIOICE="NO"
  GETPROP="NO"
  GETLOG="NO"
  GETPRF="NO"
  GETDSTR="NO"
  GETOUTFN="NO"
  GETSRVN="NO"
  GETCLSTN="NO"
  GETCFGR="NO"
  for arg in "$@"; 
  do
    if [[ $GETDR = 'YES' ]]; then
      DMGRROOT=$arg
      GETDR="NO"
      continue
    fi
    if [[ $GETNN = 'YES' ]]; then
      NODENAME=$arg
      GETNN="NO"
      continue
    fi
    if [[ $GETCN = 'YES' ]]; then
      CELLNAME=$arg
      GETCN="NO"
      continue
    fi
    if [[ $GETWN = 'YES' ]]; then
      WEBSERVERNAME=$arg
      GETWN="NO"
      continue
    fi
    if [[ $GETPROP = 'YES' ]]; then
      PROPCHOICE=$arg
      GETPROP="NO"
      continue
    fi
    if [[ $GETLOG = 'YES' ]]; then
      LOGCHOICE=$arg
      GETLOG="NO"
      continue
    fi
    if [[ $GETID = 'YES' ]]; then
      USERID=$arg
      GETID="NO"
      continue
    fi
    if [[ $GETPWD = 'YES' ]]; then
      PASSWD=$arg
      GETPWD="NO"
      continue
    fi
    if [[ $GETPRF = 'YES' ]]; then
      PRFNAME=$arg
      $GETPRF="NO"
      continue
    fi
    if [[ $GETDSTR = 'YES' ]]; then
      DESTROOT=$arg
      GETDSTR="NO"
      continue
    fi
    if [[ $GETOUTFN = 'YES' ]]; then
      OUTFNAME=$arg
      GETOUTFN="NO"
      continue
    fi
    if [[ $GETSRVN = 'YES' ]]; then
      SERVERNAME=$arg
      GETSRVN="NO"
      continue
    fi
    if [[ $GETCLSTN = 'YES' ]]; then
      CLUSTERNAME=$arg
      GETCLSTN="NO"
      continue
    fi
    if [[ $GETCFGR = 'YES' ]]; then
      CONFIGROOT=$arg
      GETCFGR="NO"
      continue
    fi
    if [ $arg = '-dmgr.root' ]; then
      let ArgCount+=1
      GETDR="YES"
      continue
    fi
    if [ $arg = '-node.name' ]; then
      let ArgCount+=1
      GETNN="YES"
      continue
    fi
    if [ $arg = '-cell.name' ]; then
      let ArgCount+=1
      GETCN="YES"
      continue
    fi
    if [ $arg = '-webserver.name' ]; then
      let ArgCount+=1
      GETWN="YES"
      continue
    fi
    if [ $arg = '-propagate' ]; then
      GETPROP="YES"
      continue
    fi
    if [ $arg = '-debug' ]; then
      GETLOG="YES"
      continue
    fi
    if [ $arg = '-userid' ]; then
      GETID="YES"
      continue
    fi
    if [ $arg = '-password' ]; then
      GETPWD="YES"
      continue
    fi
    if [ $arg = '-profileName' ]; then
      GETPRF="YES"
      continue
    fi
    if [ $arg = '-destination.root' ]; then
      GETDSTR="YES"
      continue
    fi
    if [ $arg = '-output.file.name' ]; then
      GETOUTFN="YES"
      continue
    fi
    if [ $arg = '-server.name' ]; then
      GETSRVN="YES"
      continue
    fi
    if [ $arg = '-cluster.name' ]; then
      GETCLSTN="YES"
      continue
    fi
    if [ $arg = '-config.root' ]; then
      GETCFGR="YES"
      continue
    fi

  done
}

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

if [ PROFILE_PARAM -eq 1 ]; then
    set -- "$@" -profileName $PROFILE_NAME
fi


CHECK_FOR_JYTHON "$@"

############################
# Set Old call argument list 
############################
CONT="NO"
for arg in "$@"; 
  do
    if [ $CONT = 'YES' ]; then
      CONT="NO"
      continue
    fi
    
    if [ "$arg" = '-help' ]; then
      # print out usage info
      USAGE
    fi
    
    if [ "$arg" = '-userid' ]; then
      # userid is not needed for the old call.  This will be removed from new list.
      CONT="YES"
      continue 
    fi
    
    if [ "${arg}" = '-password' ]; then
      # password is not needed for the old call.  This will be removed from new list.
      CONT="YES"
      continue 
    fi
    
    if [ "${arg}" = '-dmgr.root' ]; then
      # dmgr.root is not needed for the old call.  This will be removed from new list.
      CONT="YES"
      continue 
    fi
    
    if [[ $ARGLIST = '' ]]; then
      set -- "$arg"
      ARGLIST=SET
    else
      set -- "$@" "$arg"
    fi
done



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

# 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


if [[ $ArgCount == 4 ]]; then
  if [[ $LOGCHOICE = 'yes' ]]; then
    echo "*** Setting Log using:  ${WAS_HOME}/bin/wsadmin.sh -userid userid -password password -f ${WAS_HOME}/bin/httpPluginManagement.py -lang jython setDmgrPluginTrace"
    ${WAS_HOME}/bin/wsadmin -user $USERID -password $PASSWD -f ${WAS_HOME}/bin/httpPluginManagement.py -lang jython setDmgrPluginTrace
    echo "= = = = = = = = = = = = = = = "
  fi
  
   echo "*** Generating Plugin using: ${WAS_HOME}/bin/wsadmin -userid userid -password password -f ${WAS_HOME}/bin/httpPluginManagement.py -lang jython generatePlugin" $DMGRROOT $CELLNAME $NODENAME $WEBSERVERNAME
   ${WAS_HOME}/bin/wsadmin -user $USERID -password $PASSWD -f ${WAS_HOME}/bin/httpPluginManagement.py -lang jython generatePlugin $DMGRROOT $CELLNAME $NODENAME $WEBSERVERNAME
   echo "= = = = = = = = = = = = = = = "
  
  if [ $PROPCHOICE = 'yes' ]; then
    echo "*** Propagating Plugin using:  ${WAS_HOME}/bin/wsadmin -userid userid -password password -f ${WAS_HOME}/bin/httpPluginManagement.py -lang jython propagatePlugin " $DMGRROOT $CELLNAME $NODENAME $WEBSERVERNAME
    ${WAS_HOME}/bin/wsadmin -user $USERID -password $PASSWD -f ${WAS_HOME}/bin/httpPluginManagement.py -lang jython propagatePlugin $DMGRROOT $CELLNAME $NODENAME $WEBSERVERNAME
  fi
  
else
  echo "*** Initiating original genplugincfg call using args of:  " "$@"
  ${JAVA_HOME}/bin/java ${JAVA_PARM} \
    ${WAS_LOGGING} \
    com.ibm.ws.bootstrap.WSLauncher \
    com.ibm.websphere.plugincfg.generator.PluginConfigGenerator \
      ${USER_INSTALL_ROOT} ${CONFIG_ROOT} ${WAS_CELL} ${WAS_NODE} "$@"
fi
