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

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

###############################################
# Delete block until blank line
###############################################
DeleteUntilBlankLine() {
  local FILE=$1
  local START_LINE=$2
                                                                                                                     
  $CP $FILE $FILE-tmp
  $NAWK 'BEGIN {
           IN_REPLACE = 0;
         };
         {
           if (index($0, START_LINE) > 0) {
             IN_REPLACE = 1;
           } else if (length($0) == 0) {
             IN_REPLACE = 0;
           }
           if (IN_REPLACE == 0) {
             printf("%s\n", $0);
           }
         };
         END {};' START_LINE="$START_LINE" $FILE-tmp > $FILE
                                                                                                                     
  $RM $FILE-tmp
}

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

if [ "$DEPLOY_TYPE" = "IWS" ]; then
  DIR="$DEPLOY_DIR/https-$DEPLOY_INSTANCE/ClassCache/https-$DEPLOY_INSTANCE"
  $RM -rf $DIR/*

  $RM -f $DEPLOY_DIR/bin/https/lib/libdb*.so
elif [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  $RM -f $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib/libdb*.so
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
    FILE="$DEPLOY_DOMAIN/$DEPLOY_INSTANCE/config/server.policy"
    DeleteUntilBlankLine $FILE "// S1PS"
  else
    $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
    exit 1
  fi
elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  FILE="$DEPLOY_DIR/bin/setupCmdLine.sh"
  DeleteLine $FILE "$PS_PRODUCT_DIR\/lib:"
  DeleteLine $FILE "export LD_LIBRARY_PATH"
fi

DIRS=`$LS -d $PS_VAR_DIR/https-* 2>&1`
if [ $? -eq 0 ]; then
  for DIR in $DIRS; do
    SERVER_ROOT=`$ECHO $DIR/$DEPLOY_URI | $SED -e "s.//./.g"`
    if [ -f $SERVER_ROOT/run-cs-cli ]; then
      export SERVER_ROOT
      (cd $SERVER_ROOT; ./run-cs-cli pscronctl stop > /dev/null 2>&1; ./StopRobot > /dev/null 2>&1)
    fi
    SAVE_DIR=`$ECHO $DIR | $SED -e "s/https-/SAVE-https-/"`
    if [ -d $SAVE_DIR ]; then
      $RM -rf $SAVE_DIR
    fi
    $MV $DIR $SAVE_DIR
  done
fi

$RM -rf $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/*
$RM -rf $PS_BASEDIR/$PS_PRODUCT_DIR/lib/filter
$RM -rf $PS_VAR_DIR/tmp

exit 0
