#!/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"



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

check_svrs () {

    stop_patch=no
    cmd_torun=

    # 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="/usr/sbin/directoryserver 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}/usr/sbin/directoryserver -s ${servername} stop\n"
	    fi
        done
    fi

    if [ ${stop_patch} = yes ]
    then
        echo "You 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}
        exit 1
    fi

    exit 0

}

SLEEP_TIME=60
times_up()
{
   check_svrs
}

# 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

