#!/bin/ksh
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
# PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
#

###############################################
# Sourcing macros
DIRNAME=/usr/bin/dirname
. `$DIRNAME $0`/../lib/psmacros
###############################################

DEPLOY_TMP_DIR="$PS_VAR_DIR/tmp/deploy"

###############################################
# Get configuration from file
###############################################
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}

###############################################
# Main
###############################################

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"
if [ ! -f $STATE_FILE ]; then
  $ECHO "Cannot find $STATE_FILE."
  exit 1
fi

GrabConfig $STATE_FILE "JDK_DIR" "="
JAVA_HOME=$ANSWER

GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
DEPLOY_TYPE=$ANSWER

if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  DEPLOY_DOMAIN=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  DEPLOY_ADMIN=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  DEPLOY_ADMIN_HOST=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  DEPLOY_ADMIN_PORT=$ANSWER
elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  DEPLOY_DOMAIN=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PROTOCOL" "="
  DEPLOY_ADMIN_PROTOCOL=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  DEPLOY_ADMIN_HOST=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  DEPLOY_ADMIN_PORT=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  DEPLOY_ADMIN=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_PRODUCT_DIR" "="
  DEPLOY_PRODUCT_DIR=$ANSWER

elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_VIRTUAL_HOST" "="
  DEPLOY_VIRTUAL_HOST=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_NODE" "="
  DEPLOY_NODE=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_CELL" "="
  DEPLOY_CELL=$ANSWER
  
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  DEPLOY_ADMIN_HOST=$ANSWER

else
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER
fi

GrabConfig $STATE_FILE "DEPLOY_NOW" "="
DEPLOY_NOW=$ANSWER

GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
IDSAME_BASEDIR=$ANSWER

GrabConfig $STATE_FILE "BASEDIR" "="
PS_BASEDIR=$ANSWER

GrabConfig $STATE_FILE "SERVER_PROTOCOL" "="
SERVER_PROTOCOL=$ANSWER

GrabConfig $STATE_FILE "SERVER_HOST" "="
SERVER_HOST=$ANSWER

GrabConfig $STATE_FILE "SERVER_PORT" "="
SERVER_PORT=$ANSWER

if [ "$OSTYPE" = "SunOS" ]; then
  GrabConfig "/var/sadm/pkg/SUNWjss/pkginfo" "BASEDIR" "="
  JSS_BASEDIR=$ANSWER
else
  JSS_BASEDIR=""
fi

#
# LD_LIBRARY_PATH
#

# To workaround JDK JNI bug on Linux
if [ "$OSTYPE" = "Linux" ]; then
  EXTRA_LIBS="$JAVA_HOME/jre/lib/i386/native_threads"
else
  EXTRA_LIBS=""
fi

DIR="$JSS_BASEDIR$JSS_NSS_NSPR_LIB_DIR"
if [ -z "$LD_LIBRARY_PATH" ]; then
  LD_LIBRARY_PATH="$DIR:$EXTRA_LIBS"
else
  LD_LIBRARY_PATH="$DIR:$EXTRA_LIBS:$LD_LIBRARY_PATH"
fi
export LD_LIBRARY_PATH

#
# IS
#

IDSAME_LOCALE_DIR="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/locale"
IDSAME_LIB_DIR="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib"
IDSAME_JAR_DIR="$DEPLOY_DIR/bin/https/jar"
SERVLET_JAR="$IDSAME_LIB_DIR/servlet.jar"
JSS_JAR="$JSS_BASEDIR$JSS_JAR_LIB_DIR/jss3.jar"
JAAS_JAR="$IDSAME_LIB_DIR/jaas.jar"
SDK_JARS="$IDSAME_LIB_DIR/am_sdk.jar:$IDSAME_LIB_DIR/am_services.jar"
IDSAME_CLASSPATH="$IDSAME_LOCALE_DIR:$IDSAME_LIB_DIR:$IDSAME_CONFIG_DIR/config:$SDK_JARS:$JSS_JAR:$SERVLET_JAR:$JAAS_JAR"

PS_LIB_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/lib"

#
# JAXP
#

JAXP_JARS="$PS_LIB_DIR/jaxp-api.jar:$PS_LIB_DIR/xercesImpl.jar:$PS_LIB_DIR/xalan.jar:$PS_LIB_DIR/dom.jar:$PS_LIB_DIR/sax.jar"

#
# PS
#

PS_WEBINF_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/classes"
RW_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/rewriter.jar"
DT_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/desktop.jar"
PD_COMMON_JAR="$PS_LIB_DIR/portletcommon.jar"
PD_JDOM_JAR="$PS_LIB_DIR/jdom.jar"
PD_PCC_JAR="$PS_LIB_DIR/portletcontainercommon.jar"
PD_DEPLOYER_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/lib/deployer.jar"
PS_CLASSPATH="$PS_WEBINF_DIR:$RW_JAR:$PD_COMMON_JAR:$PD_JDOM_JAR:$PD_PCC_JAR:$DT_JAR:$PD_DEPLOYER_JAR"

#
# CLASSPATH
#

if [ -z "$CLASSPATH" ]; then
  CLASSPATH="$PS_CLASSPATH:$IDSAME_CLASSPATH:$JAXP_JARS:$JAVA_HOME/lib/lib.zip"
else
  CLASSPATH="$PS_CLASSPATH:$IDSAME_CLASSPATH:$JAXP_JARS:$JAVA_HOME/lib/lib.zip:$CLASSPATH"
fi

#
# Extract java system properties
#

set -A JAVA_SYSTEM_PROPERTIES
set -A ARGS
FOUND_INSTANCE="n"
for OPT in "$@"; do
  DOPT=${OPT#-D}
  if [ ${#DOPT} -gt 0 ] && [ "$DOPT" != "$OPT" ]; then
    JAVA_SYSTEM_PROPERTIES[${#JAVA_SYSTEM_PROPERTIES[*]}]="$OPT"
  else
    ARGS[${#ARGS[*]}]="$OPT"
    if [ "$OPT" = "-i" ] || [ "$OPT" = "--instance" ]; then
      FOUND_INSTANCE="y"
    elif [ "$FOUND_INSTANCE" = "y" ]; then
      DEPLOY_INSTANCE="$OPT"
      FOUND_INSTANCE="n"
    fi
  fi
done

#
# Call PS Portlet CLI
#

$JAVA_HOME/bin/java -Xms64m -Xmx256m -classpath $CLASSPATH -D"DDSchemaLocation=$PS_BASEDIR/$PS_PRODUCT_DIR/dtd" -D"com.iplanet.coreservices.configpath=$IDSAME_CONFIG_DIR/config/ums" -D"desktop.propertiesFile=$PS_CONFIG_DIR/desktop/desktopconfig.properties" -D"deployer.propertiesFile=$PS_CONFIG_DIR/portlet/PDConfig.properties" "${JAVA_SYSTEM_PROPERTIES[@]}" com.sun.portal.portlet.cli.PortletDeployerMain "${ARGS[@]}"
if [ $? -ne 0 ]; then
  exit $?
fi

#
# Deploy/Undeploy PS Portlet warfile
#

if [ "$1" = "deploy" ]; then
  shift
  while [ "$1" != "" ]; do
    if [ "$1" = "-wc_password" ] || [ "$1" = "-p" ]; then
      if [ "$2" != "" ] && [ `$ECHO $2 | $CUT -c1` != '-' ]; then
	DEPLOY_ADMIN_PASSWORD=$2      
	shift; shift
      fi
    else
      shift
    fi
  done

  WAR_FILE=`$BASENAME ${ARGS[${#ARGS[*]}-1]}`
  NAME=`$BASENAME $WAR_FILE .war`
  WAR_FILE="$PS_VAR_DIR/tmp/portlet/$WAR_FILE"

  if [ "$DEPLOY_NOW" = "y" ]; then
    $MKDIR -p $DEPLOY_TMP_DIR
    $ECHO "Deploying War File to Web Container..."

    if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
      eval $DEPLOY_DIR/bin/asadmin deploy -u "$DEPLOY_ADMIN" -w "$DEPLOY_ADMIN_PASSWORD" -p $DEPLOY_ADMIN_PORT -H $DEPLOY_ADMIN_HOST --instance $DEPLOY_INSTANCE --name $NAME --type web $WAR_FILE
      if [ $? -ne 0 ]; then
	exit $?
      fi
    elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
      eval $JAVA_HOME/bin/java -classpath $DEPLOY_PRODUCT_DIR/server/lib/weblogic.jar weblogic.Deployer -adminurl $DEPLOY_ADMIN_PROTOCOL://$DEPLOY_ADMIN_HOST:$DEPLOY_ADMIN_PORT -name $NAME -username $DEPLOY_ADMIN -password ${DEPLOY_ADMIN_PASSWORD} -deploy $WAR_FILE >> $PS_VAR_DIR/tmp/deploy/pdeploy.debug
      if [ $? -ne 0 ]; then
	exit $?
      fi
    elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
      $ECHO "\$AdminApp install $WAR_FILE {-contextroot "/$NAME" -usedefaultbindings -nopreCompileJSPs -distributeApp -nouseMetaDataFromBinary -node $DEPLOY_NODE -cell $DEPLOY_CELL -server $DEPLOY_INSTANCE -nodeployejb -appname ${NAME}_war -createMBeansForResources -noreloadEnabled -reloadInterval 0 -nodeployws}" > $DEPLOY_TMP_DIR/pdeploy.jacl
      $ECHO "\$AdminConfig save" >> $DEPLOY_TMP_DIR/pdeploy.jacl 
      eval $DEPLOY_DIR/bin/wsadmin.sh -f $DEPLOY_TMP_DIR/pdeploy.jacl

    elif [ "$DEPLOY_TYPE" = "IWS" ]; then
      IWS_SERVER_HOME="$DEPLOY_DIR"
      export IWS_SERVER_HOME

      DIR=`$ECHO "$PS_VAR_DIR/tmp/deploy/web-apps/https-$DEPLOY_INSTANCE/$NAME" | $SED -e "s.//./.g"`
      eval $IWS_SERVER_HOME/bin/https/httpadmin/bin/wdeploy deploy -u /$NAME -i $DEPLOY_INSTANCE -v https-$DEPLOY_INSTANCE -d $DIR $WAR_FILE
    fi
  fi
elif [ "$1" = "undeploy" ]; then
  shift
  while [ "$1" != "" ]; do
    if [ "$1" = "-wc_password" ] || [ "$1" = "-p" ]; then
      if [ "$2" != "" ] && [ `$ECHO $2 | $CUT -c1` != '-' ]; then
	DEPLOY_ADMIN_PASSWORD=$2      
	shift; shift
      fi
    else
      shift
    fi
  done

  NAME="${ARGS[${#ARGS[*]}-1]}"

  $ECHO "Undeploying War File from Web Container..."

  if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    eval $DEPLOY_DIR/bin/asadmin undeploy -u $DEPLOY_ADMIN -w $DEPLOY_ADMIN_PASSWORD -p $DEPLOY_ADMIN_PORT -H $DEPLOY_ADMIN_HOST --instance $DEPLOY_INSTANCE --type web $NAME
    if [ $? -ne 0 ]; then
      exit $?
    fi
  elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    eval $JAVA_HOME/bin/java -classpath $DEPLOY_PRODUCT_DIR/server/lib/weblogic.jar weblogic.Deployer -adminurl $DEPLOY_ADMIN_PROTOCOL://$DEPLOY_ADMIN_HOST:$DEPLOY_ADMIN_PORT -name $NAME -username $DEPLOY_ADMIN -password ${DEPLOY_ADMIN_PASSWORD} -undeploy >> $PS_VAR_DIR/tmp/deploy/pdeploy.debug
    if [ $? -ne 0 ]; then
      exit $?
    fi
  elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
    $ECHO "\$AdminApp uninstall ${NAME}_war {-node $DEPLOY_NODE -cell $DEPLOY_CELL -server $DEPLOY_INSTANCE}"  > $DEPLOY_TMP_DIR/pundeploy.jacl
    $ECHO "\$AdminConfig save" >> $DEPLOY_TMP_DIR/pundeploy.jacl

    # Execute it using the wsadmin
    eval $DEPLOY_DIR/bin/wsadmin.sh -f $DEPLOY_TMP_DIR/pundeploy.jacl 

  elif [ "$DEPLOY_TYPE" = "IWS" ]; then
    IWS_SERVER_HOME="$DEPLOY_DIR"
    export IWS_SERVER_HOME

    eval $IWS_SERVER_HOME/bin/https/httpadmin/bin/wdeploy delete -u /$NAME -i $DEPLOY_INSTANCE -v "https-$DEPLOY_INSTANCE" hard
    if [ $? -ne 0 ]; then
      exit $?
    fi
  fi
fi

# $RM -f $WAR_FILE

$ECHO "SUCCESS."

exit 0
