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

# Check to see if 51 backup files files exist:

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

REGISTRY_PATH=$BASEDIR/etc/ds
REGISTRY=$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
}

no51(){
    debug "no51"
    uninstall_in_registry 5.2
}

over51(){
    debug "over51"
    uninstall_in_registry 5.2
    chg_cmd_in_registry 5.1 $LOCAL_BASEDIR/usr/sbin/directoryserver
}

over51p(){
    debug "over51p"
    uninstall_in_registry 5.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 $IPLTDSR_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 IPLTdsr
if [ $? -eq 0 ]; then
    # Existing installed DS 5.1
    IPLTDSR_PKG=$PKGNAME
    ds51_local_basedir=`pkginfo -R ${ROOTDIR} -r $IPLTDSR_PKG`
    if [ $ds51_local_basedir = $LOCAL_BASEDIR ]; then
	# Existing installed DS 5.1 in the same basedir
	if [ -f "${BASEDIR}/etc/init.d/directory.51bak" ]; then
	    
	    installf -R $ROOTDIR $IPLTDSR_PKG $LOCAL_BASEDIR/etc/init.d/directory f 0744 root sys
	    mv ${BASEDIR}/etc/init.d/directory.51bak ${BASEDIR}/etc/init.d/directory
	    installf -R $ROOTDIR $IPLTDSR_PKG $LOCAL_BASEDIR/etc/rc0.d/K41directory=../init.d/directory s
	    installf -R $ROOTDIR $IPLTDSR_PKG $LOCAL_BASEDIR/etc/rc1.d/K41directory=../init.d/directory s
	    installf -R $ROOTDIR $IPLTDSR_PKG $LOCAL_BASEDIR/etc/rc2.d/S72directory=../init.d/directory s
	    installf -R $ROOTDIR $IPLTDSR_PKG $LOCAL_BASEDIR/etc/rcS.d/K41directory=../init.d/directory s
	    installf -R $ROOTDIR -f $IPLTDSR_PKG || exit 2
	fi
	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

