#!/bin/ksh

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

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

BELL_CHAR='\a'

ORIG_IFS=$IFS

LOG_DIR="$IDSAME_VAR_DIR/debug"
LOG_FILE="$LOG_DIR/undeploy.log"

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"

VAROPT_TMP_FILE="$PS_VAR_DIR/tmp/deploy/.amconfig"
DEPLOY_TMP_DIR="$PS_VAR_DIR/tmp/deploy"

###############################################
# Usage
###############################################
Usage() {
  $ECHO "undeploy"
  $ECHO ""
  $ECHO "  Command line options:"
  $ECHO "    help"
  $ECHO "  Command line options:"
  $ECHO "    undeploy"
  $ECHO "    -deploy_admin_password <password>"
  $ECHO "    {-instance <instance>}"
}

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

###############################################
# Question
###############################################
Question() {
  local PROMPT=$1
  local DEFAULT=$2
  local VALID_CHARS=$3
  local DONE=""
  local CHECK=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    if [ "$DEFAULT" != "" ]; then
      $ECHO "$PROMPT [$DEFAULT] $OMIT_CHAR"
    else
      $ECHO "$PROMPT $OMIT_CHAR"
    fi
    read ANSWER
    if [ "$ANSWER" = "" ]; then
      ANSWER=$DEFAULT
      DONE="y"
    else
      CHECK=`$ECHO $ANSWER | $GREP "[^$VALID_CHARS]"`
      if [ "$CHECK" = "" ]; then
        DONE="y"
      else
        $ECHO "Invalid answer! $BELL_CHAR"
      fi
    fi
  done
}

###############################################
# Get password
###############################################
GetPassword() {
  local PROMPT=$1
  local DONE=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    eval print "$PROMPT \$OMIT_CHAR"
    $STTY -echo
    read ANSWER
    $STTY echo
    if [ "$ANSWER" != "" ]; then
      DONE="y"
    fi
  done
}

###############################################
# Set DEPLOY_INSTANCES
###############################################
SetDeployInstanceList() {
  local INSTANCE=""
  local INSTANCES=""
  local ADMIN_INSTANCE=""
  local TMP_INSTANCES=""

  if [ "$DEPLOY_INSTANCE" != "" ]; then
    DEPLOY_INSTANCES="$DEPLOY_INSTANCE"
  else
    if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
      ADMIN_INSTANCE="admin-server"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DOMAIN/* 2>&1`
      if [ $? -eq 0 ]; then
	for INSTANCE in $TMP_INSTANCES; do
	  if [ -z "$INSTANCES" ]; then
        INSTANCES="$INSTANCE"
      else
        INSTANCES="$INSTANCE,$INSTANCES"
      fi
	done
      fi
    elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
	GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
	INSTANCES=$ANSWER
      fi
    elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
	GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
	INSTANCES=$ANSWER
      fi
   elif [ "$DEPLOY_TYPE" = "IWS" ]; then
      ADMIN_INSTANCE="admserv"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DIR/https-* 2>&1`
      if [ $? -eq 0 ]; then
	for INSTANCE in $TMP_INSTANCES; do
	  if [ -z "$INSTANCES" ]; then
        INSTANCES="$INSTANCE"
      else
        INSTANCES="$INSTANCE,$INSTANCES"
      fi
	done
      fi
    fi

    DEPLOY_INSTANCES=""
    IFS=","
    for INSTANCE in $INSTANCES; do
      INSTANCE=`$BASENAME $INSTANCE | $SED -e "s/https-//"`
      if [ "$INSTANCE" != "$ADMIN_INSTANCE" ]; then
	    if [ -z "$DEPLOY_INSTANCES" ]; then
          DEPLOY_INSTANCES="$INSTANCE"
        else
          DEPLOY_INSTANCES="$INSTANCE,$DEPLOY_INSTANCES"
        fi
      fi
    done
    IFS=$ORIG_IFS
  fi
}

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

if [ "$1" = "help" ]; then
  Usage
  exit 0
fi

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 "Cannot find $STATE_FILE. $BELL_CHAR"
  exit 1
fi

GrabConfig $STATE_FILE "JDK_DIR" "="
if [ "$ANSWER" != "" ]; then
  JDK_DIR=$ANSWER
else
  $ECHO "Cannot determine JDK_DIR. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_TYPE=$ANSWER
else
  $ECHO "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 "Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Cannot determine DEPLOY_DOMAIN. $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_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" = "WEBLOGIC" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Cannot determine DEPLOY_DOMAIN. $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
  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_PRODUCT_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_PRODUCT_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_PRODUCT_DIR. $BELL_CHAR"
    DEPLOY_PRODUCT_DIR=$DEPLOY_DIR/weblogic81
  fi
  GrabConfig $STATE_FILE "DEPLOY_PROJECT_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_PROJECT_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_PROJECT_DIR. $BELL_CHAR"
    DEPLOY_PROJECT_DIR="user_projects"
  fi

elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_VIRTUAL_HOST" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_VIRTUAL_HOST=$ANSWER
  else
    $ECHO "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
  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_JDK_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_JDK_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_JDK_DIR. $BELL_CHAR"
    DEPLOY_JDK_DIR=$DEPLOY_DIR/java 
  fi
else
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
fi
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO "Cannot determine Identity Server base directory. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Cannot determine Portal Server base directory. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_PROTOCOL" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PROTOCOL=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_PROTOCOL. $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 "SERVER_PORT" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_PORT. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "PS_PROTOCOL" "="
if [ "$ANSWER" != "" ]; then
  PS_PROTOCOL=$ANSWER
else
  $ECHO "Error: Cannot determine PS_PROTOCOL. $BELL_CHAR"
  PS_PROTOCOL=$SERVER_PROTOCOL
fi
GrabConfig $STATE_FILE "PS_HOST" "="
if [ "$ANSWER" != "" ]; then
  PS_HOST=$ANSWER
else
  $ECHO "Error: Cannot determine PS_HOST. $BELL_CHAR"
  PS_HOST=$SERVER_HOST
fi
GrabConfig $STATE_FILE "PS_PORT" "="
if [ "$ANSWER" != "" ]; then
  PS_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine PS_PORT. $BELL_CHAR"
  PS_PORT=$SERVER_PORT
fi
GrabConfig $STATE_FILE "DEPLOY_URI" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_URI=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_URI. $BELL_CHAR"
  exit 1
fi

if [ "$1" = "undeploy" ]; then
  shift
  while [ "$1" != "" ]; do
    if [ "$1" = "-instance" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -instance. $BELL_CHAR"
	Usage
	exit 1
      else
	DEPLOY_INSTANCE=$2      
	shift; shift
      fi
    elif [ "$1" = "-deploy_admin_password" ]; then
      if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
	$ECHO "Error: Missing value for -deploy_password. $BELL_CHAR"
	Usage
	exit 1
      else
	DEPLOY_ADMIN_PASSWORD=$2      
	shift; shift
      fi
    else
      $ECHO "Error: Unknown parameter $1. $BELL_CHAR"
      Usage
      exit 1
    fi
  done

  SetDeployInstanceList
elif [ "$1" = "" ]; then
  Question "Undeploy which instance [Leave blank for all]?" "" "a-zA-Z0-9 /\._-"
  DEPLOY_INSTANCE=$ANSWER

  if [ "$DEPLOY_TYPE" = "SUNONE" ] || [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    GetPassword "What is the Application Server administration password?"
    DEPLOY_ADMIN_PASSWORD=$ANSWER
  fi

  SetDeployInstanceList
else
  Usage
  exit 1
fi

if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  DEPLOY="$DEPLOY_DIR/bin/asadmin"
  if [ ! -x $DEPLOY ]; then
    $ECHO "Cannot find $DEPLOY. $BELL_CHAR"
    exit 1
  fi
elif [ "$DEPLOY_TYPE" = "IWS" ]; then
  IWS_SERVER_HOME="$DEPLOY_DIR"
  export IWS_SERVER_HOME

  DEPLOY="$IWS_SERVER_HOME/bin/https/httpadmin/bin/wdeploy"
  if [ ! -x $DEPLOY ]; then
    $ECHO "Cannot find $DEPLOY. $BELL_CHAR"
    exit 1
  fi
fi

$ECHO "" >> $LOG_FILE
$ECHO `$DATE +%D` "at" `$DATE +%r` >> $LOG_FILE
$ECHO "#########################################################" >> $LOG_FILE

IFS=","
CURRENT_DIR=`pwd`

cd $IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin
for DEPLOY_INSTANCE in $DEPLOY_INSTANCES; do
  $ECHO "Undeploying from instance $DEPLOY_INSTANCE..."

  if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    PARAMS="undeploy -u $DEPLOY_ADMIN -w $DEPLOY_ADMIN_PASSWORD -p $DEPLOY_ADMIN_PORT -H $DEPLOY_ADMIN_HOST --instance $DEPLOY_INSTANCE --type web portal"
    $ECHO "Calling $DEPLOY $PARAMS" | $SED -e "s/-w $DEPLOY_ADMIN_PASSWORD/-w ********/" >> $LOG_FILE
    eval $DEPLOY "$PARAMS" >> $LOG_FILE 2>&1
    if [ $? -ne 0 ]; then
      $ECHO "Possible error: See $LOG_FILE."
    fi
  elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    URI=`$ECHO $DEPLOY_URI | $SED -e "s.^/.."`
    PARAMS="-classpath $DEPLOY_PRODUCT_DIR/server/lib/weblogic.jar weblogic.Deployer -adminurl $DEPLOY_ADMIN_PROTOCOL://$DEPLOY_ADMIN_HOST:$DEPLOY_ADMIN_PORT -username $DEPLOY_ADMIN -password ${DEPLOY_ADMIN_PASSWORD} -name $URI -undeploy"
    $ECHO "Calling $JDK_DIR/bin/java $PARAMS" | $SED -e "s/ $DEPLOY_ADMIN_PASSWORD / ******** /" >> $LOG_FILE
    eval $JDK_DIR/bin/java "$PARAMS" >> $LOG_FILE 2>&1
  elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
    # Generate the undeploy.jacl file
    $ECHO "\$AdminApp uninstall portal_war {-node $DEPLOY_NODE -cell $DEPLOY_CELL -server $DEPLOY_INSTANCE}" > $DEPLOY_TMP_DIR/undeploy.jacl
    $ECHO "\$AdminConfig save" >> $DEPLOY_TMP_DIR/undeploy.jacl

    # Execute it using the wsadmin
    eval $DEPLOY_DIR/bin/wsadmin.sh -f $DEPLOY_TMP_DIR/undeploy.jacl >> $LOG_FILE 2>&1
  elif [ "$DEPLOY_TYPE" = "IWS" ]; then
    PARAMS="delete -u $DEPLOY_URI -i $DEPLOY_INSTANCE -v "https-$DEPLOY_INSTANCE" hard"
    $ECHO "Calling $DEPLOY $PARAMS" >> $LOG_FILE
    eval $DEPLOY "$PARAMS" >> $LOG_FILE 2>&1
    if [ $? -ne 0 ]; then
      $ECHO "Possible error: See $LOG_FILE."
    fi
  fi
done
IFS=$ORIG_IFS
cd $CURRENT_DIR

exit 0
