#!/bin/ksh

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

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

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

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

}

################################################################################
# Replaces the first occurance of the matching line with newline
################################################################################

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
}

################################################################################
# Delete line in file
################################################################################

DeleteLine() {
  local FILE=$1
  local MATCH=$2

  $CP -p $FILE $FILE-tmp
  $SED -e "/$MATCH/d" $FILE-tmp > $FILE
  $RM $FILE-tmp
}

################################################################################
# Exit if non root user is executing this script
################################################################################

CheckUser() {

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

}

################################################################################
# Exit if state file is not present.
################################################################################

CheckStateFile() {

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

}

###############################################
# Check for ASCII characters
###############################################

CheckChars() {
  local VALID_CHARS=$1
  local STR=$2
  local US_SPECIFIC=""

  if [ "${LANG}" != "" ]; then
    $ECHO ${LANG} | $GREP "en_US" > /dev/null 2>&1
    if [ $? -eq 1 ]; then
      US_SPECIFIC="n"
      $ECHO $VALID_CHARS | $GREP "a-z" > /dev/null 2>&1
      if [ $? -eq 0 ]; then
        US_SPECIFIC="y"
      fi
      $ECHO $VALID_CHARS | $GREP "A-Z" > /dev/null 2>&1
      if [ $? -eq 0 ]; then
	US_SPECIFIC="y"
      fi
      if [ "$US_SPECIFIC" = "y" ]; then
	return 0
      fi
    fi
  fi

  if [ "$STR" != "" ]; then
    $ECHO $STR | $GREP "[^$VALID_CHARS]" > /dev/null 2>&1
    if [ $? -eq 1 ]; then
      return 0
    else
      return 1
    fi
  else
    return 1
  fi
}

################################################################################
# Question
################################################################################

Question() {

  local PROMPT=$1
  local VALID_CHARS=$2
  local DEFAULT=$3
  local ERROR_MESSAGE=$4
  local DONE=""
  local CHECK=""

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

}

###############################################
# Question
###############################################
QuestionWithoutDefault() {
  local PROMPT=$1
  local VALID_CHARS=$2
  local DONE=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    eval print "$PROMPT \$OMIT_CHAR"
    read ANSWER
    HAD_INPUT="y"
    if [ "$ANSWER" = "" ]; then
      DONE="n"
    else
      CheckChars "$VALID_CHARS" "$ANSWER"
      if [ $? -eq 0 ]; then
        DONE="y"
      else
        print "`$GETTEXT 'Invalid answer!'` $BELL_CHAR"
      fi
    fi
  done
}


################################################################################
# Yes or No
################################################################################

YesNo() {

  local PROMPT=$1
  local DEFAULT=$2
  local DONE=""
  local FULL_ANSWER=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    if [ "$DEFAULT" = "y" ]; then
      eval print "$PROMPT [y]/n \$OMIT_CHAR"
      FULL_ANSWER="n"
    elif [ "$DEFAULT" = "n" ]; then
      eval print "$PROMPT y/[n] \$OMIT_CHAR"
      FULL_ANSWER="n"
    elif [ "$DEFAULT" = "yes" ]; then
      eval print "$PROMPT [yes]/no \$OMIT_CHAR"
      FULL_ANSWER="y"
    elif [ "$DEFAULT" = "no" ]; then
      eval print "$PROMPT yes/[no] \$OMIT_CHAR"
      FULL_ANSWER="y"
    else
      eval print "$PROMPT y/n \$OMIT_CHAR"
      FULL_ANSWER="n"
    fi

    read ANSWER
    HAD_INPUT="y"

    if [ "$ANSWER" = "" ] && [ "$DEFAULT" != "" ]; then
      ANSWER=$DEFAULT
      DONE="y"
    else
      ANSWER=`$ECHO $ANSWER | $SED -e "y/YESNO/yesno/"`
      if [ "$FULL_ANSWER" = "n" ]; then
        if [ "$ANSWER" = "yes" ]; then
          ANSWER="y"
        elif [ "$ANSWER" = "no" ]; then
          ANSWER="n"
        fi
        if [ "$ANSWER" = "y" ]; then
          DONE="y"
        elif [ "$ANSWER" = "n" ]; then
          DONE="y"
        else
          print "`$GETTEXT 'Invalid response! y/n only.'` $BELL_CHAR"
        fi
      else
        if [ "$ANSWER" = "yes" ]; then
          DONE="y"
        elif [ "$ANSWER" = "no" ]; then
          DONE="y"
        else
          print "`$GETTEXT 'Invalid response! yes/no only.'` $BELL_CHAR"
        fi
      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
    if [ "$ANSWER" != "" ]; then
      DONE="y"
    fi
  done

}

##############################################################################
# List the existing portalserver instances.
##############################################################################

ListInstances() {

    print "`$GETTEXT 'Detected portalserver instances:'`"
    print ""
    INSTANCES=""

    # Grab and print all existing instances.
    GrabConfig $STATE_FILE "S1WS_INSTANCES" "="
    if [ "$ANSWER" != "" ]; then
      INSTANCES="$INSTANCES $ANSWER"
      for INSTANCE in $INSTANCES; do
        print "        $INSTANCE on Sun Java System Web Server"
      done
    fi

    GrabConfig $STATE_FILE "S1AS_INSTANCES" "="
    if [ "$ANSWER" != "" ]; then
      INSTANCES="$INSTANCES $ANSWER"
      for INSTANCE in $INSTANCES; do
        print "        $INSTANCE on Sun Java System Application Server"
      done
    fi

    GrabConfig $STATE_FILE "BEAWL_INSTANCES" "="
    if [ "$ANSWER" != "" ]; then
      INSTANCES="$INSTANCES $ANSWER"
      for INSTANCE in $INSTANCES; do
        print "        $INSTANCE on BEA WebLogic Application Server"
      done
    fi

    GrabConfig $STATE_FILE "IBMWS_INSTANCES" "="
    if [ "$ANSWER" != "" ]; then
      INSTANCES="$INSTANCES $ANSWER"
      for INSTANCE in $INSTANCES; do
        print "        $INSTANCE on IBM Websphere Application Server"
      done
    fi
    
    if [ "$INSTANCES" == "" ]; then
        print "        - None -"
    fi

    print ""

}

################################################################################
# Show the install options and get user's choice.
################################################################################

ShowInstallOptions() {

  local PROMPT="`$GETTEXT 'Install options:'`"	

  local MENU_ITEMS="`$GETTEXT 'Create a new portalserver instance'`;CREATE;"
  if [ "$INSTANCES" != "" ]; then
    MENU_ITEMS="$MENU_ITEMS`$GETTEXT 'Remove a portalserver instance'`;REMOVE;"
  fi
  MENU_ITEMS="$MENU_ITEMS`$GETTEXT 'Exit'`;QUIT;"

  local DEFAULT=""
  local DONE=""
  local ITEM=""
  local ITEMS=""
  local ITEM_COUNT=0
  local MENU_ANSWER=""
  local RETURN=""
  local RETURNS=""

  print "$PROMPT"
  print ""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    RETURNS=""
    let ITEM_COUNT=0
    ITEMS=$MENU_ITEMS
    while [ "$ITEMS" != "" ]; do
      let ITEM_COUNT+=1

      ITEM=${ITEMS%%;*}
      ITEMS=${ITEMS#*;}

      RETURN="${ITEMS%%;*}"
      ITEMS=${ITEMS#*;}

      print "$ITEM_COUNT) $ITEM"
      DEFAULT=$ITEM_COUNT

      RETURNS="$RETURNS$RETURN;"
    done

    print ""
    print "`$GETTEXT 'Choice?'` [$DEFAULT] $OMIT_CHAR"
    read MENU_ANSWER

    if [ "$MENU_ANSWER" = "" ]; then
      MENU_ANSWER=$DEFAULT
      DONE="y"
    else
      CheckChars "0-9" $MENU_ANSWER
      if [ $? -eq 0 ]; then
        if [ $MENU_ANSWER -ge 1 ] && [ $MENU_ANSWER -le $ITEM_COUNT ]; then
          DONE="y"
	else
	  print ""
	  print "`$GETTEXT 'Invalid response!'` $BELL_CHAR"
	  print ""
        fi
	else
          print ""
          print "`$GETTEXT 'Invalid response!'` $BELL_CHAR"
	  print ""
        fi
    fi
  done
    
  let ITEM_COUNT=0
  while [ "$RETURNS" != "" ]; do
    let ITEM_COUNT+=1
    ANSWER="${RETURNS%%;*};"
    RETURNS=${RETURNS#*;}
    if [ "$ITEM_COUNT" = "$MENU_ANSWER" ]; then
      RETURNS="";
    fi
  done

}

################################################################################
# Show the container options and get user's choice.
################################################################################

ShowContainerOptions() {

  local PROMPT="`$GETTEXT 'Choose the container to which the portalserver needs to be configured/unconfigured:'`"	

  local MENU_ITEMS="`$GETTEXT 'Sun Java System Web Server'`;IWS;"
  MENU_ITEMS="$MENU_ITEMS`$GETTEXT 'Sun Java System Application Server'`;SUNONE;"
  MENU_ITEMS="$MENU_ITEMS`$GETTEXT 'BEA WebLogic Application Server'`;BEAWL;"
  MENU_ITEMS="$MENU_ITEMS`$GETTEXT 'IBM Websphere Application Server'`;IBMWS;"
  MENU_ITEMS="$MENU_ITEMS`$GETTEXT 'Go to previous screen'`;GOBACK;"

  local DEFAULT=""
  local DONE=""
  local ITEM=""
  local ITEMS=""
  local ITEM_COUNT=0
  local MENU_ANSWER=""
  local RETURN=""
  local RETURNS=""

  print "$PROMPT"
  print ""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    RETURNS=""
    let ITEM_COUNT=0
    ITEMS=$MENU_ITEMS
    while [ "$ITEMS" != "" ]; do
        let ITEM_COUNT+=1

        ITEM=${ITEMS%%;*}
        ITEMS=${ITEMS#*;}

        RETURN="${ITEMS%%;*}"
        ITEMS=${ITEMS#*;}

        print "$ITEM_COUNT) $ITEM"
        DEFAULT=$ITEM_COUNT

        RETURNS="$RETURNS$RETURN;"
    done

    print ""
    print "`$GETTEXT 'Choice?'` [$DEFAULT] $OMIT_CHAR"
    read MENU_ANSWER

    if [ "$MENU_ANSWER" = "" ]; then
        MENU_ANSWER=$DEFAULT
	DONE="y"
    else
	CheckChars "0-9" $MENU_ANSWER
	if [ $? -eq 0 ]; then
	    if [ $MENU_ANSWER -ge 1 ] && [ $MENU_ANSWER -le $ITEM_COUNT ]; then
	    	DONE="y"
	    else
		print ""
	        print "`$GETTEXT 'Invalid response!'` $BELL_CHAR"
		print ""
	    fi
	else
            print ""
	    print "`$GETTEXT 'Invalid response!'` $BELL_CHAR"
	    print ""
        fi
    fi
  done
    
  let ITEM_COUNT=0
  while [ "$RETURNS" != "" ]; do
    let ITEM_COUNT+=1
    ANSWER="${RETURNS%%;*};"
    RETURNS=${RETURNS#*;}
    if [ "$ITEM_COUNT" = "$MENU_ANSWER" ]; then
      RETURNS="";
    fi
  done

}

################################################################################
# Get Portal server base directory.
################################################################################

GetPSBaseDir() {

  # Portal server base directory - required for invoking pss1ws61config script.
  GrabConfig $STATE_FILE "BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
    PS_BASEDIR=$ANSWER
  else
    $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
    exit 1
  fi

}

################################################################################
# Load the existing information from state file.
################################################################################

GetCommonInfo() {

    # Container base directory.
    GrabConfig $STATE_FILE "DEPLOY_DIR" "="
    DEPLOY_DIR=$ANSWER

    # Deployment domain.
    GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
    DEPLOY_DOMAIN=$ANSWER

    # Deploy admin.
    GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
    DEPLOY_ADMIN=$ANSWER

    # Deploy admin port.
    GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
    DEPLOY_ADMIN_PORT=$ANSWER

    # Deployment cell.
    GrabConfig $STATE_FILE "DEPLOY_CELL" "="
    DEPLOY_CELL=$ANSWER

    # Deployment cell.
    GrabConfig $STATE_FILE "DEPLOY_NODE" "="
    DEPLOY_NODE=$ANSWER

    # Deployment JDK directory.
    GrabConfig $STATE_FILE "DEPLOY_JDK_DIR" "="
    DEPLOY_JDK_DIR=$ANSWER

    # Deploy virtual host
    GrabConfig $STATE_FILE "DEPLOY_VIRTUAL_HOST" "="
    DEPLOY_VIRTUAL_HOST=$ANSWER

}

################################################################################
# Get the required information for configuring PS on new S1WS instance.
################################################################################

GetS1WSAddInstanceInfo() {

  # Get the portalserver base directory.
  GetPSBaseDir

  # If the portal is installed with S1WS, then get the existing information.
  if [ "$INSTALLTIME_DEPLOY_TYPE" == "IWS" ]; then
    GetCommonInfo
  fi
  
  # Web container base directory.
  Question "`$GETTEXT 'Where is the Web Container installed?'`" "a-zA-Z" "$DEPLOY_DIR"
  DEPLOY_DIR=$ANSWER
  
  # New instance name.
  local DONE="n"
  while [ "$DONE" == "n" ]; do
    QuestionWithoutDefault "`$GETTEXT 'Instance nickname?'`" "a-zA-Z0-9\./_-" "`$GETTEXT 'Only alphanumeric characters!'`"
    DEPLOY_INSTANCE=$ANSWER

    CheckInstancePresent "$STATE_FILE" "S1WS_INSTANCES" "$DEPLOY_INSTANCE"
    if [ ! -d "$DEPLOY_DIR/https-$DEPLOY_INSTANCE" ]; then
      print "`$GETTEXT 'Error: Web container instance does not exist.'` $BELL_CHAR"
    elif [ "$ANSWER" == "y" ]; then
      print "`$GETTEXT 'Error: Portal server is already configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done

  # New instance port.
  QuestionWithoutDefault "`$GETTEXT 'Instance port?'`" $PORT "0-9" "`$GETTEXT 'Only numeric characters!'`"
  PS_PORT=$ANSWER

  # New instance protocol.
  PS_PROTOCOL="http"
  YesNo "`$GETTEXT 'Is the instance port secure?'`" "n"
  if [ "$ANSWER" == "y" ]; then
    PS_PROTOCOL="https"
  fi
  
  GetPassword "`$GETTEXT 'What is the Identity Server administration password?'`"
  IDSAME_ADMIN_PASSWORD=$ANSWER

  DEPLOY_DOCROOT=$DEPLOY_DIR/docs

}

################################################################################
# Get the required information for unconfiguring PS from S1WS instance.
################################################################################

GetS1WSRemoveInstanceInfo() {

  # Get the portalserver base directory.
  GetPSBaseDir

  # Instance name.
  local DONE="n"
  while [ "$DONE" == "n" ]; do
    QuestionWithoutDefault "`$GETTEXT 'Instance nickname?'`" "a-zA-Z0-9\./_-" "`$GETTEXT 'Only alphanumeric characters!'`"
    DEPLOY_INSTANCE=$ANSWER
    CheckInstancePresent "$STATE_FILE" "S1WS_INSTANCES" "$DEPLOY_INSTANCE"
    if [ "$ANSWER" == "n" ]; then
      print "`$GETTEXT 'Error: Portal server is not configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done

}

################################################################################
# Get the required information for S1WS configuration.
################################################################################

GetS1WSInfo() {

  if [ "$MODE" == "create" ]; then
    GetS1WSAddInstanceInfo
  else
    GetS1WSRemoveInstanceInfo
  fi

}

################################################################################
# Get the required information for configuring PS on new S1AS instance.
################################################################################

GetS1ASAddInstanceInfo() {

  # Get the portalserver base directory.
  GetPSBaseDir
	  
  # If the portal is installed with S1AS, then get the existing information.
  if [ "$INSTALLTIME_DEPLOY_TYPE" == "SUNONE" ]; then
    GetCommonInfo
  fi

  # Web container base directory.
  Question "`$GETTEXT 'Where is the Web Container installed?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DIR"
  DEPLOY_DIR=$ANSWER

  # Deploy domain.
  Question "`$GETTEXT 'What is the Web Container domain?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DOMAIN"
  DEPLOY_DOMAIN=$ANSWER
    
  # Deploy admin.
  Question "`$GETTEXT 'What is the Web Container administrator?'`" "a-zA-Z0-9\./_-" "$DEPLOY_ADMIN"
  DEPLOY_ADMIN=$ANSWER
  
  # Deploy admin port.
  Question "`$GETTEXT 'What is the Web Container administration port?'`" "0-9" "$DEPLOY_ADMIN_PORT"
  DEPLOY_ADMIN_PORT=$ANSWER
  
  # Deploy admin protocol.
  DEPLOY_ADMIN_PROTOCOL="http"
  YesNo "`$GETTEXT 'Is the Web Container administration port secure?'`" "n"
  if [ "$ANSWER" == "y" ]; then
    DEPLOY_ADMIN_PROTOCOL="https"
  fi

  # New instance name.
  local DONE="n"
  while [ "$DONE" == "n" ]; do

    QuestionWithoutDefault "`$GETTEXT 'Instance nickname?'`" "a-zA-Z0-9\./_-" "`$GETTEXT 'Only alphanumeric characters!'`"
    DEPLOY_INSTANCE=$ANSWER

    CheckInstancePresent "$STATE_FILE" "S1AS_INSTANCES" "$DEPLOY_INSTANCE"
    if [ ! -d "$DEPLOY_DOMAIN/$DEPLOY_INSTANCE" ]; then
      print "`$GETTEXT 'Error: Web container instance does not exist'` $BELL_CHAR"
    elif [ "$ANSWER" == "y" ]; then
      print "`$GETTEXT 'Error: Portal server is already configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done
 
  # New instance port.
  QuestionWithoutDefault "`$GETTEXT 'Instance port?'`" $PORT "0-9" "`$GETTEXT 'Only numeric characters!'`"
  PS_PORT=$ANSWER

  # New instance protocol.
  PS_PROTOCOL="http"
  YesNo "`$GETTEXT 'Is the instance port secure?'`" "n"
  if [ "$ANSWER" == "y" ]; then
    PS_PROTOCOL="https"
  fi
    
  # Deployment document root
  Question "`$GETTEXT 'What is the Web Container document root directory?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DOMAIN/$DEPLOY_INSTANCE/docroot"
  DEPLOY_DOCROOT=$ANSWER
  
  # Application server administration password
  GetPassword "What is the Application Server administration password?"
  DEPLOY_ADMIN_PASSWORD=$ANSWER
  print ""

  GetPassword "`$GETTEXT 'What is the Identity Server administration password?'`"
  IDSAME_ADMIN_PASSWORD=$ANSWER
  print ""

}

################################################################################
# Get the required information for removing PS from S1AS instance.
################################################################################

GetS1ASRemoveInstanceInfo() {

  # Get the portalserver base directory.
  GetPSBaseDir

  # Instance name.
  local DONE="n"
  while [ "$DONE" == "n" ]; do
    QuestionWithoutDefault "`$GETTEXT 'Instance nickname?'`" "a-zA-Z0-9\./_-" "`$GETTEXT 'Only alphanumeric characters!'`"
    DEPLOY_INSTANCE=$ANSWER
    CheckInstancePresent "$STATE_FILE" "S1AS_INSTANCES" "$DEPLOY_INSTANCE"
    if [ "$ANSWER" == "n" ]; then
      print "`$GETTEXT 'Error: Portal server is not configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done

  # Application server administration password
  GetPassword "What is the Application Server administration password?"
  DEPLOY_ADMIN_PASSWORD=$ANSWER
  print ""

}

################################################################################
# Get the required information for S1AS configuration.
################################################################################

GetS1ASInfo() {

  if [ "$MODE" == "create" ]; then
    GetS1ASAddInstanceInfo
  else
    GetS1ASRemoveInstanceInfo
  fi

}

################################################################################
# Get the required information for configuring PS to BEA WL container instance.
################################################################################

GetBEAWLAddInstaceInfo() {
  
  # Get the portalserver base directory.
  GetPSBaseDir
	  
  # If the portal is installed with S1AS, then get the existing information.
  if [ "$INSTALLTIME_DEPLOY_TYPE" == "WEBLOGIC" ]; then
    GetCommonInfo
  fi

  Question "`$GETTEXT 'Where is the Web Container Home Directory?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DIR"
  DEPLOY_DIR=$ANSWER
  
  # Deploy domain.
  Question "`$GETTEXT 'What is the Web Container domain?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DOMAIN"
  DEPLOY_DOMAIN=$ANSWER
  
  # Deploy admin.
  Question "`$GETTEXT 'What is the Web Container administrator?'`" "a-zA-Z0-9\./_-" "$DEPLOY_ADMIN"
  DEPLOY_ADMIN=$ANSWER
 
  # Deploy admin port.
  Question "`$GETTEXT 'What is the Web Container administration port?'`" "0-9" "$DEPLOY_ADMIN_PORT"
  DEPLOY_ADMIN_PORT=$ANSWER
  
  # Deploy admin protocol.
  DEPLOY_ADMIN_PROTOCOL="http"
  YesNo "`$GETTEXT 'Is the Web Container administration port secure?'`" "n"
  if [ "$ANSWER" == "y" ]; then
    DEPLOY_ADMIN_PROTOCOL="https"
  fi

  # User project's directory.
  Question "`$GETTEXT 'What is the User Projects Directory'`" "a-zA-Z0-9\./_-" "user_projects"
  DEPLOY_PROJECT_DIR=$ANSWER

  # New instance name.
  local DONE="n"
  while [ "$DONE" == "n" ]; do
    QuestionWithoutDefault "`$GETTEXT 'Instance nickname?'`" "a-zA-Z0-9\./_-" "`$GETTEXT 'Only alphanumeric characters!'`"
    DEPLOY_INSTANCE=$ANSWER

    CheckInstancePresent "$STATE_FILE" "BEAWL_INSTANCES" "$DEPLOY_INSTANCE"
    if [ ! -d "$DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/$DEPLOY_INSTANCE" ]; then
      print "`$GETTEXT 'Error: Web container instance does not exist'` $BELL_CHAR"
    elif [ "$MODE" == "create" ] && [ "$ANSWER" == "y" ]; then
      print "`$GETTEXT 'Error: Portal server is already configured in this instance.'` $BELL_CHAR"
    elif [ "$MODE" == "delete" ] && [ "$ANSWER" == "n" ]; then
      print "`$GETTEXT 'Error: Portal server is not configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done
  
  # New instance port.
  QuestionWithoutDefault "`$GETTEXT 'Instance port?'`" $PORT "0-9" "`$GETTEXT 'Only numeric characters!'`"
  PS_PORT=$ANSWER

  # New instance protocol.
  PS_PROTOCOL="http"
  YesNo "`$GETTEXT 'Is the instance port secure?'`" "n"
  if [ "$ANSWER" == "y" ]; then
    PS_PROTOCOL="https"
  fi
  
  # Deployment document root
  Question "`$GETTEXT 'What is the Web Container document root directory?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DIR/$DEPLOY_PROJECT_DIR/domains/$DEPLOY_DOMAIN/applications"
  DEPLOY_DOCROOT=$ANSWER
  
  # Application server administration password
  GetPassword "What is the Application Server administration password?"
  DEPLOY_ADMIN_PASSWORD=$ANSWER
  print ""

  GetPassword "`$GETTEXT 'What is the Identity Server administration password?'`"
  IDSAME_ADMIN_PASSWORD=$ANSWER
  print ""

}

################################################################################
# Get the required information for removing PS from BEA WebLogic instance.
################################################################################

GetBEAWLRemoveInstanceInfo() {
  
  # Get the portalserver base directory.
  GetPSBaseDir

  # Instance name.
  local DONE="n"
  while [ "$DONE" == "n" ]; do
    QuestionWithoutDefault "`$GETTEXT 'Instance nickname?'`" "a-zA-Z0-9\./_-" "`$GETTEXT 'Only alphanumeric characters!'`"
    DEPLOY_INSTANCE=$ANSWER
    CheckInstancePresent "$STATE_FILE" "BEAWL_INSTANCES" "$DEPLOY_INSTANCE"
    if [ "$ANSWER" == "n" ]; then
      print "`$GETTEXT 'Error: Portal server is not configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done

  # Application server administration password.
  GetPassword "What is the Application Server administration password?"
  DEPLOY_ADMIN_PASSWORD=$ANSWER
  print ""

}

################################################################################
# Get the required information for BEA WebLogic configuration.
################################################################################

GetBEAWLInfo() {

  if [ "$MODE" == "create" ]; then
    GetBEAWLAddInstaceInfo
  else
    GetBEAWLRemoveInstanceInfo
  fi

}

################################################################################
# Get the required information for configuring PS to IBM Websphere instance.
################################################################################

GetIBMWSAddInstaceInfo() {
  
  # Get the portalserver base directory.
  GetPSBaseDir
	  
  # If the portal is installed with S1AS, then get the existing information.
  if [ "$INSTALLTIME_DEPLOY_TYPE" == "WEBSPHERE" ]; then
    GetCommonInfo
  fi

  Question "`$GETTEXT 'Where is the Web Container Base Directory?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DIR"
  IBMWS_BASEDIR=$ANSWER
  export IBMWS_BASEDIR
  
  Question "`$GETTEXT 'Where is the Web Container Instance Directory?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DIR"
  DEPLOY_DIR=$ANSWER
  
  # Deploy virtual host.
  Question "`$GETTEXT 'What is the Virtual Host Name?'`" "a-zA-Z0-9\./_-" "$DEPLOY_VIRTUAL_HOST"
  DEPLOY_VIRTUAL_HOST=$ANSWER
  
  # Deploy node.
  Question "`$GETTEXT 'What is the Node name?'`" "a-zA-Z0-9\./_-" "$DEPLOY_NODE"
  DEPLOY_NODE=$ANSWER
  STATEFILE_SUFFIX=$DEPLOY_NODE
  
  # Deploy cell.
  Question "`$GETTEXT 'What is the Cell name?'`" "a-zA-Z0-9\./_-" "$DEPLOY_CELL"
  DEPLOY_CELL=$ANSWER
  
  # New instance name.
  local DONE="n"
  while [ "$DONE" == "n" ]; do
    QuestionWithoutDefault "`$GETTEXT 'Instance nickname?'`" "a-zA-Z0-9\./_-" "`$GETTEXT 'Only alphanumeric characters!'`"
    DEPLOY_INSTANCE=$ANSWER

    CheckInstancePresent "$STATE_FILE" "IBMWS_INSTANCES" "$STATEFILE_SUFFIX"
    if [ ! -d "$DEPLOY_DIR/config/cells/$DEPLOY_CELL/nodes/$DEPLOY_NODE/servers/$DEPLOY_INSTANCE" ]; then
      print "`$GETTEXT 'Error: Web container instance does not exist'` $BELL_CHAR"
    elif [ "$MODE" == "create" ] && [ "$ANSWER" == "y" ]; then
      print "`$GETTEXT 'Error: Portal server is already configured in this instance.'` $BELL_CHAR"
    elif [ "$MODE" == "delete" ] && [ "$ANSWER" == "n" ]; then
      print "`$GETTEXT 'Error: Portal server is not configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done
  
  # New instance port.
  QuestionWithoutDefault "`$GETTEXT 'Instance port?'`" $PORT "0-9" "`$GETTEXT 'Only numeric characters!'`"
  PS_PORT=$ANSWER

  # New instance protocol.
  PS_PROTOCOL="http"
  YesNo "`$GETTEXT 'Is the instance port secure?'`" "n"
  if [ "$ANSWER" == "y" ]; then
    PS_PROTOCOL="https"
  fi
  
  # Deployment document root
  Question "`$GETTEXT 'What is the Web Container document root directory?'`" "a-zA-Z0-9\./_-" "/opt/WebSphere/AppServer/web/docs"
  DEPLOY_DOCROOT=$ANSWER
  
  # JDK HOME .
  Question "`$GETTEXT 'What is the Web Container JDK Home ?'`" "a-zA-Z0-9\./_-" "/opt/WebSphere/AppServer/java"
  DEPLOY_JDK_DIR=$ANSWER
 
  GetPassword "`$GETTEXT 'What is the Identity Server administration password?'`"
  IDSAME_ADMIN_PASSWORD=$ANSWER
  print ""

}

################################################################################
# Collect the required informations for unconfiguring PS from IBM WAS instance
################################################################################

GetIBMWSRemoveInstanceInfo() {

  # Get the portalserver base directory.
  GetPSBaseDir

  Question "`$GETTEXT 'Where is the Web Container Base Directory?'`" "a-zA-Z0-9\./_-" "$DEPLOY_DIR"
  IBMWS_BASEDIR=$ANSWER
  export IBMWS_BASEDIR
  
  local DONE="n"
  while [ "$DONE" == "n" ]; do
  
    # Deploy node.
    Question "`$GETTEXT 'What is the Node name?'`" "a-zA-Z0-9\./_-" "$DEPLOY_NODE"
    DEPLOY_NODE=$ANSWER
    STATEFILE_SUFFIX=$DEPLOY_NODE

    CheckInstancePresent "$STATE_FILE" "IBMWS_INSTANCES" "$STATEFILE_SUFFIX"
    if [ "$ANSWER" == "n" ]; then
      print "`$GETTEXT 'Error: Portal server is not configured in this instance.'` $BELL_CHAR"
    else
      DONE="y"
    fi
  done

}  

################################################################################
# Get the required information for IBM Websphere configuration.
################################################################################

GetIBMWSInfo() {

  if [ "$MODE" == "create" ]; then
    GetIBMWSAddInstaceInfo
  else
    GetIBMWSRemoveInstanceInfo
  fi

}

################################################################################
# Get the information needed for creating a new instance.
################################################################################

GetInfo() {

  PREV_SCREEN="n"

  # Get the container to which portalserver should be deployed.
  ShowContainerOptions
  print ""
	
  if [ "$ANSWER" == "IWS;" ]; then
    DEPLOY_TYPE="IWS"
    GetS1WSInfo
  elif [ "$ANSWER" == "SUNONE;" ]; then
    DEPLOY_TYPE="SUNONE"
    GetS1ASInfo
  elif [ "$ANSWER" == "BEAWL;" ]; then
    DEPLOY_TYPE="WEBLOGIC"
    GetBEAWLInfo
  elif [ "$ANSWER" == "IBMWS;" ]; then
    DEPLOY_TYPE="WEBSPHERE"
    GetIBMWSInfo
  elif [ "$ANSWER" == "GOBACK;" ]; then
    PREV_SCREEN="y"
  fi

}

################################################################################
# Display the summary.
################################################################################

DisplayInstallSummary() {

  print "" 
  print "" 
  print "`$GETTEXT 'New portalserver instance installation summary'`" 
  print "" 
  if [ "$DEPLOY_TYPE" == "IWS" ]; then
    print "`$GETTEXT '  Container                               : '`" "Sun Java System Web Server"
  elif [ "$DEPLOY_TYPE" == "SUNONE" ]; then
    print "`$GETTEXT '  Container                               : '`" "Sun Java System Application Server"
  elif [ "$DEPLOY_TYPE" == "WEBLOGIC" ]; then
    print "`$GETTEXT '  Container                               : '`" "BEA WebLogic Application Server"
  elif [ "$DEPLOY_TYPE" == "WEBSPHERE" ]; then
    print "`$GETTEXT '  Container                               : '`" "IBM Websphere Application Server"
  fi
  print "`$GETTEXT '  Instance name                           : '`" "$DEPLOY_INSTANCE"
  print "`$GETTEXT '  Instance port                           : '`" "$PS_PORT"
  print "`$GETTEXT '  Instance protocol                       : '`" "$PS_PROTOCOL"
  
  if [ "$DEPLOY_TYPE" != "IWS" ] && [ "$DEPLOY_TYPE" != "WEBSPHERE" ]; then
    print "`$GETTEXT '  Administrator name                      : '`" "$DEPLOY_ADMIN"
    print "`$GETTEXT '  Administration protocol                 : '`" "$DEPLOY_ADMIN_PROTOCOL"
    print "`$GETTEXT '  Administration port                     : '`" "$DEPLOY_ADMIN_PORT"
  fi
  print ""

}

################################################################################
# Display the summary.
################################################################################

DisplayUninstallSummary() {

  print "" 
  print "" 
  print "`$GETTEXT 'Unconfiguration summary'`" 
  print "" 
  if [ "$DEPLOY_TYPE" == "IWS" ]; then
    print "`$GETTEXT '  Container                               : '`" "Sun Java System Web Server"
    print "`$GETTEXT '  Instance name                           : '`" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "SUNONE" ]; then
    print "`$GETTEXT '  Container                               : '`" "Sun Java System Application Server"
    print "`$GETTEXT '  Instance name                           : '`" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "WEBLOGIC" ]; then
    print "`$GETTEXT '  Container                               : '`" "BEA WebLogic Application Server"
    print "`$GETTEXT '  Instance name                           : '`" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "WEBSPHERE" ]; then
    print "`$GETTEXT '  Container                               : '`" "IBM Websphere Application Server"
    print "`$GETTEXT '  Node Name                               : '`" "$DEPLOY_NODE"
  fi
  print ""

}

################################################################################
# Confirms the details.
################################################################################

ConfirmSummary() {
    print ""
    YesNo "`$GETTEXT 'Are the above informations correct?'`" "y"
}

################################################################################
# Create a temporary state file for configuration
################################################################################

CreateStateFile() {

  # Copy the original $PS_CONFIG_DIR/PSConfig.properties to temporary file.
  TIMESTAMP=`date +%Y%m%d%H%M`
  BACKUP_STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties.$TIMESTAMP"
  $CP $STATE_FILE $BACKUP_STATE_FILE
  
  if [ "$MODE" == "delete" ] && [ -f "$PS_CONFIG_DIR/PSConfig-$STATEFILE_SUFFIX.properties" ]; then
    $CP $PS_CONFIG_DIR/PSConfig-$STATEFILE_SUFFIX.properties $STATE_FILE
  fi

  # Change appropriate values.
  if [ "$MODE" != "delete" ]; then
    ReplaceLine "$STATE_FILE" "DEPLOY_DOMAIN=" "DEPLOY_DOMAIN=$DEPLOY_DOMAIN"
    ReplaceLine "$STATE_FILE" "DEPLOY_ADMIN=" "DEPLOY_ADMIN=$DEPLOY_ADMIN"
    ReplaceLine "$STATE_FILE" "DEPLOY_ADMIN_PROTOCOL=" "DEPLOY_ADMIN_PROTOCOL=$DEPLOY_ADMIN_PROTOCOL"
    ReplaceLine "$STATE_FILE" "DEPLOY_ADMIN_PORT=" "DEPLOY_ADMIN_PORT=$DEPLOY_ADMIN_PORT"
    ReplaceLine "$STATE_FILE" "DEPLOY_PROJECT_DIR=" "DEPLOY_PROJECT_DIR=$DEPLOY_PROJECT_DIR"
    ReplaceLine "$STATE_FILE" "DEPLOY_DOCROOT=" "DEPLOY_DOCROOT=$DEPLOY_DOCROOT"
    ReplaceLine "$STATE_FILE" "PS_PORT=" "PS_PORT=$PS_PORT"
    ReplaceLine "$STATE_FILE" "PS_PROTOCOL=" "PS_PROTOCOL=$PS_PROTOCOL"
    ReplaceLine "$STATE_FILE" "DEPLOY_CELL=" "DEPLOY_CELL=$DEPLOY_CELL"
    ReplaceLine "$STATE_FILE" "DEPLOY_NODE=" "DEPLOY_NODE=$DEPLOY_NODE"
    ReplaceLine "$STATE_FILE" "DEPLOY_JDK_DIR=" "DEPLOY_JDK_DIR=$DEPLOY_JDK_DIR"
    ReplaceLine "$STATE_FILE" "DEPLOY_VIRTUAL_HOST=" "DEPLOY_VIRTUAL_HOST=$DEPLOY_VIRTUAL_HOST"
    ReplaceLine "$STATE_FILE" "DEPLOY_DIR=" "DEPLOY_DIR=$DEPLOY_DIR"
    ReplaceLine "$STATE_FILE" "DEPLOY_TYPE=" "DEPLOY_TYPE=$DEPLOY_TYPE"
    ReplaceLine "$STATE_FILE" "DEPLOY_INSTANCE=" "DEPLOY_INSTANCE=$DEPLOY_INSTANCE"
  fi
  
  # Add CONFIGURATION_LEVEL to state file.
  DeleteLine "$STATE_FILE" "CONFIGURATION_LEVEL="

  if [ "$MODE" == "delete" ]; then
    $ECHO "CONFIGURATION_LEVEL=11" >> $STATE_FILE
  else
    if [ -f "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/postinstall_SRA" ]; then
      $ECHO "CONFIGURATION_LEVEL=02" >> $STATE_FILE
    else
      $ECHO "CONFIGURATION_LEVEL=01" >> $STATE_FILE
    fi
  fi

  # Add INSTALLTIME_DEPLOY_DOCROOT to state file, required to copy MA (voice) files to new instance.
  $ECHO "INSTALLTIME_DEPLOY_DOCROOT=$INSTALLTIME_DEPLOY_DOCROOT" >> $STATE_FILE

  # Export the password as environment variables
  export IDSAME_ADMIN_PASSWORD
  export DEPLOY_ADMIN_PASSWORD
  
}

################################################################################
# Invoke appropriate script for configuring PS
################################################################################

ConfigureInstance() {

  if [ "$DEPLOY_TYPE" == "IWS" ]; then
    $PS_BASEDIR/$PS_PRODUCT_DIR/bin/pss1ws61config
  elif [ "$DEPLOY_TYPE" == "SUNONE" ]; then
    $PS_BASEDIR/$PS_PRODUCT_DIR/bin/pss1as70config
  elif [ "$DEPLOY_TYPE" == "WEBLOGIC" ]; then
    $PS_BASEDIR/$PS_PRODUCT_DIR/bin/pswl81config
  elif [ "$DEPLOY_TYPE" == "WEBSPHERE" ]; then
    $PS_BASEDIR/$PS_PRODUCT_DIR/bin/pswas51config
  fi

}

################################################################################
# Craete a new key-value pair if it is not already present.
################################################################################

CreateKeyValue() {

  local FILE=$1
  local KEY=$2
  local VALUE=$3
  
  # Create the key-value pair if it is not already present
  $GREP $KEY $FILE > /dev/null
  if [ $? -ne 0 ]; then
    $ECHO "$KEY=$VALUE" >> $FILE
  fi
  
}

################################################################################
# Update the list of instances to which the portal is configured/unconfigured. 
################################################################################

UpdateStateFile() {

  # Get the existing DEPLOY_TYPE and DEPLOY_INSTANCE from the state file.
  GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
  if [ "$ANSWER" != "" ]; then
    INSTALLTIME_DEPLOY_TYPE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
    exit 1
  fi

  # Get the existing DEPLOY_DOCROOT, required for copying MA (voice) files.
  GrabConfig $STATE_FILE "DEPLOY_DOCROOT" "="
  if [ "$ANSWER" != "" ]; then
    INSTALLTIME_DEPLOY_DOCROOT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOCROOT. $BELL_CHAR"
    exit 1
  fi

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    CUR_DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi

  if [ "$INSTALLTIME_DEPLOY_TYPE" == "IWS" ]; then
    CreateKeyValue $STATE_FILE "S1WS_INSTANCES" $CUR_DEPLOY_INSTANCE
  elif [ "$INSTALLTIME_DEPLOY_TYPE" == "SUNONE" ]; then
    CreateKeyValue $STATE_FILE "S1AS_INSTANCES" $CUR_DEPLOY_INSTANCE
  elif [ "$INSTALLTIME_DEPLOY_TYPE" == "WEBLOGIC" ]; then
    CreateKeyValue $STATE_FILE "BEAWL_INSTANCES" $CUR_DEPLOY_INSTANCE
  elif [ "$INSTALLTIME_DEPLOY_TYPE" == "WEBSPHERE" ]; then
    GrabConfig $STATE_FILE "DEPLOY_VIRTUAL_HOST" "="
    CUR_DEPLOY_INSTANCE=$ANSWER
    GrabConfig $STATE_FILE "DEPLOY_NODE" "="
    CUR_DEPLOY_INSTANCE=$CUR_DEPLOY_INSTANCE_$ANSWER
    CreateKeyValue $STATE_FILE "IBMWS_INSTANCES" $CUR_DEPLOY_INSTANCE
  fi

}

################################################################################
# Supporting method to add new instace name to state file.
################################################################################

AppendValueToKey() {
	
  local FILE=$1
  local KEY=$2
  local VALUE=$3
  
  CURLINE=`$GREP $KEY $FILE`
  
  # Add the value if it is not already present.
  $ECHO $CURLINE | $GREP $VALUE > /dev/null
  if [ $? -ne 0 ]; then
    CURLINE="$CURLINE $VALUE"
    DeleteLine $FILE $KEY
    $ECHO $CURLINE >> $FILE
  fi
}

################################################################################
# Supporting method to delete instance name from state file.
################################################################################

RemoveValueFromKey() {
	
  local FILE=$1
  local KEY=$2
  local VALUE=$3
  
  CURLINE=`$GREP $KEY $FILE`
  
  # Delete the value if found
  $ECHO $CURLINE | $GREP $VALUE > /dev/null
  if [ $? -eq 0 ]; then
    DeleteLine $FILE $KEY
    CURLINE=`$ECHO $CURLINE | $GREP $VALUE | $SED -e "s#$VALUE##"`
    $ECHO $CURLINE >> $FILE
  fi

}

################################################################################
# Return "y" if the instance found, false otherwise
################################################################################

CheckInstancePresent() {

  local FILE=$1
  local KEY=$2
  local VALUE=$3
  ANSWER="n"

  GrabConfig "$FILE" "$KEY" "="
  INSTANCES=$ANSWER
  for INSTANCE in $INSTANCES; do
    if [ "$INSTANCE" == "$VALUE" ]; then
      ANSWER="y"
    fi
  done

}

################################################################################
# Add the new instace name to state file.
################################################################################

AddNewInstanceToStateFile() {

  if [ "$DEPLOY_TYPE" == "IWS" ]; then
    AppendValueToKey "$BACKUP_STATE_FILE" "S1WS_INSTANCES" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "SUNONE" ]; then
    AppendValueToKey "$BACKUP_STATE_FILE" "S1AS_INSTANCES" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "WEBLOGIC" ]; then
    AppendValueToKey "$BACKUP_STATE_FILE" "BEAWL_INSTANCES" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "WEBSPHERE" ]; then
    AppendValueToKey "$BACKUP_STATE_FILE" "IBMWS_INSTANCES" "$STATEFILE_SUFFIX"
  fi

}

################################################################################
# Remove instace name from state file.
################################################################################

RemoveInstanceFromStateFile() {

  if [ "$DEPLOY_TYPE" == "IWS" ]; then
    RemoveValueFromKey "$BACKUP_STATE_FILE" "S1WS_INSTANCES" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "SUNONE" ]; then
    RemoveValueFromKey "$BACKUP_STATE_FILE" "S1AS_INSTANCES" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "WEBLOGIC" ]; then
    RemoveValueFromKey "$BACKUP_STATE_FILE" "BEAWL_INSTANCES" "$DEPLOY_INSTANCE"
  elif [ "$DEPLOY_TYPE" == "WEBSPHERE" ]; then
    RemoveValueFromKey "$BACKUP_STATE_FILE" "IBMWS_INSTANCES" "$STATEFILE_SUFFIX"
  fi

}

################################################################################
# Add new PSConfig-<instance>.properties to the system.
################################################################################

AddStateFile() {

  DeleteLine "$STATE_FILE" "CONFIGURATION_LEVEL="
  DeleteLine "$STATE_FILE" "S1WS_INSTANCES="
  DeleteLine "$STATE_FILE" "S1AS_INSTANCES="
  DeleteLine "$STATE_FILE" "BEAWL_INSTANCES="
  DeleteLine "$STATE_FILE" "IBMWS_INSTANCES="
  DeleteLine "$STATE_FILE" "INSTALLTIME_DEPLOY_DOCROOT="
  $CP $STATE_FILE "$PS_CONFIG_DIR/PSConfig-$STATEFILE_SUFFIX.properties"
  $MV $BACKUP_STATE_FILE $STATE_FILE

}

################################################################################
# Remove PSConfig-<instance>.properties
################################################################################

RemoveStateFile() {

  $RM -f "$PS_CONFIG_DIR/PSConfig-$STATEFILE_SUFFIX.properties"
  $MV $BACKUP_STATE_FILE $STATE_FILE

}

################################################################################
# Collect info, display summary, and create new instance.
################################################################################

CreateInstance() {

  local CREATED="n"

  while [ "$CREATED" = "n" ]; do

    print ""

    # Get the other information needed from the user.
    GetInfo

    # Go back to previous screen
    if [ "$PREV_SCREEN" == "y" ]; then
      return
    fi

    # Display the summary
    DisplayInstallSummary

    # Confirm the details.
    ConfirmSummary

    if [ "$ANSWER" = "y" ]; then
        CREATED="y"

  	print ""
	print "`$GETTEXT 'Installation in progres...'`"
	
        # Special case for IBM WebSphere - Store the state file as PSConfig_${DEPLOY_NODE}
	if [ "$DEPLOY_TYPE" == "WEBSPHERE" ]; then
	  STATEFILE_SUFFIX=$DEPLOY_NODE
        else
	  STATEFILE_SUFFIX=$DEPLOY_INSTANCE
	fi
        # Write the information to the state file.
        CreateStateFile
  	print ""
	print "`$GETTEXT 'Configuring new portalserver instance...'`" 
        # Configure the new portalserver instance
	ConfigureInstance
        # Update the list of instances to which the portal is configured/unconfigured. 
	AddNewInstanceToStateFile
        # Add new state file to the system.
        AddStateFile
  	print ""

    fi

  done

}

################################################################################
# Remove portalserver instance.
################################################################################

RemoveInstance() {

  local DELETED="n"

  while [ "$DELETED" = "n" ]; do

    print ""

    # Get the other information needed from the user.
    GetInfo

    # Go back to previous screen
    if [ "$PREV_SCREEN" == "y" ]; then
      return
    fi

    # Display the summary
    DisplayUninstallSummary

    # Confirm the details.
    ConfirmSummary

    if [ "$ANSWER" = "y" ]; then
        DELETED="y"

  	print ""
	print "`$GETTEXT 'Unconfiguration in progres...'`" 
        # Special case for IBM WebSphere - Store the state file as PSConfig-${DEPLOY_VIRTUAL_HOST}_${DEPLOY_NODE}
	if [ "$DEPLOY_TYPE" == "WEBSPHERE" ]; then
	  STATEFILE_SUFFIX=$DEPLOY_NODE
        else
	  STATEFILE_SUFFIX=$DEPLOY_INSTANCE
	fi
        # Write the information to the state file.
        CreateStateFile
  	print ""
        # Unconfigure the new portalserver instance
	ConfigureInstance
        # Update the list of instances to which the portal is configured/unconfigured. 
	RemoveInstanceFromStateFile
        # Remove the state file.
        RemoveStateFile
  	print ""

    fi

  done

}

################################################################################
# Removes all portalserver instances.
################################################################################

RemoveAllInstances() {
  print "Removing all instances...."
}

################################################################################
# Start the main method to create multi instances                            
################################################################################

StartMultiInstance() {

    local DONE="n"

    while [ "$DONE" = "n" ]; do

	# Clear the screen
	$CLEAR
	print ""

        # List the existing portalserver instances.
        ListInstances

        # User option.
	ShowInstallOptions
	print ""

	if [ "$ANSWER" = "CREATE;" ]; then
            MODE="create"
            CreateInstance
	elif [ "$ANSWER" = "REMOVE;" ]; then
            MODE="delete"
	    RemoveInstance
	elif [ "$ANSWER" = "REMOVEALL;" ]; then
	    RemoveAllInstances
	elif [ "$ANSWER" = "QUIT;" ]; then
	    DONE="y"
        fi
        
	if [ "$DONE" == "n" ]; then 
	  print "`$GETTEXT 'Hit ENTER key to continue...'`" 
          read x
	fi

    done
    
    print ""

}


##############################################################################
# Main                                                                       #
##############################################################################
# Make sure root is executing this script.
CheckUser

# Make sure state file exists
CheckStateFile

# Create the multiinstace specific keys in state file.
UpdateStateFile

# Enter the main loop.
StartMultiInstance

