#!/bin/ksh

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

################################################################################
# Define the constants
################################################################################

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

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

################################################################################
# Get configuration from specified file.
################################################################################

GrabConfig() {

    GRABCONFIG_FILE=$1
    GRABCONFIG_KEY=$2
    GRABCONFIG_SEPARATOR=$3

    ANSWER=`$GREP "^$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR" $GRABCONFIG_FILE | $UNIQ | $SED -e "s/$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR//"`

}

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

################################################################################
# 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 in not present
################################################################################

CheckStateFile() {

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

}

################################################################################
# Get the base directory
################################################################################

GetPSBaseDir() {

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

}

################################################################################
# Load the state file to memory.
################################################################################

LoadStateFile() {
 
  print "`$GETTEXT 'Reading the required informations for configuration...'`"

  # Get the configuration level
  GrabConfig $STATE_FILE "CONFIGURATION_LEVEL" "="
  if [ "$ANSWER" != "" ]; then
    CONFIGURATION_LEVEL=$ANSWER
  else
    $ECHO "Error: Cannot determine CONFIGURATION_LEVEL. $BELL_CHAR"
  fi
  
  # Get the deploy directory 
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi

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

  # Get the user project's directory 
  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

  # Get the deploy domain
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
    exit 1
  fi

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

  # Get the deployment document root
  GrabConfig $STATE_FILE "DEPLOY_DOCROOT" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOCROOT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOCROOT. $BELL_CHAR"
    exit 1
  fi

  # Get the install time deployment document root
  GrabConfig $STATE_FILE "INSTALLTIME_DEPLOY_DOCROOT" "="
  INSTALLTIME_DEPLOY_DOCROOT=$ANSWER

  # Get the portal server port
  GrabConfig $STATE_FILE "PS_PORT" "="
  if [ "$ANSWER" != "" ]; then
    PS_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine PS_PORT. $BELL_CHAR"
    exit 1
  fi
}

################################################################################
# Update serverclasspath SunONE webserver
################################################################################

UpdateServerClasspath() {

  print "`$GETTEXT 'Updating CLASSPATH with '` $1"

  local STR=$1
  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 $FILE $FILE-tmp
        $SED -e "s#CLASSPATH=\"#CLASSPATH=\"$STR:#" $FILE-tmp > $FILE
        $RM -f $FILE-tmp
      fi
    fi
  done

}

################################################################################
# Remove portalserver entriy from server.xml
################################################################################

RemoveEntry() {

  local STR=$1

  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
    print "`$GETTEXT 'Removing portalserver entries from $FILE'`"
    $SED -e "s#$STR:##g" $FILE > $FILE-tmp
    if [ -s $FILE-tmp ]; then
      $MV $FILE-tmp $FILE
    fi
  done

}

################################################################################
# Configure the base portal.
################################################################################

ConfigureBasePortal() {
 
  # Update CLASSPATH with PS entries.	
  UpdateServerClasspath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/container.jar"
  UpdateServerClasspath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/psimapprovider.jar"
  UpdateServerClasspath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcommon.jar"
  UpdateServerClasspath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcontainercommon.jar"
  UpdateServerClasspath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlet.jar"
  UpdateServerClasspath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlettl.jar"
  UpdateServerClasspath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jdom.jar"
  UpdateServerClasspath "$MA_CONFIG_DIR/lib/locale"
  
  # Update classpathsuffix with MA jars and MA locale directory.
  UpdateServerClasspath "$MA_DIR/locale"
  local MA_JARFILES="mobile_services.jar wireless_rendering_util.jar wireless_rendering.jar ccpp-1_0.jar ccpp-ri-1_0.jar jena-1.4.0.jar rdffilter.jar"
  for MA_JARFILE in $MA_JARFILES; do
    UpdateServerClasspath "$MA_LIB_DIR/$MA_JARFILE"
  done

  # Copy MA (voice) files to document root directory.
  if [ "$INSTALLTIME_DEPLOY_DOCROOT" != "" ] && [ "$DEPLOY_DOCROOT" != "$INSTALLTIME_DEPLOY_DOCROOT" ]; then
    $CP -rf $INSTALLTIME_DEPLOY_DOCROOT/voice $DEPLOY_DOCROOT
  fi
  
}

################################################################################
#
# Configure the required components based on the CONFIGURATION_LEVEL.
# Possible levels - 
# 	01 : Configure BasePortal
#	02 : Configure BasePortal, and SRA
#
################################################################################

Configure() {

  ConfigureBasePortal

  # Deploy the portalserver.
  print "`$GETTEXT 'Deploying portalserver...'`"
  $PS_BASEDIR/$PS_PRODUCT_DIR/lib/wcconfig config -instance $DEPLOY_INSTANCE -deploy_admin_password ${DEPLOY_ADMIN_PASSWORD}
  $PS_BASEDIR/$PS_PRODUCT_DIR/bin/deploy deploy -instance $DEPLOY_INSTANCE -deploy_admin_password ${DEPLOY_ADMIN_PASSWORD}

  print "`$GETTEXT 'Configuring search...'`"
  $PS_BASEDIR/$PS_PRODUCT_DIR/lib/searchconfig -is_admin_password "${IDSAME_ADMIN_PASSWORD}" -instance $DEPLOY_INSTANCE -port $PS_PORT

}

################################################################################
#
# Unconfigure portalserver.
#
################################################################################

UnConfigure() {

  # Undeploy the portalserver.
  print "`$GETTEXT 'Undeploying portalserver...'`"
  $PS_BASEDIR/$PS_PRODUCT_DIR/bin/undeploy undeploy -instance $DEPLOY_INSTANCE -deploy_admin_password ${DEPLOY_ADMIN_PASSWORD}
  # Remove PS jars from classpath - don't do, other instances may be running portal.
  # RemoveEntry "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/container.jar"
  # RemoveEntry "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/psimapprovider.jar"
  # RemoveEntry "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcommon.jar"
  # RemoveEntry "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcontainercommon.jar"
  # RemoveEntry "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlet.jar"
  # RemoveEntry "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlettl.jar"
  # RemoveEntry "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jdom.jar"
  # RemoveEntry "$MA_CONFIG_DIR/lib/locale"

  # Remove MA entries from classpathsuffix in server.xml - don't do, other instances may be running portal.
  # RemoveEntry "$MA_DIR/locale"
  # local MA_JARFILES="mobile_services.jar wireless_rendering_util.jar wireless_rendering.jar ccpp-1_0.jar ccpp-ri-1_0.jar jena-1.4.0.jar rdffilter.jar"
  # for MA_JARFILE in $MA_JARFILES; do
  #   RemoveEntry "$MA_LIB_DIR/$MA_JARFILE"
  # done

  # Remove the directories created by deploy and searchconfig
  $RM -rf $PS_VAR_DIR/https-$DEPLOY_INSTANCE
  $RM -rf $PS_VAR_DIR/SAVE-https-$DEPLOY_INSTANCE

}

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

# Make sure root is executing this script.
CheckUser

# Set state file pointer.
if [ "$1" = "-s" ]; then 
  STATE_FILE=$2 
fi

# Make sure the state file is present
CheckStateFile

# Get the base directory.
GetPSBaseDir

# Load the state file to memory.
LoadStateFile

# Configure/Unconfigure the required components.
if [ "$CONFIGURATION_LEVEL" == "11" ]; then
  UnConfigure
else
  Configure
fi

