#!/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/wcconfig.log"

AMCONFIG_FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"
STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

DEPLOY_TMP_DIR="$PS_VAR_DIR/tmp/deploy"

###############################################
# Usage
###############################################
Usage() {
  $ECHO "wcconfig"
  $ECHO ""
  $ECHO "  Command line options:"
  $ECHO "    help"
  $ECHO "  Command line options:"
  $ECHO "    config"
  $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
}

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

  $CP $FILE $FILE-tmp
  $SED -e "/$MATCH/ {
a\\
$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
}

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

#####################################################################
# Create IS silent install file and invoke IS-SDK configurator.
#####################################################################
ISConfig() {
  local IS_SILENT_INSTALL_FILE=""

  IS_SILENT_INSTALL_FILE=$DEPLOY_TMP_DIR/amsilent_ps

  $ECHO "DEPLOY_LEVEL=4" > $IS_SILENT_INSTALL_FILE
  $ECHO "BASEDIR=$IDSAME_BASEDIR" >> $IS_SILENT_INSTALL_FILE
  $ECHO "PRODUCT_DIR=$IDSAME_PRODUCT_DIR" >> $IS_SILENT_INSTALL_FILE
  $ECHO "CONFIG_DIR=$IDSAME_CONFIG_DIR/config" >> $IS_SILENT_INSTALL_FILE

  if [ "$DEPLOY_TYPE" = "IWS" ]; then
    $ECHO "WS61_HOME=$DEPLOY_DIR"  >> $IS_SILENT_INSTALL_FILE
    $ECHO "WS61_INSTANCE=https-$DEPLOY_INSTANCE" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WS61_PROTOCOL=$PS_PROTOCOL" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WS61_HOST=$PS_HOST" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WS61_PORT=$PS_PORT" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WS61_ADMIN=admin" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WS61_ADMINPASSWD=${DEPLOY_ADMIN_PASSWORD}" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WS61_ADMINPORT=$DEPLOY_ADMIN_PORT" >> $IS_SILENT_INSTALL_FILE
    $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amws61config -s $IS_SILENT_INSTALL_FILE
  elif [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    AS70_DOMAIN=`$BASENAME $DEPLOY_DOMAIN`

    $ECHO "AS70_HOME=$DEPLOY_DIR" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_DOMAIN=$AS70_DOMAIN" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_INSTANCE_DIR=$DEPLOY_DOMAIN/$DEPLOY_INSTANCE" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_INSTANCE=$DEPLOY_INSTANCE"  >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_DOCS_DIR=$DEPLOY_DOCROOT" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_IS_SECURE=FALSE" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_PROTOCOL=$PS_PROTOCOL" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_HOST=$DEPLOY_ADMIN_HOST" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_PORT=$PS_PORT" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_ADMIN=admin" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_ADMINPASSWD=${DEPLOY_ADMIN_PASSWORD}" >> $IS_SILENT_INSTALL_FILE
    $ECHO "AS70_ADMINPORT=$DEPLOY_ADMIN_PORT"  >> $IS_SILENT_INSTALL_FILE
    $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amas70config -s $IS_SILENT_INSTALL_FILE
  elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    $ECHO "WL8_HOME=$DEPLOY_DIR" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_PROJECT_DIR=$DEPLOY_PROJECT_DIR" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_DOMAIN=$DEPLOY_DOMAIN" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_SERVER=$DEPLOY_INSTANCE" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_INSTANCE=$DEPLOY_PRODUCT_DIR" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_PROTOCOL=$PS_PROTOCOL" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_HOST=$PS_HOST" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_PORT=$PS_PORT" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_ADMIN=$DEPLOY_ADMIN" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_PASSWORD=${DEPLOY_ADMIN_PASSWORD}" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WL8_JDK_HOME=$DEPLOY_JDK_DIR" >> $IS_SILENT_INSTALL_FILE
    $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amwl81config -s $IS_SILENT_INSTALL_FILE

    WebLogicConfigWorkarounds
  elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
    $ECHO "WAS51_HOME=$DEPLOY_DIR" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_JDK_HOME=$DEPLOY_JDK_DIR" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_NODE=$DEPLOY_NODE" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_CELL=$DEPLOY_CELL" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_INSTANCE=$DEPLOY_INSTANCE" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_PROTOCOL=$PS_PROTOCOL" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_HOST=$PS_HOST" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_PORT=$PS_PORT" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_ADMIN=$DEPLOY_ADMIN" >> $IS_SILENT_INSTALL_FILE
    $ECHO "WAS51_PASSWORD=${DEPLOY_ADMIN_PASSWORD}" >> $IS_SILENT_INSTALL_FILE
    $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amwas51config -s $IS_SILENT_INSTALL_FILE
  fi

  $RM -f $IS_SILENT_INSTALL_FILE

  # Make sure OrionConfigWorkarounds is run only once per install.
  if [ ! -f $AMCONFIG_FILE.prePS ]; then
    OrionConfigWorkarounds
  fi
}

###############################################
# Update WebLogic classpath
###############################################
UpdateWebLogicClassPath() {
  local STR=$1
  local FILE=""
  local FILES=""

  FILES="$DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/startWebLogic.sh $DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/startManagedWebLogic.sh"
  for FILE in $FILES; do
    $GREP "$STR" $FILE > /dev/null
    if [ $? -ne 0 ]; then
      $GREP "CLASSPATH=\"" $FILE > /dev/null
      if [ $? -ne 0 ]; then
        AppendLine $FILE "SERVER_NAME=" "CLASSPATH=\"$STR:\$CLASSPATH\""
      else 
        $CP -p $FILE $FILE-tmp
        $SED -e "s#CLASSPATH=\"#CLASSPATH=\"$STR:#" $FILE-tmp > $FILE
        $RM -f $FILE-tmp
      fi
    fi
  done
}

###############################################
# Remove entry from WebLogic start scripts
###############################################
RemoveWebLogicEntry() {
  local STR=$1
  local FILE=""
  local FILES=""

  FILES="$DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/startWebLogic.sh $DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/startManagedWebLogic.sh"
  for FILE in $FILES; do
    $CP -p $FILE $FILE-tmp
    $SED -e "s#$STR:##g" $FILE-tmp > $FILE
    $RM $FILE-tmp
  done
}

##################################################
# WebLogic specific modifications to configure 
# to use the JWSDP installed by Portal Server
##################################################
WebLogicConfigWorkarounds() {
  CUR_IFS=$IFS
  IFS=$ORIG_IFS

  # Step1. Update CLASSPATH with JWSDP jars shipped with PS.
  UpdateWebLogicClassPath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxrpc-api.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxrpc-impl.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxrpc-spi.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/xalan.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/xercesImpl.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/sax.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/dom.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/saaj-api.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/saaj-impl.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxp-api.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jax-qname.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/namespace.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/relaxngDatatype.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/xsdlib.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxb-api.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxb-impl.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxb-libs.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jaxb-xjc.jar"

  local IS_JARS="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jaxrpc_1.0/jaxrpc-api.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jaxrpc_1.0/jaxrpc-ri.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/xalan.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/xercesImpl.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/sax.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/dom.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/saaj-api.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/saaj-impl.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jaxp-api.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jax-qname.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/namespace.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/relaxngDatatype.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/xsdlib.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jaxb-api.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jaxb-impl.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jaxb-libs.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/jaxb-xjc.jar"
  for IS_JAR in $IS_JARS; do
    RemoveWebLogicEntry "$IS_JAR"
  done

  # Step2. Copy PS jars to endorsed directory.
  $RM -rf $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/endorsed/*
  $CP $PS_BASEDIR/$PS_PRODUCT_DIR/lib/sax.jar $PS_BASEDIR/$PS_PRODUCT_DIR/lib/dom.jar $PS_BASEDIR/$PS_PRODUCT_DIR/lib/xercesImpl.jar $PS_BASEDIR/$PS_PRODUCT_DIR/lib/xalan.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/endorsed
  
  # Step3. Make endorsed-for-weblogic directory and update IS_OPTIONS.
  $MKDIR -p $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/endorsed-for-weblogic
  $CP $PS_BASEDIR/$PS_PRODUCT_DIR/lib/xalan.jar $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/endorsed-for-weblogic
  local ORIG_IS_ENDORSED="-Djava.endorsed.dirs=$IDSAME_BASEDIR\/$IDSAME_PRODUCT_DIR\/lib\/endorsed "
  local NEW_IS_ENDORSED="-Djava.endorsed.dirs=$IDSAME_BASEDIR\/$IDSAME_PRODUCT_DIR\/lib\/endorsed-for-weblogic "
  FILES="$DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/startWebLogic.sh $DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/startManagedWebLogic.sh"
  for FILE in $FILES; do
    $CP $FILE $FILE-tmp
    $SED -e "s#$ORIG_IS_ENDORSED#$NEW_IS_ENDORSED#" $FILE-tmp > $FILE
    $RM -f $FILE-tmp
  done

  # Step4. Copy the WebLogic specific bootstrap properties
  if [ ! -f "$PS_CONFIG_DIR/bootstrapSystem.properties-orig" ]; then
     $MV $PS_CONFIG_DIR/bootstrapSystem.properties $PS_CONFIG_DIR/bootstrapSystem.properties-orig
  fi
  $CP $PS_CONFIG_DIR/WEBLOGIC.bootstrapSystem.properties $PS_CONFIG_DIR/bootstrapSystem.properties

  IFS=$CUR_IFS
}

##################################################
# Orion specific modifications
##################################################
OrionConfigWorkarounds() {
  $CP -p $AMCONFIG_FILE $AMCONFIG_FILE.prePS

  ReplaceLine $AMCONFIG_FILE "^com.iplanet.am.console.protocol=" "com.iplanet.am.console.protocol=$PS_PROTOCOL"
  ReplaceLine $AMCONFIG_FILE "^com.iplanet.am.console.host=" "com.iplanet.am.console.host=$PS_HOST"
  ReplaceLine $AMCONFIG_FILE "^com.iplanet.am.console.port=" "com.iplanet.am.console.port=$PS_PORT"
  ReplaceLine $AMCONFIG_FILE "^com.iplanet.am.console.remote=" "com.iplanet.am.console.remote=true"
  ReplaceLine $AMCONFIG_FILE "^com.iplanet.am.jssproxy.resolveIPAddress=" "com.iplanet.am.jssproxy.resolveIPAddress=true"
  AppendLine $AMCONFIG_FILE "#com.sun.identity.server.fqdnMap" "com.sun.identity.server.fqdnMap[$PS_HOST]=$PS_HOST"

  if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    ReplaceLine $AMCONFIG_FILE "^com.sun.identity.webcontainer=" "com.sun.identity.webcontainer=IAS7.0"
  elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
    ReplaceLine $AMCONFIG_FILE "^com.sun.identity.webcontainer=" "com.sun.identity.webcontainer=IBM5.1"
    ReplaceLine $AMCONFIG_FILE "^com.iplanet.am.cookie.encode=" "com.iplanet.am.cookie.encode=true"
  elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    ReplaceLine $AMCONFIG_FILE "^com.sun.identity.webcontainer=" "com.sun.identity.webcontainer=BEA8.1"
    ReplaceLine $AMCONFIG_FILE "^com.iplanet.am.cookie.encode=" "com.iplanet.am.cookie.encode=true"
    ReplaceLine $AMCONFIG_FILE "^com.iplanet.security.encryptor=" "com.iplanet.security.encryptor=com.iplanet.services.util.JCEEncryption"
    ReplaceLine $AMCONFIG_FILE "^com.iplanet.security.SSLSocketFactoryImpl=" "com.iplanet.security.SSLSocketFactoryImpl=netscape.ldap.factory.JSSESocketFactory"
    ReplaceLine $AMCONFIG_FILE "^com.iplanet.security.SecureRandomFactoryImpl=" "com.iplanet.security.SecureRandomFactoryImpl=com.iplanet.am.util.SecureRandomFactoryImpl"
  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_URI" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_URI=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_URI. $BELL_CHAR"
  exit 1
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

if [ "$1" = "config" ]; 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
else
  MODE="config"

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

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

  SetDeployInstanceList
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
  CheckISConfigured
  if [ "$ANSWER" = "n" ]; then
    ISConfig
    $ECHO "IS SDK webcontainer configuration complete for instance: $DEPLOY_INSTANCE"  >> $LOG_FILE
  else
    $ECHO "Skipping Configuration. IS SDK is already configured for instance: $DEPLOY_INSTANCE" >> $LOG_FILE
  fi
done
IFS=$ORIG_IFS

exit 0
