#!/bin/ksh

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

###############################################
# Defines
###############################################

BELL_CHAR='\a'

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

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

################################################################################
# Update weblogic classpath
################################################################################

UpdateWebLogicClassPath() {

  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

}

###############################################################
# Updat WebSphereClasspath
###############################################################

UpdateWebSphereClassPath() {

  local STR=$1
	  
  FILE=$DEPLOY_DIR/config/cells/$DEPLOY_CELL/nodes/$DEPLOY_NODE/servers/$DEPLOY_INSTANCE/server.xml
  $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
}
###############################################
# 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
}

###############################################
# Update classpath SunONE webserver
###############################################
UpdateServerClasspath1() {
  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
}

###############################################
# Update classpath for SunONE appserver
###############################################
UpdateServerClasspath2() {
  local STR=$1
  local FILE=""

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

###############################################
# Replace Tokens in PDConfig.properties
###############################################
ReplacePDConfigTokens() {
  local FILE=""

  FILE="$PS_CONFIG_DIR/portlet/PDConfig.properties"
  $ECHO "Replacing tokens in $FILE."
  if [ -f $FILE ]; then
    $SED -e "s#%PS_CONFIG_DIR%#$PS_CONFIG_DIR#g" \
         -e "s#%PS_VAR_DIR%#$PS_VAR_DIR#g" \
         -e "s#%IDSAME_VAR_DIR%#$IDSAME_VAR_DIR#g" $FILE > $FILE.tmp
    $MV $FILE.tmp $FILE
  fi
}


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

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

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

GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_TYPE=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
  exit 1
fi
if [ "$DEPLOY_TYPE" = "SUNONE" ] || [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
  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_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_PORT. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_PROJECT_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_PROJECT_DIR=$ANSWER
  else
    DEPLOY_PROJECT_DIR="user_projects"
  fi
elif [ "$DEPLOY_TYPE" = "IWS" ]; then
  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_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi
elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  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"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi
fi
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  exit 1
fi

ReplacePDConfigTokens

if [ "$DEPLOY_TYPE" = "IWS" ]; then
  UpdateServerClasspath1 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcommon.jar"
  UpdateServerClasspath1 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcontainercommon.jar"
  UpdateServerClasspath1 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlet.jar"
  UpdateServerClasspath1 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlettl.jar"
  UpdateServerClasspath1 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jdom.jar"
elif [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  UpdateServerClasspath2 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcommon.jar"
  UpdateServerClasspath2 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcontainercommon.jar"
  UpdateServerClasspath2 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlet.jar"
  UpdateServerClasspath2 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlettl.jar"
  UpdateServerClasspath2 "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jdom.jar"
elif [ "$DEPLOY_TYPE" = "WEBLOGIC70" ]; then
  FILES="$DEPLOY_DIR/config/$DEPLOY_DOMAIN/startWebLogic.sh $DEPLOY_DIR/config/$DEPLOY_DOMAIN/startManagedWebLogic.sh"
  for FILE in $FILES; do
    $GREP "CLASSPATH=" $FILE | $GREP "portlet.jar" > /dev/null 2>&1
    if [ $? -ne 0 ]; then
      $CP $FILE $FILE-tmp
      $SED -e "/WLS_PW=/ {
a\\

a\\
CLASSPATH=\$CLASSPATH:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcommon.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcontainercommon.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlet.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlettl.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jdom.jar
}" $FILE-tmp > $FILE
      $RM $FILE-tmp
    fi
  done
elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
  UpdateWebLogicClassPath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcommon.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcontainercommon.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlet.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlettl.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jdom.jar"
elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  UpdateWebSphereClassPath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcommon.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portletcontainercommon.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlet.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/portlettl.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/lib/jdom.jar"
fi

exit 0
