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




###############################################
# 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 "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_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
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 "DEPLOY_DOCROOT" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_DOCROOT=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_DOCROOT. $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

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

DPADMIN=$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin


#
# Update desktop help
#
DESKTOP_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/docs/en/desktop"
if [ -d $DESKTOP_DIR.save_by_wireless ]; then
  $RM -f $DESKTOP_DIR
  $MV $DESKTOP_DIR.save_by_wireless $DESKTOP_DIR

  $RM -rf $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/docs/desktop
  $CP -r $DESKTOP_DIR $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/docs
fi


#
# Remove common MA components (from SUNWma) from PS staging area...
#
ALIGO_JARFILES="wireless_rendering.jar wireless_rendering_util.jar"
FEDI_JARFILES="ccpp-1_0.jar ccpp-ri-1_0.jar jena-1.4.0.jar rdffilter.jar"
COMMON_JARFILES="mobile_services.jar $ALIGO_JARFILES $FEDI_JARFILES"

for JARFILE in $COMMON_JARFILES
do
  $RM -f $PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/$JARFILE
done

#
# Perform web container specific un-config
#
if [ "$DEPLOY_TYPE" == "IWS" ]; then

elif [ "$DEPLOY_TYPE" = "SUNONE" ]; then

fi

$RM -rf $DEPLOY_DOCROOT/voice

exit 0
