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

}


################################################################################
# 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 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 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 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 serverclasspath with '` $1"

  local STR=$1
  local FILE=""

  FILE="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/config/server.xml"
  $GREP "$STR" $FILE > /dev/null
  if [ $? -ne 0 ]; then
    $CP $FILE $FILE-tmp
    $SED -e "s#serverclasspath=\"#serverclasspath=\"$STR:#" $FILE-tmp > $FILE
    $RM -f $FILE-tmp
  fi

}

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

RemoveEntry() {

  print "`$GETTEXT 'Removing portalserver entries from server.xml'`"

  local STR=$1
  local FILE=""

  FILE="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/config/server.xml"
  $SED -e "s#$STR:##g" $FILE > $FILE-tmp
  if [ -s $FILE-tmp ]; then
    $MV $FILE-tmp $FILE
  fi

}

################################################################################
# Update classpathsuffix SunONE webserver
################################################################################

UpdateClasspathSuffix() {

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

  local STR=$1
  local FILE=""

  FILE="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/config/server.xml"
  $GREP "$STR" $FILE > /dev/null
  if [ $? -ne 0 ]; then
    $CP $FILE $FILE-tmp
    $SED -e "s#classpathsuffix=\"#classpathsuffix=\"$STR:#" $FILE-tmp > $FILE
    $RM -f $FILE-tmp
  fi

}

################################################################################
# method to add MA specific mime types
################################################################################

AddMAMimeTypes() {

  print "`$GETTEXT 'Adding MA mime-types...'`"

  MIME_TYPES_FILE=$1

  MIME_TYPES="type=text/vnd.wap.wml type=image/vnd.wap.wbmp"
  set -A MIME_TYPE_EXTS exts=wml exts=wbmp

  export j=0
  for MIME_TYPE in ${MIME_TYPES}; do
    $GREP ${MIME_TYPE} $MIME_TYPES_FILE > /dev/null 2>&1
    if [ $? -ne 0 ]; then
      MIME_TYPE_DEFN="${MIME_TYPE}        ${MIME_TYPE_EXTS[j]}"
      $ECHO "Adding '${MIME_TYPE_DEFN}' in ${MIME_TYPES_FILE}"
      $ECHO "${MIME_TYPE_DEFN}" >> ${MIME_TYPES_FILE}
    else
      $ECHO "Mime type: '${MIME_TYPE}' already exists: Skipping ...."
    fi

    ((j=j+1))
  done

}

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

ConfigureBasePortal() {

  # Copy library files to webserver lib directory.
  print "`$GETTEXT 'Adding library files to container instance...'`"
  $CP $PS_BASEDIR/$PS_PRODUCT_DIR/lib/libdb*.so $DEPLOY_DIR/bin/https/lib
  
  $RM -rf $DEPLOY_DIR/https-$DEPLOY_INSTANCE/ClassCache/https-$DEPLOY_INSTANCE/*

  FILE="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/config/obj.conf"
  $GREP "fn=index-common" $FILE > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    DeleteLine $FILE "fn=index-common"
  fi

  # Update serverclasspath with portal jars.
  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"
 
  # Update classpathsuffix with MA jars and MA locale directory. 
  UpdateClasspathSuffix "$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
    UpdateClasspathSuffix "$MA_LIB_DIR/$MA_JARFILE"
  done

  # Add MA mime-types.
  AddMAMimeTypes $DEPLOY_DIR/https-$DEPLOY_INSTANCE/config/mime.types

}

################################################################################
# Configure SRA to web container.
################################################################################

ConfigureSRA() {

  print "`$GETTEXT 'Adding libgetpwnam library file to container instance...'`"

  $CP $PS_BASEDIR/$PS_PRODUCT_DIR/lib/libgetpwnam.so $DEPLOY_DIR/bin/https/lib
  $CP $PS_BASEDIR/$PS_PRODUCT_DIR/lib/libgetpwnam2.so $DEPLOY_DIR/bin/https/lib

}

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

Configure() {

  ConfigureBasePortal

  if [ "$CONFIGURATION_LEVEL" == "02" ]; then
    ConfigureSRA
  fi

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

  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
  
  # Remove portalserver contents from container instance
  $RM -rf $DEPLOY_DIR/https-$DEPLOY_INSTANCE/ClassCache/https-$DEPLOY_INSTANCE/*
  $RM -f $DEPLOY_DIR/bin/https/lib/libdb*.so $DEPLOY_DIR/bin/https/lib/libgetpwnam.so $DEPLOY_DIR/bin/https/lib/libgetpwnam2.so

  # Remove PS jars from classpath in server.xml
  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"

  # Remove MA entries from classpathsuffix in server.xml
  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

