#!/bin/ksh

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

CAT=/usr/bin/cat
CP=/usr/bin/cp
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
NAWK=/usr/bin/nawk
RM=/usr/bin/rm
SED=/usr/bin/sed
STTY=/usr/bin/stty
UNIQ=/usr/bin/uniq

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  CAT=/bin/cat
  CP=/bin/cp
  ECHO=/bin/echo
  GREP=/bin/grep
  NAWK=/bin/gawk
  RM=/bin/rm
  SED=/bin/sed
  STTY=/bin/stty
fi

BELL_CHAR='\a'
OMIT_CHAR='\c'

STATE_FILE="/etc/opt/SUNWps/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//"`
}

###############################################
# 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
    $ECHO ""
    if [ "$ANSWER" != "" ]; then
      DONE="y"
    fi
  done
}

###############################################
# 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 "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 "DEPLOY_TYPE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_TYPE=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
  exit 1
fi

if [ "${IDSAME_ADMIN_PASSWORD}" = "" ]; then
  GetPassword "What is the Identity Server administration password?"
  IDSAME_ADMIN_PASSWORD=$ANSWER
fi

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

PORTLETDEPLOYER="$PS_BASEDIR/SUNWps/bin/pdeploy"

FILE="/etc/opt/SUNWam/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

if [ -x $PORTLETDEPLOYER ]; then
  FILE="/etc/opt/SUNWps/portlet/userInfoMapping.properties"
  $ECHO "Loading $FILE."
  eval $PORTLETDEPLOYER deploy -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -p "${DEPLOY_ADMIN_PASSWORD}" -g -f $FILE "$PS_BASEDIR/SUNWps/samples/portlet/original/portletsamples.war"
fi

exit 0
