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


# NPCTE Fix for bugid 6176080 - <M.O.Parissis> <January,25 2005>
# Sun ONE DS 5.2 Patch2 could be already installed then 5.1sp4 should not
# break previous DS configuration
# This is the aim of the lines below.
#

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

#########################################
#
# get_pkgname
#
#########################################
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
#
#########################################
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
}


#########################################
#
# is_52OR2
#
#########################################
is_52OR2 () {
	debug "\nis 5.2OR2 or later version installed?"

	current_patch=`pkgparam -R $ROOTDIR $SUNWDSVU_PKG PATCHLIST`
	if [ "$current_patch" != "" ]; then
		for i in `echo $current_patch`
		do
			check_ret=`echo "$i" | \
			    awk '{ FS="-" ; \
			    if (($1 == '$DS52OR2_PATCHID') && ($2 >= '$DS52OR2_MIN_PATCHREV'))
				print 0 ; \
			    else print 1;}'`
			[ $check_ret -eq 0 ] && break
		done

		if [ $check_ret -eq 1 ]; then
			debug "is_52OR2 returns 1"
			return 1
		fi
	else
		debug "is_52OR2 returns 1"
		return 1
	fi

	debug "is_52OR2 returns 0"
	return 0
}


#########################################
#
# which_DS_versions
#
#########################################
which_DS_versions_already_installed () {
	debug "\nIs there other version of DS already installed?"

	get_basedir IPLTdsu
	ds51_basedir=$PKGBASEDIR
	ds51_local_basedir=${LOCAL_BASEDIR}

	REGISTRY_PATH=$ds51_basedir/etc/ds
	REGISTRY=$REGISTRY_PATH/versions

	if [ `uname -i` = "i86pc" ]
	then
		DS52OR2_PATCHID="115615"
		DS52OR2_MIN_PATCHREV="10"
	else
		DS52OR2_PATCHID="115614"
		DS52OR2_MIN_PATCHREV="10"
	fi

	debug "\nIs SUNWdsvu package present ?"
	get_basedir SUNWdsvu
	if [ $? -eq 0 ]; then
		# Existing installed DS 5.2
		SUNWDSVU_PKG=$PKGNAME
		if [ -f $REGISTRY ]; then
			is_52OR2
			if [ $? -eq 0 ] ; then
				# DS5.2OR2 installed (case A.1.3.3)
				# wrapper exists
				cmd_ds="${ds51_local_basedir}/usr/iplanet/ds5/sbin/directoryserver"
			else
				# DS 5.2 installed (case A.1.3.2)
				# DS5.2 saved the 5.1 directoryserver cmd in .51bak
				cmd_ds="${ds51_local_basedir}/usr/sbin/directoryserver.51bak"
			fi
		else
			# DS 5.2 installed (case A.1.3.2)
			# DS5.2 saved the 5.1 directoryserver cmd in .51bak
			cmd_ds="${ds51_local_basedir}/usr/sbin/directoryserver.51bak"
		fi
	else
		# No DS 5.2 (case A1.3.1)
		# 5.1sp4 being applied on top of DS 5.1 or patched
		# nothing has changed yet
		cmd_ds="${ds51_local_basedir}/usr/sbin/directoryserver"
	fi
}


#########################################
#
# check_svrs
#
#########################################
check_svrs () {

# Check to see if the admin and directory server are running or configured.
#
    stop_patch=no
    cmd_torun=

    # The command directoryserver stored in cmd_ds depends on the DS version installed
    which_DS_versions_already_installed

    # Admin server first
    if [ -f ${ROOTDIR}/usr/iplanet/admserv5.1/admin-serv/logs/pid ]
    then
        stop_patch=yes
        echo "Admin Server is running"
        cmd_torun="${cmd_ds} stop-admin\n"
    fi

    # Directory server(s)
    instance_list=`ls -d ${ROOTDIR}/var/ds5/slapd-* 2>/dev/null`
    if [ ! "x${instance_list}" = x ]
    then
        for i in `ls -d ${ROOTDIR}/var/ds5/slapd-*`
        do
	    if [ -f ${i}/logs/pid ]
	    then
	        stop_patch=yes
	        servername=`/bin/basename $i | /bin/awk '{ print substr($0,7,length($0)-6) }'` 
	        echo "Directory Server is running for instance ${i}"
	        cmd_torun="${cmd_torun}${cmd_ds} -s ${servername} stop\n"
	    fi
        done
    fi

    if [ ${stop_patch} = yes ]
    then
        echo "\nYou need to stop the servers before applying this patch."
        if [ "x${ROOTDIR}" = x ] || [ "x${ROOTDIR}" = "x/" ]
        then
	    echo "Please run the following commands:"
        else
	    echo "Please log on the machine on which this patch is to be"
	    echo "installed and run the following commands:"
        fi
        echo ${cmd_torun}
	debug "\n<-- PREPATCH\n"
        exit 1
    fi

    exit 0

}

#########################################
#
# times_up
#
#########################################
SLEEP_TIME=60
times_up()
{
   check_svrs
}

#########################################
#
# main
#
#########################################
debug "\n--> PREPATCH\n"
# check if DS 5.2 packages are installed
/bin/pkginfo -R ${ROOTDIR} SUNWdsvu 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
    if [ "${ROOTDIR}" = "/" ]
    then
        ECHO_ROOTDIR=
    else
        ECHO_ROOTDIR="${ROOTDIR}"
    fi
    echo "Warning: Directory Server 5.2 packages are \c"
    echo "installed on ${ROOTDIR}"
    echo
    echo_inst_cmd=${ECHO_ROOTDIR}/usr/sbin/directoryserver
    echo "    If you apply this Directory Server 5.1 patch,"
    echo "    ${echo_inst_cmd} will then be the 5.1 version."
    ds52_inst_cmd=${ROOTDIR}/usr/ds/v5.2/sbin/directoryserver
    echo_ds52_inst_cmd=${ECHO_ROOTDIR}/usr/ds/v5.2/sbin/directoryserver
    if [ -f ${ds52_inst_cmd} ]
    then
        echo "    The 5.2 version will however still be available from"
        echo "    ${echo_ds52_inst_cmd}."
    fi

    trap 'times_up' ALRM
    ( sleep $SLEEP_TIME ; kill -ALRM $$ ) &
    child_pid=$!

    echo
    echo "Do you wish to continue this installation {yes or no} [yes]?"
    echo "(by default, installation will continue in $SLEEP_TIME seconds)"

    read Response
    while [ 1 ]
    do
        case $Response in
                n | no | N | NO | No)
                        kill -9 $child_pid
                        exit 1 ;;

                "" | y | yes | Y | Yes | YES)
                        kill -9 $child_pid
                        check_svrs
                        exit 0 ;;

                *)  echo "yes or no please. \\c"
                        read Response ;;
        esac
    done
else
    check_svrs
fi

debug "\n<-- PREPATCH\n"
# End of NPCTE Fix for bugid 6176080
