#!/bin/ksh


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

OMIT_CHAR='\c'
BELL_CHAR='\a'
CP=/usr/bin/cp
MV=/usr/bin/mv
WC=/usr/bin/wc
SED=/usr/bin/sed
AWK=/usr/bin/awk
STTY=/usr/bin/stty
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
UNIQ=/usr/bin/uniq
ID=/usr/bin/id
RM=/usr/bin/rm
CLEAR=/usr/bin/clear
SLEEP=/usr/bin/sleep
GETTEXT=/usr/bin/gettext
PKGINFO=/usr/bin/pkginfo
INSTALLF=/usr/sbin/installf
SUNWMALOCALEDIR=/opt/SUNWma/locale
SUNWMALIBDIR=/opt/SUNWma/lib

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  CP=/bin/cp
  ECHO=/bin/echo
  GREP=/bin/grep
  MV=/bin/mv
  RM=/bin/rm
  SED=/bin/sed
  SLEEP=/bin/sleep
  STTY=/bin/stty
  SUNWMALIBDIR=/opt/sun/mobileaccess/share/lib
  SUNWMALOCALEDIR=/opt/sun/mobileaccess/locale
fi

STATE_FILE="/etc/opt/SUNWps/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/SUNWps/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/SUNWps/lib/container.jar"
  UpdateServerClasspath "$PS_BASEDIR/SUNWps/lib/psimapprovider.jar"
  UpdateServerClasspath "$PS_BASEDIR/SUNWps/lib/portletcommon.jar"
  UpdateServerClasspath "$PS_BASEDIR/SUNWps/lib/portletcontainercommon.jar"
  UpdateServerClasspath "$PS_BASEDIR/SUNWps/lib/portlet.jar"
  UpdateServerClasspath "$PS_BASEDIR/SUNWps/lib/portlettl.jar"
  UpdateServerClasspath "$PS_BASEDIR/SUNWps/lib/jdom.jar"
 
  # Update classpathsuffix with MA jars and MA locale directory. 
  UpdateClasspathSuffix "$SUNWMALOCALEDIR"
  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 "$SUNWMALIBDIR/$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/SUNWps/lib/libgetpwnam.so $DEPLOY_DIR/bin/https/lib
  $CP $PS_BASEDIR/SUNWps/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/SUNWps/lib/wcconfig config -instance $DEPLOY_INSTANCE
  $PS_BASEDIR/SUNWps/bin/deploy redeploy -instance $DEPLOY_INSTANCE
  
  print "`$GETTEXT 'Configuring search...'`"
  $PS_BASEDIR/SUNWps/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/SUNWps/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/SUNWps/lib/container.jar"
  RemoveEntry "$PS_BASEDIR/SUNWps/lib/psimapprovider.jar"
  RemoveEntry "$PS_BASEDIR/SUNWps/lib/portletcommon.jar"
  RemoveEntry "$PS_BASEDIR/SUNWps/lib/portletcontainercommon.jar"
  RemoveEntry "$PS_BASEDIR/SUNWps/lib/portlet.jar"
  RemoveEntry "$PS_BASEDIR/SUNWps/lib/portlettl.jar"
  RemoveEntry "$PS_BASEDIR/SUNWps/lib/jdom.jar"

  # Remove MA entries from classpathsuffix in server.xml
  RemoveEntry "$SUNWMALOCALEDIR"
  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 "$SUNWMALIBDIR/$MA_JARFILE"
  done

  # Remove the directories created by deploy and searchconfig
  $RM -rf /var/opt/SUNWps/https-$DEPLOY_INSTANCE
  $RM -rf /var/opt/SUNWps/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

