#!/bin/sh
#
# Copyright (c) 2003-2004 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident "@(#)postremove 1.0 - 03/02/04"

debug() {
    if [ "x"$VERBOSE != "x" ] ; then
	echo $1
    fi
}

if [ -z "$PKG_INSTALL_ROOT" ] ; then
    ROOTDIR="/"
else
    ROOTDIR=$PKG_INSTALL_ROOT
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
}

LOCAL_BASEDIR=$CLIENT_BASEDIR
debug LOCAL_BASEDIR=$LOCAL_BASEDIR 

no51(){
    debug "no51"
    return
}

over51(){
    debug "over51"
    if [ -f "$BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html.51bak" ]; then
	mv $BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html.51bak $BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html
        installf -R ${ROOTDIR} $IPLTDSU_PKG $LOCAL_BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html  f 0444 root bin
	installf -R ${ROOTDIR} -f $IPLTDSU_PKG || exit 2
	removef -R ${ROOTDIR} $IPLTDSU_PKG $LOCAL_BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html.51bak
	removef -R ${ROOTDIR} -f $IPLTDSU_PKG || exit 2
    fi
    if [ -f $BASEDIR/usr/iplanet/ds5/sbin/directoryserver ]; then
	debug "Moving /usr/iplanet/ds5/sbin/directoryserver to /usr/sbin/directoryserver"
	mv $BASEDIR/usr/iplanet/ds5/sbin/directoryserver $BASEDIR/usr/sbin/directoryserver
	debug "Removing /usr/iplanet/ds5/sbin/directoryserver from $IPLTDSU_PKG"
	removef -R ${ROOTDIR} $IPLTDSU_PKG $LOCAL_BASEDIR/usr/iplanet/ds5/sbin/directoryserver
	removef -R ${ROOTDIR} $IPLTDSU_PKG $LOCAL_BASEDIR/usr/iplanet/ds5/sbin
	removef -R ${ROOTDIR} -f $IPLTDSU_PKG
    fi
    if [ -f $BASEDIR/usr/sbin/directoryserver ] ; then
	debug "Adding /usr/sbin/directoryserver in $IPLTDSU_PKG"
	installf -R ${ROOTDIR} $IPLTDSU_PKG $LOCAL_BASEDIR/usr/sbin/directoryserver f 0755 root bin
	installf -R ${ROOTDIR} -f $IPLTDSU_PKG
    fi
}

over51p(){
    debug "over51p"
    if [ -f "$BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html.51bak" ]; then
	mv $BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html.51bak $BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html
        installf -R ${ROOTDIR} $IPLTDSU_PKG $BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html
	installf -R ${ROOTDIR} -f $IPLTDSU_PKG || exit 2
	removef -R ${ROOTDIR} $IPLTDSU_PKG $BASEDIR/usr/lib/help/profiles/locale/C/RtiDSMngmnt.html.51bak
	removef -R ${ROOTDIR} -f $IPLTDSU_PKG || exit 2
    fi
}

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_pkgname IPLTdsu
if [ $? -eq 0 ]; then
    # Existing installed DS 5.1
    IPLTDSU_PKG=$PKGNAME
    ds51_local_basedir=`pkginfo -R ${ROOTDIR} -r $IPLTDSU_PKG`
    if [ $ds51_local_basedir = $LOCAL_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.1.2)
	    over51p
	else
	    # DS 5.1 has not been patched (case 2.1.1)
	    over51
	fi
    else
	# No existing installed DS 5.1 in the same basedir (case 2.1.3)
	no51
    fi
else
    # No DS 5.1 (case 2.1.3)
    no51 
fi

