#!/bin/ksh

PKGCOND=/usr/bin/pkgcond

ip_mv_patchid=125504-01

last_patch() {
    ## returns the number of patches installed at or above this rev.
    root_dir=${ROOTDIR:-/}
    
    ## 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_global () {
    srcpkg=$1
    dstpkg=$2

    pkgmapfilter=/var/run/.$$.chpkg.$$.filter.$$
    > $pkgmapfilter

    echo "$3" | while read type class object ; do
	[ -z "$object" ] && continue

	if [ "$type" = "di" ] ; then
	    [ -e "$ROOTDIR/$object" ] && installf -R ${ROOTDIR:-/} $dstpkg /$object
	    
	elif [ "$type" = "dr" ] ; then
	    removef -R ${ROOTDIR:-/} $srcpkg /$object > /dev/null
	    echo " $object[ =]" >> $pkgmapfilter
	    
	else
	    removef -R ${ROOTDIR:-/} $srcpkg /$object > /dev/null
	    if [ "$type" = "d" ] ; then 
		[ -e "$ROOTDIR/$object" ] && installf -R ${ROOTDIR:-/} $dstpkg /$object
		
	    elif [ "$type" = "s" ] ; then 
		[ -h "$ROOTDIR/$object" ] && installf -c $class -R ${ROOTDIR:-/} $dstpkg /$object
		
	    else
		[ -e "$ROOTDIR/$object" ] && installf -c $class -R ${ROOTDIR:-/} $dstpkg /$object $type
	    fi
	    echo " $object[ =]" >> $pkgmapfilter
	fi
	
    done

    removef -R ${ROOTDIR:-/} -f $srcpkg
    installf -R ${ROOTDIR:-/} -f $dstpkg

    pspool_pkgmap=${ROOTDIR:-/}var/sadm/pkg/$srcpkg/save/pspool/$srcpkg/pkgmap
    [ -f "$pspool_pkgmap" ] && {
	egrep -f $pkgmapfilter -v $pspool_pkgmap > $pspool_pkgmap.tmp
	mv $pspool_pkgmap.tmp $pspool_pkgmap
    }
    rm $pkgmapfilter

    pspool_pkgmap=${ROOTDIR:-/}var/sadm/pkg/$dstpkg/save/pspool/$dstpkg/pkgmap
    pspool_pkgmap_removed=${ROOTDIR:-/}var/sadm/patch/$PatchNum/${dstpkg}_pspool_pkgmap_removed
    [ -f "$pspool_pkgmap" -a -f "$pspool_pkgmap_removed" ] && {
	sort -u -k3,5 $pspool_pkgmap $pspool_pkgmap_removed > /var/run/.$$.pspool_pkgmap_removed.$$
	grep '^:' /var/run/.$$.pspool_pkgmap_removed.$$ > $pspool_pkgmap
	grep -v '^:' /var/run/.$$.pspool_pkgmap_removed.$$ >> $pspool_pkgmap
	rm /var/run/.$$.pspool_pkgmap_removed.$$
    }
}



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 [ "$ROOTDIR" = "/" -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
}


## Backout of non-hollow pkg SUNWcsr will have removed all trace of files
## from non-global zones, so they are done. All that remains is to transfer
## the files from SUNWcsr back to the original pkgs in the global zone.

global_context=true

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


if [ "$global_context" = "true" ] ; then
    last_patch $ip_mv_patchid
    if [ "$?" -eq "0" ] ; then    
	pkginfo -R ${ROOTDIR:-/} -q SUNWcnetr && chpkg_global SUNWcsr SUNWcnetr "$SUNWcsr_to_SUNWcnetr"
	pkginfo -R ${ROOTDIR:-/} -q SUNWipfr && chpkg_global SUNWcsr SUNWipfr "$SUNWcsr_to_SUNWipfr"
    fi
fi

exit 0

