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

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

###############################################
# Update weblogic classpath
###############################################
UpdateWebLogicClassPath() {
  local STR=$1
  local FILES=""
  local FILE=""

  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
}

###############################################
# Update weblogic library path.
###############################################
UpdateWebLogicLibraryPath() {
  local STR=$1
  local FILES=""
  local FILE=""

  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 "LD_LIBRARY_PATH=\"" $FILE > /dev/null
      if [ $? -ne 0 ]; then
        AppendLine $FILE "SERVER_NAME=" "LD_LIBRARY_PATH=\"$STR:\$LD_LIBRARY_PATH\""
        AppendLine $FILE "LD_LIBRARY_PATH=" "export LD_LIBRARY_PATH"
      else 
        $CP $FILE $FILE-tmp
        $SED -e "s#LD_LIBRARY_PATH=\"#LD_LIBRARY_PATH=\"$STR:#" $FILE-tmp > $FILE
        $RM -f $FILE-tmp
      fi
    fi
  done
}

###############################################
# Update WebSphereClasspath
###############################################
UpdateWebSphereClassPath() {
  local STR=$1
  local FILE=""
  local JVM_ENTRIES=""
	  
  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
}

###############################################
# Update WebSphereLibraryPath
###############################################
UpdateWebSphereLibraryPath () {
  local STR=$1
  local FILE=""
	  
  FILE="$DEPLOY_DIR/bin/setupCmdLine.sh"
  $ECHO "" >> $FILE
  $ECHO "LD_LIBRARY_PATH=$STR:\$LD_LIBRARY_PATH" >> $FILE
  $ECHO "export LD_LIBRARY_PATH" >> $FILE
  $ECHO "" >> $FILE
}

###############################################
# 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" ]; 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
elif [ "$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_PROJECT_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_PROJECT_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_PROJECT_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_DOCROOT" "="
  if [ "$ANSWER" != "" ]; then
     DEPLOY_DOCROOT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOCROOT. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PROTOCOL" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_PROTOCOL=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_PROTOCOL. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_HOST. $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
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_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
  GrabConfig $STATE_FILE "DEPLOY_DOCROOT" "="
  if [ "$ANSWER" != "" ]; then
     DEPLOY_DOCROOT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOCROOT. $BELL_CHAR"
    exit 1
  fi
else
  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
fi
GrabConfig $STATE_FILE "JDK_DIR" "="
if [ "$ANSWER" != "" ]; then
  JDK_DIR=$ANSWER
else
  $ECHO "Error: Cannot determine JDK_DIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_HOST" "="
if [ "$ANSWER" != "" ]; then
  SERVER_HOST=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_HOST. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "DEPLOY_URI" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_URI=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_URI. $BELL_CHAR"
  exit 1
fi

DIRS=`$LS $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/docs/en 2>&1`
if [ $? -eq 0 ]; then
  for DIR in $DIRS; do
    $CP -r $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/docs/en/$DIR $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/docs
  done
fi

if [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  DIR="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/public_html/online_help"
  if [ -d $DEPLOY_DOCROOT ] && [ -d $DIR ]; then
    $LN -s $DIR $DEPLOY_DOCROOT
  fi
fi

CS_LIB_DIR=$PS_BASEDIR/$PS_PRODUCT_DIR/lib
if [ "$DEPLOY_TYPE" = "IWS" ]; then
  $CP $CS_LIB_DIR/libdb*.so $DEPLOY_DIR/bin/https/lib
fi

FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"
$CP $FILE $FILE-tmp
$SED -e "s/com.iplanet.am.jssproxy.resolveIPAddress=false/com.iplanet.am.jssproxy.resolveIPAddress=true/" $FILE-tmp > $FILE
$RM -f $FILE-tmp

FILE=$PS_CONFIG_DIR/desktop/desktopconfig.properties
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF "SUNWpsdt" $FILE
fi
$CP $FILE $FILE-tmp
$SED -e "s#%PS_BASEDIR%#$PS_BASEDIR#g" \
	 -e "s#%PS_PRODUCT_DIR%#$PS_PRODUCT_DIR#g" \
	 -e "s#%PS_CONFIG_DIR%#$PS_CONFIG_DIR#g" $FILE-tmp > $FILE
$RM -f $FILE-tmp
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF -f "SUNWpsdt"
fi

if [ "$DEPLOY_TYPE" = "IWS" ]; then
  #
  # All below should be in an iWS multi instance script.
  #

  DIR="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/ClassCache/https-$DEPLOY_INSTANCE"
  $RM -rf $DIR/*

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

  UpdateServerClasspathWS "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/psimapprovider.jar"

elif [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  #
  # All below should be in a SUNONE multi instance script
  #

  FILE="$DEPLOY_DOMAIN/$DEPLOY_INSTANCE/config/server.policy"
  $GREP "permission java.io.FilePermission \"\${/}-\"" $FILE | $GREP "read,write,execute,delete" > /dev/null
  if [ $? -ne 0 ]; then
    $CAT >> $FILE << EOF
// S1PS
grant {
      permission java.util.PropertyPermission "*", "read,write";
      permission java.lang.RuntimePermission "writeFileDescriptor";
      permission java.lang.RuntimePermission "createClassLoader";
      permission java.io.FilePermission "\${/}-", "read,write,execute,delete";
};

EOF
  fi

  $DEPLOY_DIR/bin/asadmin set "$DEPLOY_INSTANCE.java-config.libpathprefix=.:$PS_BASEDIR/$PS_PRODUCT_DIR/lib" -u $DEPLOY_ADMIN -w "${DEPLOY_ADMIN_PASSWORD}" -p $DEPLOY_ADMIN_PORT

  $DEPLOY_DIR/bin/asadmin reconfig -u $DEPLOY_ADMIN -w "${DEPLOY_ADMIN_PASSWORD}" -p $DEPLOY_ADMIN_PORT $DEPLOY_INSTANCE

  UpdateServerClasspathAS "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/psimapprovider.jar"

  $DEPLOY_DIR/bin/asadmin reconfig -u $DEPLOY_ADMIN -w "${DEPLOY_ADMIN_PASSWORD}" -p $DEPLOY_ADMIN_PORT --keepmanualchanges=true $DEPLOY_INSTANCE

elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
  UpdateWebLogicLibraryPath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib:/usr/lib/lwp"
  UpdateWebLogicClassPath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/psimapprovider.jar"
elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  UpdateWebSphereLibraryPath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib:/usr/lib/lwp"
  UpdateWebSphereClassPath "$PS_BASEDIR/$PS_PRODUCT_DIR/lib/psimapprovider.jar"
fi

FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/index.html"
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF "SUNWps" $FILE
fi
$CP $FILE $FILE-tmp
$SED -e "s#%URL%#$DEPLOY_URI/dt#g" $FILE-tmp > $FILE
$RM $FILE-tmp
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF -f "SUNWps"
fi

exit 0
