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

#########################################
#
# do_mv
#
#########################################
do_mv () {
   src=$1
   dst=$2

   debug "move $src to $dst"
   mv $src $dst
}

#########################################
#
# remove_force
#
#########################################
remove_force () {
   file_list=$*
   remove_force_rc=0

   for file in $file_list
   do
      if [ -h $file ]
      then
         file_type="link"
      else
         file_type="file"
      fi
      debug "remove $file_type $file"

      rm -f $file
      if [ $? -ne 0 ]
      then
         echo "Failed to remove $file_type: $file"
         remove_force_rc=1
      fi
   done

   return $remove_force_rc
}


#########################################
#
# over52
#
#########################################
over52() {
	debug "over52"
}

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

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

	# At this step, $ds51_basedir/usr/sbin/directoryserver is the 5.1 command
	# which means the wrapper has been erased.
	# Copy back the wrapper saved during prebackout
	remove_force $ds51_basedir/usr/sbin/directoryserver
	do_mv $ds51_basedir/usr/sbin/directoryserver.save $ds51_basedir/usr/sbin/directoryserver
}

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

#########################################
#
# 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.1.3.2)
				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--> POSTBACKOUT\n"

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

#
# Step_1 : Manage the command directoryserver
# -------------------------------------------
#
manage_directoryserver


#
# Step_2 : Re-start the slapd instances
# -------------------------------------
#
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 $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 "Postbackout script is starting Directory Server instance ${i}..."
	    	cmd_torun_ds="${DS_LOCAL_BASEDIR}/usr/iplanet/ds5/sbin/directoryserver -s ${servername} start"
		eval ${cmd_torun_ds}
		if [ $? -eq 1 ];then
			echo "Directory Server instance $servername could not be restarted"
		else
			echo "Directory Server instance $servername started"
		fi
            else
		start_request=yes
		cmd_torun_ds="${cmd_torun_ds} ${DS_LOCAL_BASEDIR}/usr/sbin/directoryserver -u 5.1 -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 ""
	echo ${cmd_torun_ds}
	debug "\n<-- POSTBACKOUT\n"
	exit 0
fi

debug "\n<-- POSTBACKOUT\n"
exit 0

# End of NPCTE Fix for bugid 6176080
