#!/bin/ksh

#I386 version
zoneadmd_patch=122661-08
PKGCOND=/usr/bin/pkgcond

last_patch() {

        ## returns the number of patches installed at or above this rev.
        ## if this is the first rev of the patch
        ##      prepatch will return 0
        ##      postpatch, prebackout, and postbackout will return 1



        ## 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 $ROOTDIR -p | 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=$(($patch_cnt + 1))
                fi
        done

        return $patch_cnt
}

ExecuteALLCmds () {

      # if SUNWzoneu is installed and this is not a miniroot
      # then enfore addition of 122661-08 or higher
      # we check if SUNWzoneu is installed, if not
      #  no zones bits are installed, so ok to not
      #   add the zones patch.
      #
      # need to add to ABE in order to be safe.
      # 
      if [ `/sbin/zonename` != "global" ]; then
        # looks like we are in a non global zone
        # so just return at this point.
             return 0
      fi
      /usr/bin/pkginfo -R $ROOTDIR SUNWzoneu > /dev/null 2>&1
       if [ "$?" -eq "0"  ]; then
                last_patch $zoneadmd_patch
                 if [ $? -eq 0 ] ; then
                         echo
                         echo "Patch $zoneadmd_patch needs to be installed before this patch can be"
                         echo "successfully installed on this system."
                         echo
                         echo "Please install $zoneadmd_patch and then run patchadd again for this patch."
                         exit 1

                 fi
       fi

}

ExecuteInProperEnvironment () {

   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.
       return 0
   fi

   if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
       # Execute non-global zone commands. Should be no action here
       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
       ExecuteALLCmds
       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.
       ExecuteALLCmds
       return 0
   fi
   return 1
}

ExecuteInProperEnvironment || exit 1


exit 0
