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

get_basedir SUNWdsvu
ds52_basedir=$PKGBASEDIR
ds52_local_basedir=${LOCAL_BASEDIR}
SUNWDSVU_PKG=$PKGNAME

REGISTRY_PATH=$ds52_basedir/etc/ds
REGISTRY=$REGISTRY_PATH/versions
LOCAL_REGISTRY_PATH=$ds52_local_basedir/etc/ds
LOCAL_REGISTRY=$LOCAL_REGISTRY_PATH/versions

uninstall_in_registry(){
    debug "uninstall_in_registry $1"
    VERSION=$1
    if [ -f $REGISTRY ] ; then
	cat $REGISTRY | awk '{ FS = "|" ; \
        if ($1 ~ /^#/ ) { \
            print $0 } \
        else  { \
            if ( $1 == '$VERSION' ) { \
                $3="NO" \
            }; \
            print $1"|"$2"|"$3"|"$4} \
        }' > $REGISTRY.tmp
	mv $REGISTRY.tmp $REGISTRY
    fi
}

chg_cmd_in_registry(){
    debug "chg_cmd_in_registry $1 $2"
    VERSION=$1
    CMD=$2
    if [ -f $REGISTRY ] ; then
	cat $REGISTRY | awk '{ FS = "|" ; \
            if ( $1 == '$VERSION' ) { \
                $2="'$CMD'" \
            }\
            print $1"|"$2"|"$3"|"$4 \
        }' > $REGISTRY.tmp
	mv $REGISTRY.tmp $REGISTRY
    fi
}

over52() {
    debug "over52"
}

over51(){
    debug "over51"
    if [ -f $ds52_basedir/usr/iplanet/ds5/sbin/directoryserver ] ; then
	debug "Moving $ds52_basedir/usr/iplanet/ds5/sbin/directoryserver to $ds52_basedir/usr/sbin/directoryserver.51bak"
	mv $ds52_basedir/usr/iplanet/ds5/sbin/directoryserver \
	    $ds52_basedir/usr/sbin/directoryserver.51bak
	debug "Removing $ds52_local_basedir/usr/iplanet/ds5/sbin/directoryserver from $IPLTDSU_PKG"
	removef -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/iplanet/ds5/sbin/directoryserver
	removef -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/iplanet/ds5/sbin
	removef -R $ROOTDIR -f  $IPLTDSU_PKG || exit 2
    fi
    if [ -f $ds52_basedir/usr/sbin/directoryserver ] ; then
        debug "Removing $ds52_basedir/usr/sbin/directoryserver"
	rm $ds52_basedir/usr/sbin/directoryserver
	removef -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/sbin/directoryserver
	removef -R $ROOTDIR -f	$IPLTDSU_PKG || exit 2
        removef -R $ROOTDIR $SUNWDSVU_PKG $ds52_local_basedir/usr/sbin/directoryserver
        removef -R $ROOTDIR -f  $SUNWDSVU_PKG || exit 2
	installf -R $ROOTDIR $SUNWDSVU_PKG $ds52_local_basedir/usr/sbin/directoryserver=../ds/v5.2/sbin/directoryserver s
	installf -R $ROOTDIR -f	$SUNWDSVU_PKG || exit 2
    fi
    chg_cmd_in_registry 5.1 $ds52_local_basedir/usr/sbin/directoryserver.51bak
}
    
over51p(){
    debug "over51p"
    # Get saved wrapper
    debug "Moving $ds52_basedir/usr/sbin/directoryserver.save to $ds52_basedir/usr/sbin/directoryserver"
    rm -f $ds52_basedir/usr/sbin/directoryserver
    mv -f $ds52_basedir/usr/sbin/directoryserver.save \
	$ds52_basedir/usr/sbin/directoryserver
    debug "Adding $ds52_local_basedir/usr/sbin/directoryserver in $IPLTDSU_PKG"
    installf -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/sbin/directoryserver f 0755 root bin
    installf -R $ROOTDIR -f $IPLTDSU_PKG || exit 2
}

if [ `uname -i` = "i86pc" ] ; then
    DS51_PATCHID="114273"
    DS51_MIN_PATCHREV="04"
else
    DS51_PATCHID="113859"
    DS51_MIN_PATCHREV="04"
fi

is_51_patch(){
    debug "is_51_patch"
    current_patch=`pkgparam -R $ROOTDIR $IPLTDSU_PKG PATCHLIST`
    if [ "x$current_patch" != "x" ]; then
	for i in `echo $current_patch`
	  do
	  check_ret=`echo "$i" | \
                     awk '{ FS="-" ; \
                     if (($1 == '$DS51_PATCHID') && ($2 >= '$DS51_MIN_PATCHREV'))
                         print 0 ; \
                     else print 1;}'`
	  if [ $check_ret -eq 0 ]; then
	      break
	  fi
	done
	if [ $check_ret -eq 1 ]; then
	    debug "is_51_patch returns 1"
	    return 1
	fi
    else
	debug "is_51_patch returns 1"
	return 1
    fi    
    debug "is_51_patch returns 0"
    return 0
}

get_basedir IPLTdsu
if [ $? -eq 0 ]; then
    # Existing installed DS 5.1
    IPLTDSU_PKG=$PKGNAME 
    ds51_basedir=$PKGBASEDIR
    if [ $ds51_basedir = $ds52_basedir ]; then
	# Existing installed DS 5.1 in the same basedir
	is_51_patch
	if [ $? -eq 0 ] ; then
	    # DS 5.1 has been patched (case 2.2.2)
	    over51p
	else
	    # DS 5.1 has not been patched (case 2.2.1)
	    over51
	fi
    else
	# No existing installed DS 5.1 in the same basedir (case 2.2.3)
	over52
    fi
else
    # No DS 5.1 (case 2.2.3)
    over52 
fi

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

# For AS

get_basedir SUNWasvu
AS_BASEDIR=${PKGBASEDIR}
AS_LOCAL_BASEDIR=${LOCAL_BASEDIR}

# For DS

DS_BASEDIR=${ds52_basedir}
DS_LOCAL_BASEDIR=${ds52_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


