#!/bin/ksh

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

###############################################
# Defines
###############################################

OMIT_CHAR='\c'
BELL_CHAR='\a'

ORIG_IFS=$IFS

LOG_DIR="$IDSAME_VAR_DIR/debug"
LOG_FILE="$LOG_DIR/deploy.log"

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

DEPLOY_TMP_DIR="$PS_VAR_DIR/tmp/deploy"

###############################################
# Usage
###############################################
Usage() {
  $ECHO "deploy"
  $ECHO ""
  $ECHO "  Command line options:"
  $ECHO "    help"
  $ECHO "  Command line options:"
  $ECHO "    deploy"
  $ECHO "    -deploy_admin_password <password>"
  $ECHO "    -uri <URI of war file>"
  $ECHO "    {-instance <instance>}"
  $ECHO "  Command line options:"
  $ECHO "    redeploy"
  $ECHO "    -deploy_admin_password <password>"
  $ECHO "    {-instance <instance>}"
}

###############################################
# 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//"`
}

###############################################
# Question
###############################################
Question() {
  local PROMPT=$1
  local DEFAULT=$2
  local VALID_CHARS=$3
  local DONE=""
  local CHECK=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    if [ "$DEFAULT" != "" ]; then
      $ECHO "$PROMPT [$DEFAULT] $OMIT_CHAR"
    else 
      $ECHO "$PROMPT $OMIT_CHAR"
    fi
    read ANSWER
    if [ "$ANSWER" = "" ]; then
      ANSWER=$DEFAULT
      DONE="y"
    else
      CHECK=`$ECHO $ANSWER | $GREP "[^$VALID_CHARS]"`
      if [ "$CHECK" = "" ]; then
        DONE="y"
      else
        $ECHO "Invalid answer! $BELL_CHAR"
      fi
    fi
  done
}

###############################################
# Get password
###############################################
GetPassword() {
  local PROMPT=$1
  local DONE=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    eval print "$PROMPT \$OMIT_CHAR"
    $STTY -echo
    read ANSWER
    $STTY echo
    $ECHO ""
    if [ "$ANSWER" != "" ]; then
      DONE="y"
    fi
  done
}

###############################################
# Replace line in file
###############################################
ReplaceLine() {
  local FILE=$1
  local MATCH=$2
  local TEXT=$3

  $CP $FILE $FILE-tmp
  $SED -e "/$MATCH/ {
c\\
$TEXT
}" $FILE-tmp > $FILE
  $RM $FILE-tmp
}

###############################################
# Set DEPLOY_INSTANCES
###############################################
SetDeployInstanceList() {
  local INSTANCE=""
  local INSTANCES=""
  local ADMIN_INSTANCE=""
  local TMP_INSTANCES=""

  if [ "$DEPLOY_INSTANCE" != "" ]; then
    DEPLOY_INSTANCES="$DEPLOY_INSTANCE"
  else
    if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
      ADMIN_INSTANCE="admin-server"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DOMAIN/* 2>&1`
      if [ $? -eq 0 ]; then
	for INSTANCE in $TMP_INSTANCES; do
	  if [ -z "$INSTANCES" ]; then
	    INSTANCES="$INSTANCE"
	  else
	    INSTANCES="$INSTANCE,$INSTANCES"
	  fi
	done
      fi
    elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
	GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
	INSTANCES=$ANSWER
      fi
    elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
	GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
	INSTANCES=$ANSWER
      fi
    elif [ "$DEPLOY_TYPE" = "IWS" ]; then
      ADMIN_INSTANCE="admserv"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DIR/https-* 2>&1`
      if [ $? -eq 0 ]; then
	for INSTANCE in $TMP_INSTANCES; do
      if [ -z "$INSTANCES" ]; then
        INSTANCES="$INSTANCE"
      else
        INSTANCES="$INSTANCE,$INSTANCES"
      fi
	done
      fi
    fi

    DEPLOY_INSTANCES=""
    IFS=","
    for INSTANCE in $INSTANCES; do
      INSTANCE=`$BASENAME $INSTANCE | $SED -e "s/https-//"`
      if [ "$INSTANCE" != "$ADMIN_INSTANCE" ]; then
        if [ -z "$DEPLOY_INSTANCES" ]; then
          DEPLOY_INSTANCES="$INSTANCE"
        else
          DEPLOY_INSTANCES="$INSTANCE,$DEPLOY_INSTANCES"
        fi
	  fi
    done
    IFS=$ORIG_IFS
  fi
}

###############################################
# Test for deploy
###############################################
TestDeploy() {
  local FILES=""

  FILES=`$LS $DEPLOY_SRC_DIR/WEB-INF/xml/* 2>&1`
  if [ $? -eq 0 ]; then
    if [ "$FILES" != "" ]; then
      return 0
    else
      return 1
    fi
  else
    return 1
  fi
}

###############################################
# Do war file
###############################################
DoWar() {
  local INSTANCE=$1
  local FILE=""
  local FILES=""
  local TMP_IFS=""
  local CURR_DIR=`pwd`
  
  cd $DEPLOY_SRC_DIR

  FILE="WEB-INF/xml/1.xml"
  $ECHO "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" > $FILE
  $ECHO "" >> $FILE
  $ECHO "<!DOCTYPE web-app PUBLIC" >> $FILE
  $ECHO "  \"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN\"" >> $FILE
  $ECHO "  \"file://$PS_CONFIG_DIR/dtd/web-app_2_3.dtd\">" >> $FILE
  $ECHO "" >> $FILE
  $ECHO "<web-app>" >> $FILE
  $ECHO "  <icon/>" >> $FILE
  $ECHO "  <display-name/>" >> $FILE
  $ECHO "  <description/>" >> $FILE
  $ECHO "  <distributable/>" >> $FILE
  $ECHO "  <context-param>" >> $FILE
  $ECHO "    <param-name>portal.base.dir</param-name>" >> $FILE
  $ECHO "    <param-value>$PS_BASEDIR</param-value>" >> $FILE
  $ECHO "  </context-param>" >> $FILE
  $ECHO "  <context-param>" >> $FILE
  $ECHO "    <param-name>portal.product.dir</param-name>" >> $FILE
  $ECHO "    <param-value>$PS_PRODUCT_DIR</param-value>" >> $FILE
  $ECHO "  </context-param>" >> $FILE
  $ECHO "  <context-param>" >> $FILE
  $ECHO "    <param-name>portal.config.dir</param-name>" >> $FILE
  $ECHO "    <param-value>$PS_CONFIG_DIR</param-value>" >> $FILE
  $ECHO "  </context-param>" >> $FILE
  $ECHO "  <context-param>" >> $FILE
  $ECHO "    <param-name>portal.data.dir</param-name>" >> $FILE
  $ECHO "    <param-value>$PS_VAR_DIR</param-value>" >> $FILE
  $ECHO "  </context-param>" >> $FILE
  $ECHO "  <context-param>" >> $FILE
  $ECHO "    <param-name>dynamicContext</param-name>" >> $FILE
  $ECHO "    <param-value>$DEPLOY_URI</param-value>" >> $FILE
  $ECHO "  </context-param>" >> $FILE
  $ECHO "  <context-param>" >> $FILE
  $ECHO "    <param-name>staticContext</param-name>" >> $FILE
  $ECHO "    <param-value>$DEPLOY_URI</param-value>" >> $FILE
  $ECHO "  </context-param>" >> $FILE
  $ECHO "  <context-param>" >> $FILE
  $ECHO "    <param-name>desktop.propertiesFile</param-name>" >> $FILE
  $ECHO "    <param-value>$PS_CONFIG_DIR/desktop/desktopconfig.properties</param-value>" >> $FILE
  $ECHO "  </context-param>" >> $FILE
  $ECHO "  <filter/>" >> $FILE
  $ECHO "  <filter-mapping/>" >> $FILE
  $ECHO "  <listener/>" >> $FILE
  $ECHO "  <servlet/>" >> $FILE
  $ECHO "  <servlet-mapping/>" >> $FILE
  $ECHO "  <session-config/>" >> $FILE
  $ECHO "  <mime-mapping/>" >> $FILE
  $ECHO "  <welcome-file-list/>" >> $FILE
  $ECHO "  <error-page/>" >> $FILE
  $ECHO "  <taglib/>" >> $FILE
  $ECHO "  <resource-env-ref/>" >> $FILE
  $ECHO "  <resource-ref/>" >> $FILE
  $ECHO "  <security-constraint/>" >> $FILE
  $ECHO "  <login-config/>" >> $FILE
  $ECHO "  <security-role/>" >> $FILE
  $ECHO "  <env-entry/>" >> $FILE
  $ECHO "  <ejb-ref/>" >> $FILE
  $ECHO "  <ejb-local-ref/>" >> $FILE
  $ECHO "</web-app>" >> $FILE

  FILE="WEB-INF/xml/search.xml"
  if [ -f $FILE-orig ]; then
    $SED -e "s#%DEPLOY_INSTANCE%#$INSTANCE#g" \
	 -e "s#%DYNAMIC_URI%#$DEPLOY_URI#g" \
	 $FILE-orig > $FILE
  fi

  TMP_IFS=$IFS
  IFS=$ORIG_IFS

  #Replacing the web-app xml tokens
  FILES=`$LS $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/*.xml 2>&1`
  for FILE in $FILES; do
    $CP -p $FILE $FILE-tmp
    $SED -e "s#%PS_CONFIG_DIR%#$PS_CONFIG_DIR#g" \
         -e "s#%PS_VAR_DIR%#$PS_VAR_DIR#g" \
         -e "s#%IDSAME_VAR_DIR%#$IDSAME_VAR_DIR#g" $FILE-tmp > $FILE
    $RM -f $FILE-tmp
  done

  FILES=`$LS $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/*.xml 2>&1`
  if [ $? -eq 0 ]; then
    IDSAME_LIB_DIR="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib"
    eval $JDK_DIR/bin/java -classpath $IDSAME_LIB_DIR/jaxp-api.jar:$IDSAME_LIB_DIR/xercesImpl.jar:$IDSAME_LIB_DIR/xalan.jar:$IDSAME_LIB_DIR/dom.jar:$IDSAME_LIB_DIR/sax.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/webxmlmerge.jar com.sun.portal.webxmlmerge.WebxmlMerge $FILES > WEB-INF/web.xml
  fi
  IFS=$TMP_IFS

  $RM -f WEB-INF/xml/1.xml
  $RM -f WEB-INF/xml/search.xml
  $RM -rf $DEPLOY_TMP_DIR/xml

  if [ "$DEPLOY_TYPE" = "IWS" ] || [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    FILE="WEB-INF/sun-web.xml"
    if [ ! -f $FILE ]; then
      $ECHO "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > $FILE
      $ECHO "<!DOCTYPE sun-web-app PUBLIC" >> $FILE
      $ECHO "  \"-//Sun Microsystems Inc.//DTD Sun ONE Application Server 7.0 Servlet 2.3//EN\"" >> $FILE
      $ECHO "  \"file:$PS_CONFIG_DIR/dtd/sun-web-app_2_3-0.dtd\">" >> $FILE
      $ECHO "" >> $FILE
      $ECHO "<sun-web-app>" >> $FILE
      $ECHO "  <locale-charset-info>" >> $FILE
      $ECHO "    <parameter-encoding form-hint-field=\"gx_charset\"/>" >> $FILE
      $ECHO "  </locale-charset-info>" >> $FILE
      $ECHO "  <session-config>" >> $FILE
      $ECHO "    <cookie-properties>" >> $FILE
      $ECHO "       <property name=\"cookiePath\" value=\"/\"/>" >> $FILE
      $ECHO "    </cookie-properties>" >> $FILE
      $ECHO "  </session-config>" >> $FILE
      $ECHO "</sun-web-app>" >> $FILE
    fi
  fi

  ##################################################################################
  # Virtual directory Mapping in Weblogic requires the creation of the weblogic.xml
  ##################################################################################
                                                                                                                                     
 if [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    FILE="WEB-INF/weblogic.xml"
    IDSAME_HELP_DIR="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/public_html"
    if [ ! -f $FILE ]; then
      $ECHO "<!DOCTYPE weblogic-web-app PUBLIC" > $FILE
      $ECHO "  \"-//BEA Systems, Inc.//DTD Web Application 8.1//EN\"" >> $FILE
      $ECHO "  \"http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd\">" >> $FILE
      $ECHO "" >> $FILE
      $ECHO "<weblogic-web-app>" >> $FILE
      $ECHO "  <virtual-directory-mapping>" >> $FILE
      $ECHO "    <local-path>$IDSAME_HELP_DIR</local-path>" >> $FILE
      $ECHO "    <url-pattern>/online_help/*</url-pattern>" >> $FILE
      $ECHO "  </virtual-directory-mapping>" >> $FILE
      $ECHO "</weblogic-web-app>" >> $FILE
    fi
  fi


  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/export/deploy.import"
  if [ -f $FILE ]; then
    $NAWK 'BEGIN {
	     printf("#!/bin/ksh\n");
	     printf("\n");
	   };
	   (length($0) > 0) {
	     webSrcDir = psBasedir"/"psProddir"/web-src";

	     from = $1;
	     gsub(/%IS_BASEDIR%/, isBasedir, from);
	     gsub(/%IS_PROD_DIR%/, isProddir, from);
	     gsub(/%IS_SHARELIB_DIR%/, isShareLibdir, from);
	     gsub(/%PS_BASEDIR%/, psBasedir, from);
	     gsub(/%PS_PROD_DIR%/, psProddir, from);
	     gsub(/%KTSEARCH_LIB_DIR%/, ktSearchdir, from);
	     gsub(/%IM_LIB_DIR%/, imLibDir, from);
	     gsub(/%WEB_SRC_DIR%/, webSrcDir, from);

	     to = $2;
	     gsub(/%WEB_SRC_DIR%/, webSrcDir, to);

	     printf("if [ -d %s ]; then\n", from);
	     if (index(osType, "Linux") != 0) {
	       printf("  /bin/rm -rf %s\n", to);
	       printf("  /bin/mkdir -p `/usr/bin/dirname %s`\n", to);
	       printf("  /bin/cp -r %s %s\n", from, to);
	     } else {
	       printf("  /usr/bin/rm -rf %s\n", to);
	       printf("  /usr/bin/mkdir -p `/usr/bin/dirname %s`\n", to);
	       printf("  /usr/bin/cp -r %s %s\n", from, to);
	     }
	     printf("elif [ -f %s ]; then\n", from);
	     if (index(osType, "Linux") != 0) {
  	       printf("  /bin/rm -f %s\n", to);
	       printf("  /bin/mkdir -p `/usr/bin/dirname %s`\n", to);
	       printf("  /bin/cp %s %s\n", from, to);
	     } else {
	       printf("  /usr/bin/rm -f %s\n", to);
	       printf("  /usr/bin/mkdir -p `/usr/bin/dirname %s`\n", to);
	       printf("  /usr/bin/cp %s %s\n", from, to);
	     }
	     printf("fi\n");
	     printf("\n");
	   };
	   END {
	     printf("exit 0\n");
	   };' isBasedir=$IDSAME_BASEDIR isProddir=$IDSAME_PRODUCT_DIR isShareLibdir=$IDSAME_SHARELIB_DIR psBasedir=$PS_BASEDIR psProddir=$PS_PRODUCT_DIR ktSearchdir=$KTSEARCH_LIB_DIR imLibDir=$IM_LIB_DIR osType=$OSTYPE $FILE > $DEPLOY_TMP_DIR/deploy_importer
    $CHMOD 777 $DEPLOY_TMP_DIR/deploy_importer
    ( unset ENV; $DEPLOY_TMP_DIR/deploy_importer )
  fi

  $ECHO "#!/bin/ksh" > $DEPLOY_TMP_DIR/scrub_properties
  PROPERTIES=`$LS WEB-INF/classes/*.properties 2>&1`
  if [ $? -eq 0 ]; then
    TMP_IFS=$IFS
    IFS=$ORIG_IFS
    for PROPERTY in $PROPERTIES; do
      TMP_STR1=${PROPERTY%.properties}
      TMP_STR2=${TMP_STR1##*_}
      if [ ${#TMP_STR2} -ne 2 ]; then
        PATHNAME=`$DIRNAME $PROPERTY`
        FILENAME1=`$BASENAME $PROPERTY .properties`
        FILENAME2=$FILENAME1"_en"
        $CP $PATHNAME/$FILENAME1.properties $PATHNAME/$FILENAME2.properties
        $ECHO "$RM -f $PATHNAME/$FILENAME2.properties" >> $DEPLOY_TMP_DIR/scrub_properties
      fi
    done
    IFS=$TMP_IFS
  fi

  WAR_FILE="$DEPLOY_TMP_DIR/$DEPLOY_URI.war"
  WAR_FILE=`$ECHO $WAR_FILE | $SED -e "s.//./.g"`

  $JDK_DIR/bin/jar cf $WAR_FILE * >> $LOG_FILE

  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/export/deploy.import"
  if [ -f $FILE ]; then
    $NAWK 'BEGIN {
	     printf("#!/bin/ksh\n");
	     printf("\n");
	   };
	   (length($0) > 0) {
	     webSrcDir = psBasedir"/"psProddir"/web-src";

	     to = $2;
	     gsub(/%WEB_SRC_DIR%/, webSrcDir, to);

	     if (index(osType, "Linux") != 0) {
  	       printf("/bin/rm -rf %s\n", to);
	     } else {
	       printf("/usr/bin/rm -rf %s\n", to);
	     }
	     printf("\n");
	   };
	   END {
	     printf("exit 0\n");
	   };' psBasedir=$PS_BASEDIR psProddir=$PS_PRODUCT_DIR osType=$OSTYPE $FILE > $DEPLOY_TMP_DIR/deploy_import_remover
    $CHMOD 777 $DEPLOY_TMP_DIR/deploy_import_remover
    ( unset ENV; $DEPLOY_TMP_DIR/deploy_import_remover )
  fi
  cd $CURR_DIR
}

#####################################################################
# Return ANSWER="y" if IS-SDK is already configured, "n" if not.
#####################################################################
CheckISConfigured() {
  local FILE=""

  ANSWER="y"

  if [ "$DEPLOY_TYPE" = "IWS" ]; then
    FILE="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/config/server.xml"
  elif [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    FILE="$DEPLOY_DOMAIN/$DEPLOY_INSTANCE/config/server.xml"
  elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    FILE="$DEPLOY_DIR/user_projects/domains/$DEPLOY_DOMAIN/startWebLogic.sh"
  elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
    FILE=$DEPLOY_DIR/config/cells/$DEPLOY_CELL/nodes/$DEPLOY_NODE/servers/$DEPLOY_INSTANCE/server.xml
  fi

  $GREP "am_sdk.jar" $FILE > /dev/null
  if [ $? -ne 0 ]; then
    ANSWER="n"
  fi
}

###############################################
# Do deploy
###############################################
DoDeploy() {
  local APP=""
  local DIR=""
  local FILE=""
  local PORT=""
  local PORTS=""
  local URI=""

  IFS=","

  for DEPLOY_INSTANCE in $DEPLOY_INSTANCES; do
    CheckISConfigured
    if [ "$ANSWER" = "n" ]; then
      $ECHO "Error: $DEPLOY_INSTANCE is not configured. Run wcconfig on this instance first."
    else
      $ECHO "Deploying to instance \"$DEPLOY_INSTANCE\"..."

      DoWar "$DEPLOY_INSTANCE"

      if [ "$DEPLOY_NOW" = "y" ]; then
	if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
	  PARAMS="deploy -u $DEPLOY_ADMIN -w $DEPLOY_ADMIN_PASSWORD -p $DEPLOY_ADMIN_PORT -H $DEPLOY_ADMIN_HOST --instance $DEPLOY_INSTANCE --name portal --contextroot $DEPLOY_URI --type web $WAR_FILE"
	  $ECHO "Calling $DEPLOY $PARAMS" | $SED -e "s/-w $DEPLOY_ADMIN_PASSWORD/-w ********/" >> $LOG_FILE
	  eval $DEPLOY $PARAMS >> $LOG_FILE 2>&1
	  if [ $? -ne 0 ]; then
	    $ECHO "Error: See $LOG_FILE. $BELL_CHAR"
	    exit 1
	  fi
	elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
	  URI=`$ECHO $DEPLOY_URI | $SED -e "s.^/.."`
	  if [ "$MODE" = "deploy" ]; then
	    PARAMS="-classpath $DEPLOY_PRODUCT_DIR/server/lib/weblogic.jar weblogic.Deployer -adminurl $DEPLOY_ADMIN_PROTOCOL://$DEPLOY_ADMIN_HOST:$DEPLOY_ADMIN_PORT -name $URI -username $DEPLOY_ADMIN -password ${DEPLOY_ADMIN_PASSWORD} -deploy $WAR_FILE"
	  else
	    PARAMS="-classpath $DEPLOY_PRODUCT_DIR/server/lib/weblogic.jar weblogic.Deployer -adminurl $DEPLOY_ADMIN_PROTOCOL://$DEPLOY_ADMIN_HOST:$DEPLOY_ADMIN_PORT -name $URI -username $DEPLOY_ADMIN -password ${DEPLOY_ADMIN_PASSWORD} -redeploy"
	  fi
	  $ECHO "Calling $JDK_DIR/bin/java $PARAMS" | $SED -e "s/ $DEPLOY_ADMIN_PASSWORD / ******** /" >> $LOG_FILE
	  eval $JDK_DIR/bin/java $PARAMS >> $LOG_FILE 2>&1
	  if [ $? -ne 0 ]; then
	    $ECHO "Error: See $LOG_FILE. $BELL_CHAR"
	    exit 1
	  fi
	elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
	  # Generate the deploy.jacl file
	  $ECHO "\$AdminApp install $WAR_FILE {-contextroot $DEPLOY_URI -usedefaultbindings -nopreCompileJSPs -distributeApp -nouseMetaDataFromBinary -node $DEPLOY_NODE -cell $DEPLOY_CELL -server $DEPLOY_INSTANCE -nodeployejb -appname portal_war -createMBeansForResources -noreloadEnabled -reloadInterval 0 -nodeployws} " > $DEPLOY_TMP_DIR/deploy.jacl
	  $ECHO "\$AdminConfig save" >> $DEPLOY_TMP_DIR/deploy.jacl

	  # Execute it using the wsadmin
	  eval $DEPLOY_DIR/bin/wsadmin.sh -f $DEPLOY_TMP_DIR/deploy.jacl >> $LOG_FILE 2>&1
	elif [ "$DEPLOY_TYPE" = "IWS" ]; then
	  DIR=`$ECHO "$PS_VAR_DIR/https-$DEPLOY_INSTANCE/$DEPLOY_URI/web-apps" | $SED -e "s.//./.g"`
	  PARAMS="deploy -u $DEPLOY_URI -i $DEPLOY_INSTANCE -v https-$DEPLOY_INSTANCE -d $DIR $WAR_FILE"
	  $ECHO "Calling $DEPLOY $PARAMS" >> $LOG_FILE
	  eval $DEPLOY $PARAMS >> $LOG_FILE 2>&1
	fi
      fi
    fi
  done
  IFS=$ORIG_IFS
}

###############################################
# Update JSP compiler classpath
###############################################
UpdateJSPCompilerClasspath() {
  local DIR=""
  local FILE=""
  local FILES=""
  local COMPILER_CLASSPATH=""

  DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib"
  if [ -d $DIR ]; then
    COMPILER_CLASSPATH="$PS_CONFIG_DIR/desktop/default/tld:$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/classes"

    FILES=`$LS $DIR/*.jar`
    for FILE in $FILES; do
      COMPILER_CLASSPATH="$COMPILER_CLASSPATH:$FILE"
    done

    FILE="$PS_CONFIG_DIR/desktop/desktopconfig.properties"
    if [ -f $FILE ]; then
      ReplaceLine $FILE "jspCompilerWARClassPath=" "jspCompilerWARClassPath=$COMPILER_CLASSPATH"
    fi
  fi
}

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

if [ "$1" = "help" ]; then
  Usage
  exit 0
fi

if [ `$ID | $NAWK '{print $1}'` != "uid=0(root)" ]; then
  $ECHO "You must be root user. $BELL_CHAR"
  exit 1
fi

if [ ! -f $STATE_FILE ]; then
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  exit 1
fi

GrabConfig $STATE_FILE "JDK_DIR" "="
if [ "$ANSWER" != "" ]; then
  JDK_DIR=$ANSWER
else
  $ECHO "Error: Cannot determine JDK_DIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_TYPE=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
  exit 1
fi
if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_HOST. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_PORT. $BELL_CHAR"
    exit 1
  fi
elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PROTOCOL" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_PROTOCOL=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_PROTOCOL. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_HOST. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_PORT. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_PRODUCT_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_PRODUCT_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_PRODUCT_DIR. $BELL_CHAR"
    DEPLOY_PRODUCT_DIR=$DEPLOY_DIR/weblogic81
  fi
  GrabConfig $STATE_FILE "DEPLOY_PROJECT_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_PROJECT_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_PROJECT_DIR. $BELL_CHAR"
    DEPLOY_PROJECT_DIR="user_projects"
  fi
  GrabConfig $STATE_FILE "DEPLOY_JDK_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_JDK_DIR=$ANSWER
  else
    # If the JES doesn't dump the correct value for DEPLOY_JDK_DIR, set
    # it correctly.
    if [ -d "$DEPLOY_DIR/jdk141_03" ]; then
      DEPLOY_JDK_DIR=$DEPLOY_DIR/jdk141_03
    else
      DEPLOY_JDK_DIR=$DEPLOY_DIR/jdk141_05
    fi
  fi
elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_VIRTUAL_HOST" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_VIRTUAL_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_VIRTUAL_HOST. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_NODE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_NODE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_NODE. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_CELL" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_CELL=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_CELL. $BELL_CHAR"
    DEPLOY_CELL=$DEPLOY_NODE
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_HOST. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_JDK_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_JDK_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_JDK_DIR. $BELL_CHAR"
    DEPLOY_JDK_DIR=$DEPLOY_DIR/java 
  fi
else
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
fi
GrabConfig $STATE_FILE "DEPLOY_NOW" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_NOW=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_NOW. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_PROTOCOL" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PROTOCOL=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_PROTOCOL. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_HOST" "="
if [ "$ANSWER" != "" ]; then
  SERVER_HOST=$ANSWER
  SERVER_HOSTNAME=${SERVER_HOST%%.*}
else
  $ECHO "Error: Cannot determine SERVER_HOST. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_PORT" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_PORT. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "PS_PROTOCOL" "="
if [ "$ANSWER" != "" ]; then
  PS_PROTOCOL=$ANSWER
else
  $ECHO "Error: Cannot determine PS_PROTOCOL. $BELL_CHAR"
  PS_PROTOCOL=$SERVER_PROTOCOL
fi
GrabConfig $STATE_FILE "PS_HOST" "="
if [ "$ANSWER" != "" ]; then
  PS_HOST=$ANSWER
else
  $ECHO "Error: Cannot determine PS_HOST. $BELL_CHAR"
  PS_HOST=$SERVER_HOST
fi
GrabConfig $STATE_FILE "PS_PORT" "="
if [ "$ANSWER" != "" ]; then
  PS_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine PS_PORT. $BELL_CHAR"
  PS_PORT=$SERVER_PORT
fi

DEPLOY_SRC_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src"
if [ ! -d $DEPLOY_SRC_DIR ]; then
  $ECHO "Error: $DEPLOY_SRC_DIR does not exist. $BELL_CHAR"
  exit 1
fi

if [ "$1" = "redeploy" ]; then
  MODE=$1

  DEPLOY_INSTANCE=""
  shift
  while [ "$1" != "" ]; do
    if [ "$1" = "-instance" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -instance. $BELL_CHAR"
	Usage
	exit 1
      else
	DEPLOY_INSTANCE=$2
	shift; shift
      fi
    elif [ "$1" = "-deploy_admin_password" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -deploy_admin_password. $BELL_CHAR"
	Usage
	exit 1
      else
	DEPLOY_ADMIN_PASSWORD=$2
	shift; shift
      fi
    else
      $ECHO "Error: Unknown parameter $1. $BELL_CHAR"
      Usage
      exit 1
    fi
  done

  SetDeployInstanceList

  if [ "$DEPLOY_TYPE" = "SUNONE" ] || [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    if [ "$DEPLOY_ADMIN_PASSWORD" = "" ]; then
      $ECHO "Error: DEPLOY_ADMIN_PASSWORD is not set. $BELL_CHAR"
      exit 1
    fi
  fi

  GrabConfig $STATE_FILE "DEPLOY_URI" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_URI=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_URI. $BELL_CHAR"
    exit 1
  fi
elif [ "$1" = "deploy" ]; then
  MODE=$1

  DEPLOY_INSTANCE=""
  shift
  while [ "$1" != "" ]; do
    if [ "$1" = "-instance" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -instance. $BELL_CHAR"
	Usage
	exit 1
      else
	DEPLOY_INSTANCE=$2
	shift; shift
      fi
    elif [ "$1" = "-uri" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -uri. $BELL_CHAR"
	Usage
	exit 1
      else
	DEPLOY_URI=$2
	shift; shift
      fi
    elif [ "$1" = "-deploy_admin_password" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -deploy_admin_password. $BELL_CHAR"
	Usage
	exit 1
      else
	DEPLOY_ADMIN_PASSWORD=$2
	shift; shift
      fi
    else
      $ECHO "Error: Unknown parameter $1. $BELL_CHAR"
      Usage
      exit 1
    fi
  done

  SetDeployInstanceList

  if [ "$DEPLOY_TYPE" = "SUNONE" ] || [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    if [ "$DEPLOY_ADMIN_PASSWORD" = "" ]; then
      $ECHO "Error: DEPLOY_ADMIN_PASSWORD is not set. $BELL_CHAR"
      exit 1
    fi
  fi

  if [ "$DEPLOY_URI" = "" ]; then
    $ECHO "Error: DEPLOY_URI is not set. $BELL_CHAR"
    exit 1
  fi
else
  MODE="deploy"

  DEPLOY_URI="/portal"
  GrabConfig $STATE_FILE "DEPLOY_URI" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_URI=$ANSWER
  fi

  Question "Deploy into which instance [Leave blank for all]?" "" "a-zA-Z0-9 /\._-"
  DEPLOY_INSTANCE=$ANSWER

  Question "What is the deployment URI?" $DEPLOY_URI "a-zA-Z0-9/\._-"
  DEPLOY_URI=$ANSWER

  if [ "$DEPLOY_TYPE" = "SUNONE" ] || [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    GetPassword "What is the Application Server administration password?"
    DEPLOY_ADMIN_PASSWORD=$ANSWER
  fi

  SetDeployInstanceList
fi

if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  DEPLOY="$DEPLOY_DIR/bin/asadmin"
  if [ ! -x $DEPLOY ]; then
    $ECHO "Error: Cannot find $DEPLOY. $BELL_CHAR"
    exit 1
  fi
elif [ "$DEPLOY_TYPE" = "IWS" ]; then
  IWS_SERVER_HOME="$DEPLOY_DIR"
  export IWS_SERVER_HOME

  DEPLOY="$IWS_SERVER_HOME/bin/https/httpadmin/bin/wdeploy"
  if [ ! -x $DEPLOY ]; then
    $ECHO "Error: Cannot find $DEPLOY. $BELL_CHAR"
    exit 1
  fi
fi

$MKDIR -p $DEPLOY_TMP_DIR

$ECHO "" >> $LOG_FILE
$ECHO `$DATE +%D` "at" `$DATE +%r` >> $LOG_FILE
$ECHO "#########################################################" >> $LOG_FILE

IFS=","
for DEPLOY_INSTANCE in $DEPLOY_INSTANCES; do
  if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    $ECHO "Sync instance $DEPLOY_INSTANCE..." >> $LOG_FILE
    PARAMS="reconfig -u $DEPLOY_ADMIN -w $DEPLOY_ADMIN_PASSWORD -p $DEPLOY_ADMIN_PORT --keepmanualchanges $DEPLOY_INSTANCE"
    $ECHO "Calling $DEPLOY $PARAMS" | $SED -e "s/-w $DEPLOY_ADMIN_PASSWORD/-w ********/" >> $LOG_FILE
    eval $DEPLOY $PARAMS >> $LOG_FILE 2>&1

    $ECHO "Undeploying WAR from instance $DEPLOY_INSTANCE..." >> $LOG_FILE
    PARAMS="undeploy -u $DEPLOY_ADMIN -w $DEPLOY_ADMIN_PASSWORD -p $DEPLOY_ADMIN_PORT -H $DEPLOY_ADMIN_HOST --instance $DEPLOY_INSTANCE --type web portal"
    $ECHO "Calling $DEPLOY $PARAMS" | $SED -e "s/-w $DEPLOY_ADMIN_PASSWORD/-w ********/" >> $LOG_FILE
    eval $DEPLOY $PARAMS >> $LOG_FILE 2>&1
  elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    if [ "$MODE" = "" ]; then
      # XXX - need code to determine if portal is already deployed.
      MODE="redeploy"
    fi
  elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
    $ECHO "Undeploying WAR from instance $DEPLOY_INSTANCE..." >> $LOG_FILE
    $ECHO "\$AdminApp uninstall portal_war {-node $DEPLOY_NODE -cell $DEPLOY_CELL -server $DEPLOY_INSTANCE}" > $DEPLOY_TMP_DIR/undeploy.jacl
    $ECHO "\$AdminConfig save" >> $DEPLOY_TMP_DIR/undeploy.jacl
   
    # Execute it using the wsadmin
    eval $DEPLOY_DIR/bin/wsadmin.sh -f $DEPLOY_TMP_DIR/undeploy.jacl >> $LOG_FILE 2>&1
  elif [ "$DEPLOY_TYPE" = "IWS" ]; then
    $ECHO "Undeploying WAR from instance $DEPLOY_INSTANCE..." >> $LOG_FILE
    PARAMS="delete -u $DEPLOY_URI -i $DEPLOY_INSTANCE -v https-$DEPLOY_INSTANCE hard"
    $ECHO "Calling $DEPLOY $PARAMS" >> $LOG_FILE
    eval $DEPLOY $PARAMS >> $LOG_FILE 2>&1
  fi
done
IFS=$ORIG_IFS

TestDeploy

if [ $? -eq 0 ]; then
  DoDeploy
  UpdateJSPCompilerClasspath
  ReplaceLine $STATE_FILE "DEPLOY_URI=" "DEPLOY_URI=$DEPLOY_URI"
fi

exit 0
