#!/bin/ksh
# Copyright 2005 Sun Microsystems, Inc., All Rights Reserved.
#
# @(#)checkJESRunning.sh	1.7 05/10/13
#
# This software is the confidential and proprietary information of Sun
# Microsystems, Inc. ("Confidential Information"). You shall not
# disclose such Confidential Information and shall use it only in
# accordance with the terms of the license agreement you entered into
# with Sun.
#
# SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
# THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
# ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
# DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
#
# Exit Codes:
#   0 = success
#   2 = config/properties error (fatal)
#   3 = stop JES servers failure (fatal)
#   4 = start JES servers failure (fatal)
#   5 = invalid amAdmin password (could retry)
#   6 = not used
#   7 = not used
#   8 = JES was detected to not be installed properly/completely
#
###############################################
# Defines
###############################################

AWK=/usr/bin/awk
CAT=/usr/bin/cat
CP=/usr/bin/cp
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
LN=/usr/bin/ln
MV=/usr/bin/mv
NAWK=/usr/bin/nawk
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

exitStatus=0;
ESM_BASE="`pkginfo -r SUNWesmportal`"
CACAO_BASE="`pkginfo -r SUNWcacao`"
BASE_DIR="${ESM_BASE}/SUNWesmportal"
WEB_BASEDIR="`pkginfo -r SUNWwbsvr`"
BELL_CHAR='\a'
HOSTNAME="`uname -n`"
PS_FILE="PSConfig.properties"
STATE_FILE="/etc`pkginfo -r SUNWps`/SUNWps/${PS_FILE}"
CREATE_INFILE=${BASE_DIR}/profiles/createEsmPortal.xml
CREATE_CONVFILE=${BASE_DIR}/profiles/createEsmPortalConverted.xml
DP_INFILE=${BASE_DIR}/profiles/dp-esmportal.xml
DP_CONVFILE=${BASE_DIR}/profiles/dp-esmportalConverted.xml
DPA_INFILE=${BASE_DIR}/profiles/dp-esmadmin.xml
DPA_CONVFILE=${BASE_DIR}/profiles/dp-esmadminConverted.xml
DATESTAMP=`date +%m%d\%H\%M`
DESKTOP_BASE="/etc`pkginfo -r SUNWps`/SUNWps/desktop"

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

#
#   Prevent breaking out during portal config operations
#   Can leave the portal in an unusable state.
#
_backout () {
    # reset traps at this point - otherwise end up in a loop if
    # try to ^C out
    trap 2 3 15
    $ECHO "Please wait for all changes to complete."
}

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

# Need get_password () from esmlib.sh
. ${BASE_DIR}/esmlib.sh

#
# Must be root to perform these functions
#
if [ `$ID | $AWK '{print $1}'` != "uid=0(root)" ]; then
  $ECHO
  $ECHO "You must be root user. $BELL_CHAR"
  $ECHO
  exit 2
fi

#
# Need amAdmin password to perform these functions
#
if [ -z "$1" ]; then
    $ECHO "Must supply amAdmin password. $BELL_CHAR"
    get_password
else
    password="$1"
fi

#
# Verify JES Portal Servers are Installed
#
${BASE_DIR}/bin/checkjes3installed >/dev/null 2>&1
if [ $? = 1 ]; then
#    ${BASE_DIR}/bin/checkjes2installed
#    if [ $? = 1 ]; then
        $ECHO
        $ECHO " Java ES Portal is not completely/properly installed $BELL_CHAR"
        $ECHO
        exit 8
#    else
#        $ECHO "Detected Java ES 2 Portal Servers"
#    fi
else
    $ECHO "Detected Java ES 3 Portal Servers"
fi

#
# Need portal server config properties to perform these functions
#
if [ ! -f $STATE_FILE ]; then
  $ECHO
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  $ECHO
  exit 2
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
  $ECHO
  exit 2
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
  $ECHO
  exit 2
fi

#
# Need portal server config properties to perform these functions
#
GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_INSTANCE=$ANSWER
else
  $ECHO
  $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
  $ECHO
  exit 2
fi

AM_FILE="$IDSAME_BASEDIR/SUNWam/lib/AMConfig.properties"
ADMIN_DN=`$GREP "^com.sun.identity.authentication.super.user=" $AM_FILE | $SED -e "s/com.sun.identity.authentication.super.user=//"`
ROOT_DN=`$GREP "^com.iplanet.am.rootsuffix=" $AM_FILE | $SED -e "s/com.iplanet.am.rootsuffix=//"`
ORG_DN=`$GREP "^com.iplanet.am.defaultOrg=" $AM_FILE | $SED -e "s/com.iplanet.am.defaultOrg=//"`
HOST=`$GREP "^com.iplanet.am.directory.host=" $AM_FILE | $SED -e "s/com.iplanet.am.directory.host=//"`
if [ "$ORG_DN" != "$ROOT_DN" ]; then
  ORG_DN="$ORG_DN,$ROOT_DN"
fi
ESM_ORG_NAME="esmjss.com"
ORG_NAMING_ATTR="o"
ESM_ORG_DN="${ORG_NAMING_ATTR}=${ESM_ORG_NAME},${ORG_DN}"
SERVER_CONFIG="${WEB_BASEDIR}/https-${DEPLOY_INSTANCE}/config/server.xml"
SERVER_CONFIG_BK="${WEB_BASEDIR}/https-${DEPLOY_INSTANCE}/config/server.xml-orig.${DATESTAMP}"
SERVER_ADMIN="${WEB_BASEDIR}/https-admserv/config/magnus.conf"
SERVER_ADMIN_BK="${WEB_BASEDIR}/https-admserv/config/magnus.conf-orig.${DATESTAMP}"
SERVER_USER="${WEB_BASEDIR}/https-${DEPLOY_INSTANCE}/config/magnus.conf"
SERVER_USER_BK="${WEB_BASEDIR}/https-${DEPLOY_INSTANCE}/config/magnus.conf-orig.${DATESTAMP}"
AM_FILE_BK="$IDSAME_BASEDIR/SUNWam/lib/AMConfig.properties.${DATESTAMP}"

if [ -z "${ADMIN_DN}" ] || [ -z "${ROOT_DN}" ] || [ -z "${ORG_DN}" ] || [ -z "${HOST}" ] || [ -z "${JSSTAG1}" ] || [ -z "${JCETAG1}" ] || [ -z "${JSSTAG2}" ] || [ -z "${JCETAG2}" ] || [ -z "${JSSTAG3}" ] || [ -z "${JCETAG3}" ] || [ -z "${TAG}" ] || [ -z "${HEADLESS}" ] || [ -z "${LOGLINE}" ] || [ -z "${CLASSPATH}" ] || [ -z "${CLASSPATHADD}" ] || [ -z "${DOCSOURCE}" ] || [ -z "${JVM_MS}" ] || [ -z "${JVM_MXPERM}" ] || [ -z "${JVM_MXSIZE}" ] || [ -z "${JVM_MXSIZE_DEF}" ] || [ -z "${BRANDING}" ] || [ -z "${AM_COOKIE_NAME}" ] || [ -z "${AM_SESSION}" ] || [ -z "${DEPLOY_INSTANCE}" ] || [ -z "${DEFAULTUSER}" ] || [ -z "${WEBSERVD}" ]; then 
  $ECHO
  $ECHO "Parse Config Properties ERROR. $BELL_CHAR"
  $ECHO "One or more of the following properties are NULL"
  $ECHO
  $ECHO "admin_dn     = ${ADMIN_DN}"
  $ECHO "root_dn      = ${ROOT_DN}"
  $ECHO "org_dn       = ${ORG_DN}"
  $ECHO "host         = ${HOST}"
  $ECHO "jsstag1      = ${JSSTAG1}"
  $ECHO "jcetag1      = ${JCETAG1}"
  $ECHO "jsstag2      = ${JSSTAG2}" 
  $ECHO "jcetag2      = ${JCETAG2}" 
  $ECHO "jsstag3      = ${JSSTAG3}" 
  $ECHO "jcetag3      = ${JCETAG3}" 
  $ECHO "am_cookie_name = ${AM_COOKIE_NAME}" 
  $ECHO "am_session   = ${AM_SESSION}" 
  $ECHO "deploy_instance = ${DEPLOY_INSTANCE}" 
  $ECHO "tag          = ${TAG}" 
  $ECHO "headless     = ${HEADLESS}"
  $ECHO "logline      = ${LOGLINE}"
  $ECHO "classpath    = ${CLASSPATH}"
  $ECHO "classpathadd = ${CLASSPATHADD}"
  $ECHO "docsource    = ${DOCSOURCE}"
  $ECHO "jvm_ms       = ${JVM_MS}"
  $ECHO "jvm_mxperm   = ${JVM_MXPERM}"
  $ECHO "jvm_mxsize   = ${JVM_MXSIZE}"
  $ECHO "jvm_mxsize_def   = ${JVM_MXSIZE_DEF}"
  $ECHO "branding     = ${BRANDING}"
  $ECHO "defaultuser  = ${DEFAULTUSER}"
  $ECHO "webservd     = ${WEBSERVD}"
  exit 2
fi

#
#DEBUG
#
#$ECHO
#$ECHO "STATE_FILE:\t${STATE_FILE}"
#$ECHO "JDK_DIR:\t${JDK_DIR}"
#$ECHO "IDSAME_BASEDIR:\t${IDSAME_BASEDIR}"
#$ECHO "PS_BASEDIR:\t${PS_BASEDIR}"
#$ECHO "ADMIN_DN:\t${ADMIN_DN}"
#$ECHO "ROOT_DN:\t${ROOT_DN}"
#$ECHO "ORG_DN:\t\t${ORG_DN}"
#$ECHO "ESM_ORG_DN:\t\t${ESM_ORG_DN}"
#$ECHO "HOST:\t\t${HOST}"
#$ECHO "BASE_DIR:\t\t${BASE_DI}"
#$ECHO

#
# From here on out we need no interuptions.
#
trap 2 3 15

#
# Makes sure portal servers are up
#
# check each of the servers - if any one is down restart servers
#${BASE_DIR}/bin/checkservers -wid >/dev/null 2>&1
${BASE_DIR}/bin/checkservers -wd >/dev/null 2>&1
if [ $? -ne 0 ]; then
    ${BASE_DIR}/bin/stopservers
    ${BASE_DIR}/bin/checkservers -w >/dev/null 2>&1
    if [ $? -ne 1 ]; then
        $ECHO "ERROR: Cannot access desktop could not stop Web Server"
        $ECHO "       Verify that All Portal Servers are running and responsive"
        exit 3
    fi
#    ${BASE_DIR}/bin/checkservers -i >/dev/null 2>&1
#    if [ $? -ne 1 ]; then
#        $ECHO "ERROR: Cannot access desktop could not stop Access Manager Server"
#        $ECHO "       Verify that All Portal Servers are running and responsive"
#        exit 3
#    fi
    ${BASE_DIR}/bin/checkservers -d >/dev/null 2>&1
    if [ $? -ne 1 ]; then
        $ECHO "ERROR: Cannot access desktop could not stop Directory Server"
        $ECHO "       Verify that All Portal Servers are running and responsive"
        exit 3
    fi
    ${BASE_DIR}/bin/startservers
#    ${BASE_DIR}/bin/checkservers -wid >/dev/null 2>&1
    ${BASE_DIR}/bin/checkservers -wd >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        $ECHO "ERROR: Cannot access desktop could not start Portal Servers"
        $ECHO "       Verify that All Portal Servers are running and responsive"
        exit 4
    fi
fi

# Verify amadmin password is valid
$PS_BASEDIR/SUNWps/bin/dpadmin list -n "JSPProvider" -u "$ADMIN_DN" -w "$password" -g >/dev/null 2>&1
if [ $? = 0 ]; then
    $ECHO "Detected Running Portal Servers"
else
    # attempt to restart portal servers before giving up.
    ${BASE_DIR}/bin/stopservers
    ${BASE_DIR}/bin/checkservers -w >/dev/null 2>&1
    if [ $? -ne 1 ]; then
        $ECHO "ERROR: Cannot access desktop could not stop Web Server"
        $ECHO "       Verify that All Portal Servers are running and responsive"
        exit 3
    fi
#    ${BASE_DIR}/bin/checkservers -i >/dev/null 2>&1
#    if [ $? -ne 1 ]; then
#        $ECHO "ERROR: Cannot access desktop could not stop Access Manager Server"
#        $ECHO "       Verify that All Portal Servers are running and responsive"
#        exit 3
#    fi
    ${BASE_DIR}/bin/checkservers -d >/dev/null 2>&1
    if [ $? -ne 1 ]; then
        $ECHO "ERROR: Cannot access desktop could not stop Directory Server"
        $ECHO "       Verify that All Portal Servers are running and responsive"
        exit 3
    fi
    ${BASE_DIR}/bin/startservers
#    ${BASE_DIR}/bin/checkservers -wid >/dev/null 2>&1
    ${BASE_DIR}/bin/checkservers -wd >/dev/null 2>&1
    if [ $? = 0 ]; then
        $PS_BASEDIR/SUNWps/bin/dpadmin list -n "JSPProvider" -u "$ADMIN_DN" -w "$password" -g >/dev/null 2>&1
        if [ $? = 0 ]; then
            $ECHO "Detected Running Portal Servers"
        else
            $ECHO "ERROR: Cannot access desktop amAdmin password is invalid"
            $ECHO "       Verify that All Portal Servers are running and responsive"
            $ECHO "       Verify that the supplied password is valid"
            exit 5
        fi
    else
        $ECHO "ERROR: Cannot access desktop could not start Portal Servers"
        $ECHO "       Verify that All Portal Servers are running and responsive"
        exit 4
    fi
fi

exit $exitStatus
