#!/bin/sh
#
# Copyright (c) 2002-2004 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#ident "@(#)prepatch 1.0 - 02/10/23"

# Check to see if the admin and directory server are running or configured. 
#

stop_patch=no
cmd_torun_admin=

# Admin server first

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

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
}

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
}

#########################################
# security_version_check
# precondition to patch the system: having NSS3.3.6 or newer version
# installed on the system.
# The test consists in the checking the presence of the library 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

   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
}


#####################################
#
# Check that NSS3.3.6 security libraries
# have been installed
#
#####################################

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

#####################################
#
# Setup the variables for AS and DS 
#
#####################################

# For AS

get_basedir SUNWasvu
AS_BASEDIR=${PKGBASEDIR}
AS_LOCAL_BASEDIR=${LOCAL_BASEDIR}

# For DS

get_basedir SUNWdsvu
DS_BASEDIR=${PKGBASEDIR}
DS_LOCAL_BASEDIR=${LOCAL_BASEDIR}

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

#####################################
#
# stop the ldap instances
#
#####################################

PATHNUM=`env | grep PatchNum`
stop_patch=no
cmd_torun_ds=
instance_list=`ls -d ${SERVER_ROOT}/slapd-* 2>/dev/null`
if [ ! "x${instance_list}" = "x" ]
then
    for i in `ls -d ${SERVER_ROOT}/slapd-*`
    do
	servername=`/bin/basename $i | /bin/awk '{ print substr($0,7,length($0)-6) }'` 
	if [ -f /var/tmp/.shouldRestart_DS_${servername}_${PATHNUM} ]; then
		rm -f /var/tmp/.shouldRestart_DS_${servername}_${PATHNUM}
	fi
	if [ -f ${i}/logs/pid ]
	then
            if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
    	    then
           	echo "Prepatch script is stopping Directory Server instance ${i}..."
		cd ${DS_LOCAL_BASEDIR}/usr/ds/v5.2/sbin
		eval ./directoryserver -s ${servername} stop
		if [ $? -eq 1 ];then
			echo "Directory Server instance could not be stopped"
			exit 1
		else
			echo "Directory Server instance stopped"
		fi
        	touch /var/tmp/.shouldRestart_DS_${servername}_${PATHNUM}
            else
		stop_patch=yes
		cmd_torun_ds="${cmd_torun_ds} ${DS_LOCAL_BASEDIR}/usr/ds/v5.2/sbin/directoryserver -s ${servername} stop\n"
    	    fi
	fi
    done
fi

if [ ${stop_patch} = yes ]
then
	echo "You must stop the servers before applying this patch."
	echo "Log in to the system where this patch is to be"
	echo "installed and run the following commands:"
	echo ${cmd_torun_admin}
	echo ${cmd_torun_ds}
	exit 1
fi


get_basedir SUNWdsvu
ds52_basedir=$PKGBASEDIR

over52() {
    debug "over52"
}

over51() {
    debug "over51"
    if [ ! -d $ds52_basedir/usr/iplanet/ds5/sbin ] ; then
	mkdir $ds52_basedir/usr/iplanet/ds5/sbin
    fi
    if [ -f $ds52_basedir/usr/sbin/directoryserver -a ! -f $ds52_basedir/usr/iplanet/ds5/sbin/directoryserver ] ; then 
	debug "Moving $ds52_basedir/usr/sbin/directoryserver in DS 5.1 tree"
	mv $ds52_basedir/usr/sbin/directoryserver $ds52_basedir/usr/iplanet/ds5/sbin/directoryserver    
    fi
}

over51p(){
    debug "over51p"
}

if [ `uname -i` = "i86pc" ] ; then
    DS51_PATCHID="114273"
    DS51_MIN_PATCHREV="04"
else
    DS51_PATCHID="113859"
    DS51_MIN_PATCHREV="04"
fi

is_51_patch(){
    debug "is_51_patch"
    current_patch=`pkgparam -R $ROOTDIR $IPLTDSU_PKG PATCHLIST`
    if [ "x$current_patch" != "x" ]; then
	for i in `echo $current_patch`
	  do
	  check_ret=`echo "$i" | \
                     awk '{ FS="-" ; \
                     if (($1 == '$DS51_PATCHID') && ($2 >= '$DS51_MIN_PATCHREV'))
                         print 0 ; \
                     else print 1;}'`
	  if [ $check_ret -eq 0 ]; then
	      break
	  fi
	done
	if [ $check_ret -eq 1 ]; then
	    debug "is_51_patch returns 1"
	    return 1
	fi
    else
	debug "is_51_patch returns 1"
	return 1
    fi    
    debug "is_51_patch returns 0"
    return 0
}

get_basedir IPLTdsu
if [ $? -eq 0 ]; then
    # Existing installed DS 5.1
    IPLTDSU_PKG=$PKGNAME
    ds51_basedir=$PKGBASEDIR
    if [ $ds51_basedir = $ds52_basedir ]; then
	# Existing installed DS 5.1 in the same basedir
	is_51_patch
	if [ $? -eq 0 ] ; then
	    # DS 5.1 has been patched (case 1.3.4.3)
	    over51p
	else
	    # DS 5.1 has not been patched (case 1.3.4.2)
	    over51
	fi
    else
	# No existing installed DS 5.1 in the same basedir (case 1.3.4.1)
	over52
    fi
else
    # No DS 5.1 (case 1.3.4.1)
    over52 
fi
