# This script deletes the used backout data for a patch package
# and removes the deletes file entries.
#
# directory format options.
#
#       @(#)patch_postinstall 1.3 98/07/29 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

PATH=/usr/sadm/bin:$PATH
THIS_DIR=`dirname $0`

Our_Deletes=$THIS_DIR/deletes

#
# Delete the used backout data
#
if [ -f $Our_Deletes ]; then
	cat $Our_Deletes | while read path; do
		if valpath -l $path; then
			Client_Path=`echo "$CLIENT_BASEDIR/$path" | sed "s|//|/|"`
		else	# It's an absolute path
			Client_Path=$path
		fi
		rm `removef $PKGINST $Client_Path`
	done
	removef -f $PKGINST

	rm $Our_Deletes
fi


PKGCOND=/usr/bin/pkgcond

ip_mv_patchid=125504-01

last_patch() {
    ## returns the number of patches installed at or above this rev.
    root_dir=${PKG_INSTALL_ROOT:-/}
    
    ## parse id and rev
    pid=`echo $1 | cut -d\- -f1`
    prev=`echo $1 | cut -d\- -f2`
    patch_cnt=0

    ## get all installed refernces to the installed patch base id
    installed_patches=`patchadd -p -R $root_dir | sed -n -e 's/Req.*//' -e 's/[a-zA-Z]*://g' -e 's/,//g' -e "/$pid/p"`
    
    for x in $installed_patches ; do
	base=`echo $x  | cut -d\- -f1`
	rev=`echo $x | cut -d\- -f2`
	if [ $pid -eq $base ] && [ $rev -ge $prev ] ; then
            ## count all installed patches includeing this patch
	    patch_cnt=`expr $patch_cnt + 1`
	fi
    done
    
    return $patch_cnt
}


# revert to original types/classes from SUNWcnetr/SUNWipfr

SUNWcsr_to_SUNWcnetr="
e preserve etc/aggregation.conf
e preserve etc/default/dhcpagent
e preserve etc/default/inetinit
e preserve etc/default/ipsec
e preserve etc/default/mpathd
f none etc/inet/datemsk.ndpd
f none etc/inet/ike/config.sample
e ipsecalgsbase etc/inet/ipsecalgs
f none etc/inet/ipsecinit.sample
f none etc/inet/secret/ike.preshared
f none etc/inet/secret/ipseckeys.sample
e sock2path etc/inet/sock2path
s none etc/sock2path
f none sbin/dladm
di none etc
di none etc/default
di none etc/inet
d none etc/inet/ike
d none etc/inet/ike/crls
d none etc/inet/ike/publickeys
d none etc/inet/secret
d none etc/inet/secret/ike.privatekeys
di none sbin"

SUNWcsr_to_SUNWipfr="
e preserve etc/ipf/ipf.conf
f none lib/svc/method/ipfilter
f manifest var/svc/manifest/network/ipfilter.xml
f none lib/svc/method/pfil
f manifest var/svc/manifest/network/pfil.xml
dr none etc/ipf
di none lib
di none lib/svc
di none lib/svc/method
di none var
di none var/svc
di none var/svc/manifest
di none var/svc/manifest/network"

chpkg_nonglobal () {
    srcpkg=$1
    dstpkg=$2

    echo "$3" | while read type class object ; do
	[ -z "$object" ] && continue
	if [ "$type" = "di" ] ; then
	    :	
	elif [ "$type" = "d" -o "$type" = "dr" ] ; then
	    removef -R ${PKG_INSTALL_ROOT:-/} $srcpkg /$object
	else
	    rm -f `removef -R ${PKG_INSTALL_ROOT:-/} $srcpkg /$object` 2>/dev/null
	fi
    done
    removef -R ${PKG_INSTALL_ROOT:-/} -f $srcpkg
}



ExecuteInProperEnvironment () {
    
   ##  $PKGCOND is_path_writable $ROOTDIR/usr/bin
#   /usr/bin/touch ${ROOTDIR}/usr/bin/.test.$$ > /dev/null 2>&1 && {
#       /usr/bin/rm /usr/bin/.test.$$ > /dev/null 2>&1
#   }
    
    if $PKGCOND is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
        # Execute non-global whole root zone commands.
        # Should be same action as the default action.
	global_context=false
	return 0
    fi
    
    if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
        # Execute non-global zone commands. Should be no action here
	global_context=false
	return 0
    fi
    
    if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; then
        # Execute commands applicable to patching the mini-root.
        # There are usually no actions to take here since your patching
        # the mini-root on an install server.
 	return 0
    fi
    
    if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
        # Execute commands specific to the mini-root
	return 0
    fi
    
    if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
        # Execute commands specific to diskless client
	return 0
    fi
    
    if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
        # Execute commands specific to an alternate root
	return 0
    fi
    
    if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
        # In a global zone and system is mounted on /.
        # Execute all commands.
	return 0
    fi
    
    return 1
}

CheckZones()
{
    if [ "${PKG_INSTALL_ROOT:-/}" = "/" -a -x /usr/bin/zonename ]; then
	ZONENAME=`/usr/bin/zonename`
	if [ ${ZONENAME} = "global" ]; then
	    GLOBAL_ZONE=true
	else
	    GLOBAL_ZONE=false
	fi
    else
        # Unable to determine zone
	GLOBAL_ZONE=true
    fi
}


## Transfer files from non-hollow pkg (SUNWcsr) back to original hollow
## pkg (SUNWcnetr, SUNWipfr). In non-global zones, non-hollow -> hollow
## transfer equates to removing the file from the filesystem and removing
## its corresponding entry from the contents db. This needs to be done in
## patch_postinstall (as opposed patch postbackout) because zones created
## after applying the patch don't inherit a copy of the postbackout script
## (or even the /var/sadm/patch/<PID> directory, CR 6579248) 

global_context=true

if [ -x "$PKGCOND" ] ; then
    ExecuteInProperEnvironment
else
    CheckZones
    if [ "${GLOBAL_ZONE}" = "true" ]; then
	:
    else
	global_context=false
    fi
fi


if [ "$global_context" = "false" ] ; then
    last_patch $ip_mv_patchid
    if [ "$?" -eq "1" ] ; then
	chpkg_nonglobal SUNWcsr SUNWcnetr "$SUNWcsr_to_SUNWcnetr"
	chpkg_nonglobal SUNWcsr SUNWipfr "$SUNWcsr_to_SUNWipfr"
    fi
fi



#
# Remove the deletes file, checkinstall and the postinstall
#
rm -r $PKGSAV/$ACTIVE_PATCH
rm -f $THIS_DIR/checkinstall $THIS_DIR/postinstall

exit 0
