#!/bin/sh
#
# Copyright (c) 2001-2004 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident "@(#)postbackout 1.0 - 02/10/23"


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
}


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

# For AS

get_basedir SUNWasvu
AS_BASEDIR=${PKGBASEDIR}

# For DS

get_basedir SUNWdsvu
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

#####################################
#
# Restart the ldap instances
#
#
#####################################

PATHNUM=`env | grep PatchNum`


start_request=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
	if [ ! -f ${i}/logs/pid ]
	then
	    servername=`/bin/basename $i | /bin/awk '{ print substr($0,7,length($0)-6) }'` 
            if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
    	    then
		if [ -f /var/tmp/.shouldRestart_DS_${servername}_${PATHNUM} ]; then
           		echo "Postbackout script is starting Directory Server instance ${i}..."
			eval ${DS_LOCAL_BASEDIR}/usr/ds/v5.2/sbin/directoryserver -s ${servername} start
			if [ $? -eq 1 ]; then
				echo "Directory Server instance could not be restarted"
			else
				echo "Directory Server instance successfully restarted"
			fi
			rm -f /var/tmp/.shouldRestart_DS_${servername}_${PATHNUM}
		fi
            else
		start_request=yes
		cmd_torun_ds="${cmd_torun_ds} ${DS_LOCAL_BASEDIR}/usr/ds/v5.2/sbin/directoryserver -s ${servername} start\n"
    	    fi
	fi
    done
fi

if [ ${start_request} = "yes" ]
then
	echo "You must restart the servers after removing this patch."
	echo "Log in to the system where this patch has been "
	echo "removed and run the following commands:"
	echo ${cmd_torun_admin}
	echo ${cmd_torun_ds}
	exit 0
fi


