#!/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
LN=/usr/bin/ln
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//"`

}

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

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

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

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

###############################################

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

UpdateServerClasspath() {

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

  FILE=$DEPLOY_DIR/config/cells/$DEPLOY_CELL/nodes/$DEPLOY_NODE/servers/$DEPLOY_INSTANCE/server.xml
  local STR=$1
  $GREP "$STR" $FILE > /dev/null
  if [ $? -ne 0 ]; then
      $GREP "<classpath>" $FILE > /dev/null
      if [ $? -ne 0 ]; then
	$GREP "<\/jvmEntries>" $FILE > /dev/null
	if [ $? -ne 0 ]; then
	  JVM_ENTRIES=`$GREP "<jvmEntries" $FILE | $SED -e "s#\/##g"`
          DeleteLine $FILE "<jvmEntries"
          AppendLine $FILE "<monitoringPolicy" "$JVM_ENTRIES"
	  AppendLine $FILE "<jvmEntries" "<classpath>$STR</classpath>"
	  AppendLine $FILE "<\/classpath>" "</jvmEntries>"
	else
	  AppendLine $FILE "<jvmEntries" "<classpath>$STR</classpath>"
        fi
      else 
        $CP $FILE $FILE-tmp
        $SED -e "s#<classpath>#<classpath>$STR:#" $FILE-tmp > $FILE
        $RM -f $FILE-tmp
      fi
  fi

}

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

RemoveEntry() {

  local STR=$1

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

}

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

ConfigureBasePortal() {
 
  # Update CLASSPATH with PS entries.	
  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"
  UpdateServerClasspath "/etc/opt/SUNWma/lib/locale"
  
  # Update classpathsuffix with MA jars and MA locale directory. 
  UpdateServerClasspath "$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
    UpdateServerClasspath "$SUNWMALIBDIR/$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
  
}

################################################################################
# Create the softlinks temporarily.
################################################################################

CreateLinks() {

  cd $DEPLOY_DIR/bin

  # Set all the command line parameters not set by $DEPLOY_DIR/bin/setupCmdLine.sh
  . $IBMWS_BASEDIR/bin/setupCmdLine.sh
  
  # Create a link to wsadmin.sh for the portalserver deployment.
  $LN -s $IBMWS_BASEDIR/bin/wsadmin.sh $DEPLOY_DIR/bin/wsadmin.sh
  
  # Create a link to startServer.sh to workaround bug with IS-SDK config.
  $LN -s $IBMWS_BASEDIR/bin/startServer.sh $DEPLOY_DIR/bin/startServer.sh

  # Create a link to stopServer.sh to workaround bug with IS-SDK config. 
  $LN -s $IBMWS_BASEDIR/bin/stopServer.sh $DEPLOY_DIR/bin/stopServer.sh

}

################################################################################
# Remove the temporarily created softlinks
################################################################################

RemoveLinks() {

  $RM $DEPLOY_DIR/bin/wsadmin.sh
  $RM $DEPLOY_DIR/bin/startServer.sh
  $RM $DEPLOY_DIR/bin/stopServer.sh

}

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

Configure() {

  local CUR_DIR=`pwd`

  # Create the temporary links.
  CreateLinks

  # Configure base portal.
  ConfigureBasePortal

  # 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

  # Remove the temporarily created softlinks
  RemoveLinks

  cd $CUR_DIR

}

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

UnConfigure() {

  local CUR_DIR=`pwd`

  CreateLinks

  # Undeploy the portalserver.
  print "`$GETTEXT 'Undeploying portalserver...'`"
  $PS_BASEDIR/SUNWps/bin/undeploy undeploy -instance $DEPLOY_INSTANCE
 
  # Remove PS jars from classpath 
  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"
  RemoveEntry "/etc/opt/SUNWma/lib/locale"

  # 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 

  RemoveLinks

}

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

