#!/bin/sh
#
# Copyright (c) 2004 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#

#set -x

#########################################
# Debug
##########################################
debug() {
    if [ "x"$VERBOSE != "x" ] ; then
	echo $1
    fi
}

#########################################
# getPkgBase
#
# sets PKGNAME variable 
##########################################
get_pkgname() {
    debug "get_pkgname $1"
    PKG=$1
    pkginfo -R $ROOTDIR $PKG.\* 1>/dev/null 2>&1
    if [ $? -ne 0 ]; then
	debug "No package $1 installed"
	return 1
    else
	PKGNAME=`pkginfo -R ${ROOTDIR} $1.\* | awk '{print $2}'`
    fi
    debug "get_pkgname returns $PKGNAME"
    return 0
}


#########################################
# pkg_get_basedir
#
# set PKGBASEDIR variable 
##########################################
pkg_get_basedir() {
    debug "get_basedir $1"    
    get_pkgname $1
    if [ $? -ne 0 ]; then
	return 1
    else
	LOCAL_BASEDIR=`pkginfo -R ${ROOTDIR} -r $PKGNAME`
	if [ ${ROOTDIR} != '/' ] ; then
	    PKGBASEDIR=${ROOTDIR}/${LOCAL_BASEDIR}
	else
	    PKGBASEDIR=${LOCAL_BASEDIR}
	fi
    fi
    debug "get_basedir returns $PKGBASEDIR"
    return 0
}

#########################################
# stops Admin server instances 
##########################################
stop_server() {
stop_patch=no
cmd_torun_admin=

# admin server instance under the server root must be stopped.


AS_BASEDIR=${ROOTDIR}/`pkginfo -R ${ROOTDIR} -r SUNWasvu`
# BugId 4943278: Error 'cat: cannot open /[...]/serverroot.conf' when patchadd 115614-01
#    add existence file test
if [ -f ${AS_BASEDIR}/etc/mps/admin/v5.2/shared/config/serverroot.conf ] ; then
    SERVER_ROOT=${ROOTDIR}/`cat ${AS_BASEDIR}/etc/mps/admin/v5.2/shared/config/serverroot.conf`
fi

if [ -f ${SERVER_ROOT}/admin-serv/logs/pid  ]
then
    stop_patch=yes
	TMP_BASE_DIR=`pkginfo -R ${ROOTDIR} -r SUNWasvu`
    cmd_torun_admin="${TMP_BASE_DIR}/usr/sbin/mpsadmserver stop\n"
else
   return 0
fi

if [ "x${ROOTDIR}" = x ] || [ "x${ROOTDIR}" = "x/" ]
    then
	echo "Prepatch script is stopping Administration Server..."
	USER=`/usr/bin/ls -l ${SERVER_ROOT}/admin-serv/logs/pid | awk '{print $3}'`
	echo ${cmd_torun_admin} | sh
	if [ $? -ne 0 ]
	then
		echo "Prepatch script failed to stop Administration Server." 
		return 1
	fi
	PATHNUM=`env | grep PatchNum`
	echo $USER > /var/tmp/.shouldRestartAS_prepatch_${PATHNUM}
	echo "Administration Server stopped."
	return 0
    else
	echo "Prepatch script cannot stop Administration Server when option -R is used."
	echo "Log in to the system where Administration Server is running"
	echo "and run the following command:"
	echo "${cmd_torun_admin}" | sed "s,//*,/,g"
	return 1  
fi
}

#########################################
# security_version_check 
# precondition to patch the system: having NSS3.3.6 or newer version 
# installed on the system. 
# Our test consist in the checking the presence of the librairy nss3.so in 
# <SUNWtls base dir>/usr/lib/mps/secv1/libnss3.so
#
# Returns 0 libnss3.so from at least NSS3.3.6 found
#         1 libnss3.so from at least NSS3.3.6  NOT found
##########################################
security_version_check () {

   rc=1

   pkg_get_basedir SUNWtls
   if [ $? -eq 0 ]; then

     TLS_LIBDIR=$PKGBASEDIR/usr/lib/mps/secv1  
     NSS3_LIB=$TLS_LIBDIR/libnss3.so

     if [ -f $NSS3_LIB ]; then
       rc=0
     fi 
   fi

   return $rc
}

#########################################
# main 
##########################################


# At least NSS3.3.6 version must be installed on the system. 
security_version_check
if [ $? -ne 0 ]; then
    echo "NSS version 3.3.6 or later must be installed on the system."
    echo "Abandoning patch addition..."
    exit 1
fi

# Server must be stopped before patching. 
stop_server
if [ $? -ne 0 ]; then
    exit 1
fi

exit 0

