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

###############################################
# Remove package
###############################################
PackageRemove() {
  local WORK_DIR=$1
  local PACKAGES=$2
  local ADMIN_FILE=""
  local PACKAGE=""

  ADMIN_FILE="$WORK_DIR/package_remove_admin"
  print "action=nocheck" >> $ADMIN_FILE
  print "idepend=nocheck" >> $ADMIN_FILE
  print "rdepend=nocheck" >> $ADMIN_FILE
  print "mail=" >> $ADMIN_FILE

  for PACKAGE in $PACKAGES; do
    $PKGINFO -q $PACKAGE
    if [ $? -eq 0 ]; then
      $ECHO "Removing $PACKAGE..."
      $PKGRM -A -n -a $ADMIN_FILE $PACKAGE > /dev/null 2>&1
    else
      $ECHO "$PACKAGE was not installed."
    fi
    $RM -rf /var/sadm/pkg/$PACKAGE
    $RM -rf /var/sadm/pkg/.*$PACKAGE
    $RM -f /var/sadm/install/logs/$PACKAGE
    $RM -f /var/sadm/install/.pkg.lock
    $RM -f /var/sadm/install/.lockfile
  done
}

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

if [ `$ID | $AWK '{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 "JDK_DIR" "="
if [ "$ANSWER" != "" ]; then
  JDK_DIR=$ANSWER
else
  $ECHO "Error: Cannot determine JDK_DIR. $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_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_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Error: 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
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
else
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
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_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
  SERVER_HOSTNAME=${SERVER_HOST%%.*}
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

DEPLOY_ADMIN_PASSWORD=""
IS_ADMIN_PASSWORD=""
PACKAGES=""

while [ "$1" != "" ]; do
  if [ "$1" = "-deploy_admin_password" ]; then
    if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
      $ECHO "Error: Missing value for -deploy_admin_password. $BELL_CHAR"
      exit 1
    else
      DEPLOY_ADMIN_PASSWORD=$2      
      shift; shift
    fi
  elif [ "$1" = "-is_admin_password" ]; then
    if [ "$2" = "" ] || [ `$ECHO $2 | $CUT -c1` = '-' ]; then
      $ECHO "Error: Missing value for -is_admin_password. $BELL_CHAR"
      exit 1
    else
      IS_ADMIN_PASSWORD=$2      
      shift; shift
    fi
  else
    PACKAGES="$PACKAGES $1"
    shift
  fi
done

if [ "$PACKAGES" = "" ]; then
  $ECHO "No packages specified. $BELL_CHAR"
  exit 1
fi

PORTLET_APP_NAME="portletsamples"

WARFILE_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/portlet/original"
WARFILE="$PORTLET_APP_NAME.war"

if [ ! -f $WARFILE_DIR/$WARFILE ]; then
  $ECHO "$WARFILE_DIR/$WARFILE not found. $BELL_CHAR"
  exit 1
fi

$ECHO "Preparing $WARFILE..."

$CP $WARFILE_DIR/$WARFILE $WARFILE_DIR/$WARFILE.save

PATCH_DIR="$PS_VAR_DIR/tmp/portlet/patch"
WORK_DIR="$PS_VAR_DIR/tmp/portlet/patch/work"

$MKDIR -p $WORK_DIR
$MV $WARFILE_DIR/$WARFILE $WORK_DIR

CURRENT_DIR="$PWD"
cd $WORK_DIR

$JDK_DIR/bin/jar xf $WARFILE
$RM $WARFILE

cd $CURRENT_DIR

PackageRemove $PATCH_DIR "$PACKAGES"

$ECHO "Preparing $WARFILE..."

CURRENT_DIR="$PWD"
cd $WORK_DIR

$JDK_DIR/bin/jar cf $WARFILE_DIR/$WARFILE *

cd $CURRENT_DIR

$RM -rf $PATCH_DIR

FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"
ADMIN_DN=`$GREP "^com.sun.identity.authentication.super.user=" $FILE | $SED -e "s/com.sun.identity.authentication.super.user=//"`
ROOT_DN=`$GREP "^com.iplanet.am.rootsuffix=" $FILE | $SED -e "s/com.iplanet.am.rootsuffix=//"`
ORG_DN=`$GREP "^com.iplanet.am.defaultOrg=" $FILE | $SED -e "s/com.iplanet.am.defaultOrg=//"`
if [ "$ORG_DN" != "$ROOT_DN" ]; then
  ORG_DN="$ORG_DN,$ROOT_DN"
fi

$ECHO "Undeploying $PORTLET_APP_NAME..."
$PS_BASEDIR/$PS_PRODUCT_DIR/bin/pdeploy undeploy -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -g -p "$DEPLOY_ADMIN_PASSWORD" $PORTLET_APP_NAME

$ECHO "Deploying $WARFILE..."
FILE="$PS_CONFIG_DIR/portlet/userInfoMapping.properties"
$PS_BASEDIR/$PS_PRODUCT_DIR/bin/pdeploy deploy -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -g -p "$DEPLOY_ADMIN_PASSWORD" -g -f $FILE "$WARFILE_DIR/$WARFILE"
exit $?
