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

#########################################
#
# add_in_registry
#
##########################################
add_in_registry() {
    debug "add_in_registry $1 $2 $3 $4" 
    VERSION=$1
    CMD_PATH=$2
    INSTALLED=$3
    DEFAULT=$4
    if [ ! -d $REGISTRY_PATH ] ; then
	mkdir -p $REGISTRY_PATH
	chown root:sys $REGISTRY_PATH
	chmod 755 $REGISTRY_PATH	
    fi
    if [ ! -f $REGISTRY ] ; then
	echo "#version|command path|installed?|default?" > $REGISTRY
	chown root:sys $REGISTRY
	chmod 644 $REGISTRY
    fi
    grep "^$VERSION|" $REGISTRY 1>/dev/null 2>&1
    if [ $? -eq 0 ] ; then
	grep -v "^$VERSION|" $REGISTRY > $REGISTRY.tmp
	mv -f $REGISTRY.tmp $REGISTRY
	add_in_registry $VERSION $CMD_PATH $INSTALLED $DEFAULT
	return 
    fi
    echo "$VERSION|$CMD_PATH|$INSTALLED|$DEFAULT" >> $REGISTRY
}


#########################################
#
# 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 : 5.1sp4 applied on top of DS52"
	
	# Remove the previous cmd /usr/sbin/directoryserver.51bak
	debug "Removing $ds51_basedir/usr/sbin/directoryserver.51bak"
	rm -f $ds51_basedir/usr/sbin/directoryserver.51bak

	add_in_registry 5.2 $CMD52_PATH YES YES
	add_in_registry 5.1 $CMD51_PATH YES NO

	# Add /etc/ds/versions in IPLTdsr package
	get_pkgname IPLTdsr
	if [ $? -ne 0 ] ; then
		 exit 1
	fi

	debug "Adding $LOCAL_REGISTRY in $PKGNAME"
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH d 0755 root sys
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY f 0644 root sys
	installf -R $ROOTDIR -f $PKGNAME || exit 2

	# Add /etc/ds/versions in SUNWdsvr package
	get_pkgname SUNWdsvr
	if [ $? -ne 0 ] ; then
		 exit 1
	fi

	debug "Adding $LOCAL_REGISTRY in $PKGNAME"
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH d 0755 root sys
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY f 0644 root sys
	installf -R $ROOTDIR -f $PKGNAME || exit 2
}

#########################################
#
# over52OR2
#
##########################################
over52OR2() {
	debug "over52OR2 : 5.1sp4 applied on top of DS52 Orion2"

	# just tell the registry that 5.1 versions has been installed
	add_in_registry 5.1 $CMD51_PATH YES NO
}

#########################################
#
# over51
#
##########################################
over51() {
	debug "over51 : 5.1sp4 applied on top of DS5.1 rtm or patched"

	add_in_registry 5.1 $CMD51_PATH YES YES

	# Add /etc/ds/versions in IPLTdsr package
	get_pkgname IPLTdsr
	if [ $? -ne 0 ] ; then
		 exit 1
	fi

	debug "Adding $LOCAL_REGISTRY in $PKGNAME"
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH d 0755 root sys
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY f 0644 root sys
	installf -R $ROOTDIR -f $PKGNAME || exit 2
}

#########################################
#
# manage_directory
#
##########################################
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 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)
				over52OR2
			else
				# DS 5.2 installed (case A.1.3.2)
				over52
			fi
		else
			# DS 5.2 installed (case A.1.3.2)
			over52
		fi
	else
		# No DS 5.2 (case A1.3.1)
		# 5.1sp4 being applied on top of DS 5.1 or patched
		# don't need to distinguish these 2 cases 5.1 or 5.1P
		# because they did deliver the same installation/configuration
		# Caution : next 5.1sp5 will need to check if sp4 is installed
		over51
	fi
}


#####################################
#
# Main
#
#####################################

debug "\n--> POSTPATCH\n"

#
# Init var env for DS
# -------------------
#
get_basedir IPLTdsu
DS_BASEDIR=${PKGBASEDIR}
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
	debug "\n\tDS_is_configured"
    fi

#
# Manage directoryserver command
# ------------------------------
manage_directoryserver

#
# If DS is not configured, then there is nothing to do. Just exit 0
# -----------------------------------------------------------------
#
if [ "${DS_is_configured}" = "no" ]; then
	debug "DS is not configured, exit postpatch."
	debug "\n<-- POSTPATCH\n"
	exit 0
fi

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

#
# Restart the ldap 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
	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
           	echo "Postpatch script is starting Directory Server instance ${i} ..."
		eval ${DS_LOCAL_BASEDIR}/usr/iplanet/ds5/sbin/directoryserver -s ${servername} start
		if [ $? -eq 1 ]; then
			echo "Directory Server instance $servername could not be restarted"
		else
			echo "Directory Server instance $servername successfully restarted"
		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 applying this patch."
	echo "Log in to the system where this patch is "
	echo "installed and run the following commands:"
	echo ""
	echo "${cmd_torun_ds}"
fi

# End of fix for bugid 6176080


#
# Recommend the CDS update 
# -------------------------
#
UPDATE_CDS=${ROOTDIR}/usr/iplanet/ds5/setup/slapd/examples/update_configDS.sh
SLAPD_INF=/usr/iplanet/ds5/setup/slapd/slapd.inf
ADMIN_INF=/usr/iplanet/ds5/setup/admin/admin.inf
instance_list=`ls -d ${ROOTDIR}/var/ds5/slapd-* 2>/dev/null`
if [ ! "x${instance_list}" = x ] && [ -f ${UPDATE_CDS} ]
then 
    echo
    echo "You now need to update the Configuration Directory Server"
    echo "to reflect the new configuration data for both "
    echo "Administration and Directory Servers."
    echo "\nTo do that, you need to modify in the Configuration Directory"
    echo "Server the 2 entries \"cn=iPlanet Directory Server\" and"
    echo "\"cn=Netscape Administration Server\" of the relevant Server"
    echo "Group with new values for "
    echo "nsbuildnumber and nsproductversion attributes."
    echo "\nCorrect values for build numbers can be found in:"
    echo "${SLAPD_INF} and"
    echo "${ADMIN_INF}"
    echo "\nThe product version for Directory Server should be set to:"
    echo "5.1 Service Pack 4".
    echo "Product version for the Administration Server should not be changed."
    echo ""
    echo "A example script is also provided to help perform these updates:"
    echo "\t/usr/iplanet/ds5/setup/slapd/examples/update_configDS.sh"
    echo ""
fi


#
# Fix 2 bugids 6212731 and 6212732 
#	will  prevent issues when upgrading system to Solaris 10
# --------------------------------------------------------------
#
# NPCTE Fix for bugid 6212731 - <M.O.Parissis> <January,6 2005>
# 5.1sp4 start-admin fails due to the wrong jre path on Sol 10
# The web server is trying to launch a JVM 1.5.0_01 release ,
#   this is the default JAVA release and non supported within 5.1
# To be able to still use the JAVA release 1.4.2,
#  the link /usr/iplanet/ds5/bin/base/jre must point to ../../../../j2se/jre
#  instead of ../../../../java/jre
#
CONSOLE_JRE_PATH=${ROOTDIR}/usr/iplanet/ds5/bin/base/jre
if [ -d ${CONSOLE_JRE_PATH} ]
then
	debug "\n ... Removing ${CONSOLE_JRE_PATH}"
	debug "\n ... make new link ln -s ../../../../j2se/jre ${CONSOLE_JRE_PATH}"
	rm ${CONSOLE_JRE_PATH}
	ln -s ../../../../j2se/jre ${CONSOLE_JRE_PATH}
fi
# End of fix for bugid 6212731


# NPCTE Fix for bugid 6212732 - <M.O.Parissis> <January,6 2005>
# The perl tool version 5.00503 is no longer available on Solaris 10 system.
# But the Directory Server bundled with Solaris 9 still uses perl 5.00503 on sparc system.
# That means the migration of a sparc Solaris 9 system to Solaris 10 will break
#    Directory Server when perl scripts are being executed.
# Use instead /usr/bin/perl.
# This defect does not apply on intel platform since perl link is correctly
#    referencing /usr/bin/perl
#
platform=`uname -p`
if [ "${platform}" = "sparc" ]
then
	# originally ${ROOTDIR}/usr/iplanet/ds5/bin/slapd/admin/bin/perl is linked to
	#   ../../../../../../perl5/5.00503/bin/perl which does no longer exist.
	# instead use /usr/bin/perl which is linked to  ../perl5/5.8.4/bin/perl
	PERL_LINK=${ROOTDIR}/usr/iplanet/ds5/bin/slapd/admin/bin/perl
	if [ -L ${PERL_LINK} ]
	then
		debug "\n ... Removing ${PERL_LINK}"
		debug "\n ... make new link ln -s ../../../../../../bin/perl ${PERL_LINK}"
		rm ${PERL_LINK}
		ln -s ../../../../../../bin/perl ${PERL_LINK}
	fi
fi
# End of fix for bugid 6212732 

debug "\n<-- POSTPATCH\n"
exit 0
# end main
