#""!/bin/sh
#
# Copyright (c) 2002 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#ident "@(#)prebackout 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
}


#########################################
#
# flag_uninstall_in_registry
#
#########################################
flag_uninstall_in_registry () {
	debug "flag_uninstall_in_registry $1"

	VERSION=$1
	INSTALL="NO"
	if [ -f $REGISTRY ]
	then
		# Look for VERSION and set NO at install field
		cat $REGISTRY | awk '{ FS = "|" ; \
		   if ( $1 == '$VERSION' ) { \
			$3="'$INSTALL'" \
		   }\
		   print $1"|"$2"|"$3"|"$4 \
		}' > $REGISTRY.tmp
		mv $REGISTRY.tmp $REGISTRY
	fi
}


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


#########################################
#
# over52
#
#########################################
over52() {
	debug "over52"
	
	# Tell the registry, the 5.1 version is no longer installed
	flag_uninstall_in_registry 5.1

	get_pkgname IPLTdsr
	if [ $? -ne 0 ] ; then
		exit 1
	fi

	debug "Removing $LOCAL_REGISTRY from $PKGNAME"
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH
	removef -R $ROOTDIR -f  $PKGNAME || exit 2

	get_pkgname SUNWdsvr
	if [ $? -ne 0 ] ; then
		exit 1
	fi

	debug "Removing $LOCAL_REGISTRY from $PKGNAME"
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH
	removef -R $ROOTDIR -f  $PKGNAME || exit 2

	if [ -f $ds51_basedir/usr/iplanet/ds5/sbin/directoryserver ] ; then
		debug "Copying $ds51_basedir/usr/iplanet/ds5/sbin/directoryserver to $ds51_basedir/usr/sbin/directoryserver.51bak"

		cp $ds51_basedir/usr/iplanet/ds5/sbin/directoryserver \
			$ds51_basedir/usr/sbin/directoryserver.51bak
	fi
}

#########################################
#
# over_no52
#
#########################################
over_no52() {
	debug "over_no52"

	# Tell the registry, the 5.1 version is no longer installed
	flag_uninstall_in_registry 5.1

	get_pkgname IPLTdsr
	if [ $? -ne 0 ] ; then
		exit 1
	fi

	debug "Removing $LOCAL_REGISTRY from $PKGNAME"
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH
	removef -R $ROOTDIR -f  $PKGNAME || exit 2
}

#########################################
#
# over52OR2
#
#########################################
over52OR2() {
	debug "over52OR2"

	# Save wrapper. Otherwise backout will overwrite it with the 5.1 command
	debug "Save the directoryserver wrapper command"
	cp $ds51_basedir/usr/sbin/directoryserver $ds51_basedir/usr/sbin/directoryserver.save

	# Tell the registry, the 5.1 version is no longer installed
	flag_uninstall_in_registry 5.1

	get_pkgname IPLTdsr
	if [ $? -ne 0 ] ; then
		exit 1
	fi

	debug "Removing $LOCAL_REGISTRY from $PKGNAME"
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY
	removef -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH
	removef -R $ROOTDIR -f  $PKGNAME || exit 2
}


#########################################
#
# manage_directoryserver
#
#########################################
manage_directoryserver() {
	debug "\nManage directoryserver command"

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

	CMD51_PATH=${LOCAL_BASEDIR}/usr/iplanet/ds5/sbin/directoryserver
	CMD52_PATH=${LOCAL_BASEDIR}/usr/ds/v5.2/sbin/directoryserver

	REGISTRY_PATH=$ds51_basedir/etc/ds
	REGISTRY=$REGISTRY_PATH/versions
	LOCAL_REGISTRY_PATH=$ds51_local_basedir/etc/ds
	LOCAL_REGISTRY=$LOCAL_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 package SUNWdsvu 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.2.2)
				over52OR2
			else
				# DS 5.2 installed (case A.2.1)
				over52
			fi
		else
			# DS 5.2 installed (case A.2.1)
			over52
		fi
	else
		# No DS 5.2 installed
		over_no52
	fi
}

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

debug "\n--> PREBACKOUT\n"

#
# Setup the variables for DS 
# --------------------------
#
get_basedir IPLTdsu
DS_BASEDIR=${PKGBASEDIR}
ds51_basedir=$DS_BASEDIR
DS_LOCAL_BASEDIR=${LOCAL_BASEDIR}

#
# Check whether DS is configured
# ------------------------------
#
DS_is_configured=no
SERVER_ROOT=${ROOTDIR}/usr/iplanet/ds5
    instance_list=`ls -d ${SERVER_ROOT}/slapd-* 2>/dev/null`
    if [ ! "x${instance_list}" = "x" ]
    then
	DS_is_configured=yes
    fi

#
# Check whether AS is configured
# ------------------------------
#
debug "Check Admin Server"
stop_patch=no
if [ -f ${ROOTDIR}/usr/iplanet/admserv5.1/admin-serv/logs/pid ]
then
	echo "Prebackout script is stopping Admin Server" 
	cmd_torun_as="${DS_LOCAL_BASEDIR}/usr/iplanet/ds5/sbin/directoryserver stop-admin"
	eval ${cmd_torun_as}
	if [ $? -eq 1 ];then
		echo "Admin Server could not be stopped"
		exit 1
        else
		echo "Admin Server stopped"
	fi
fi

#
# If DS is not configured, just need to check directoryserver command
# -------------------------------------------------------------------
#
if [ "${DS_is_configured}" = "no" ]; then
    manage_directoryserver
    debug "\n<-- PREBACKOUT\n"
    exit 0
fi

#
# ==============================================
#	DS is configured
# ==============================================

#
# Step_1 : Stop the slapd instances
# ---------------------------------
#
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 $instance_list
    do
	servername=`/bin/basename $i | /bin/awk '{ print substr($0,7,length($0)-6) }'`
	if [ -f ${i}/logs/pid ] ; then
            if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
		then
           	echo "Prebackout script is stopping Directory Server instance ${i}..."
	    	cmd_torun_ds="${DS_LOCAL_BASEDIR}/usr/iplanet/ds5/sbin/directoryserver -s ${servername} stop"
		eval ${cmd_torun_ds}
		if [ $? -eq 1 ];then
			echo "Directory Server instance $servername could not be stopped"
		else
			echo "Directory Server instance $servername stopped"
		fi
            else
		stop_patch=yes
		cmd_torun_ds="${cmd_torun_ds} ${DS_LOCAL_BASEDIR}/usr/sbin/directoryserver -u 5.1 -s ${servername} stop\n"
    	    fi
	fi
    done
fi

if [ ${stop_patch} = "yes" ]
then
	echo "You need to stop the servers before removing this patch."
	echo "Please log on the machine on which this patch is to be"
	echo "removed and run the following commands:"
	echo ""
	echo ${cmd_torun_ds}
	debug "\n<-- PREBACKOUT\n"
	exit 1
fi

#
# Step_2 : Manage the command directoryserver
# -------------------------------------------
#
manage_directoryserver
debug "\n<-- PREBACKOUT\n"
exit 0

# End of NPCTE Fix for bugid 6176080
