# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)137137_prepatch.sh	1.3	10/02/25 SMI"
#
# Provide fixed definitions on patchadd execution.

### BEGIN: overlay 137137-09/prepatch
if [ "${PatchNum}" = "137137-09" -a `/usr/bin/basename $0` = "prepatch" ] ; then
prepatch()
{
PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
PKGCOND=/usr/bin/pkgcond
KARCH=`uname -m`
ROOTFSTYPE=`df -n $ROOTDIR | awk '{print $NF}'`

check_newboot_space()
{
    [ -s "$ROOTDIR/platform/$KARCH/boot_archive" ] && return 0
    
    ku_min_space_root=`expr 11000 + 85000 + 85000`  # space for : patch payload + boot archive build + boot archive
    ku_min_space_usr=10000
    ku_min_space_var=90000
    
    root_space_available=`df -k "$ROOTDIR" | nawk 'NR == 2 { print $4 }'`

    root_space_needed="$ku_min_space_root"
    df -k "$ROOTDIR/usr" | grep "/usr$" > /dev/null || root_space_needed=`expr $root_space_needed + $ku_min_space_usr`
    df -k "$ROOTDIR/var" | grep "/var$" > /dev/null || root_space_needed=`expr $root_space_needed + $ku_min_space_var`
    
    [ "$root_space_available" -lt "$root_space_needed" ] && {
	REALROOT="$ROOTDIR"
	[ "$SAFEMODE_INSTALL" = "true" ] && REALROOT="/"
	echo
	echo "There is insufficient free space in the '$REALROOT' filesystem to transition"
	echo "to the new boot architecture."
	echo
	echo "The '$REALROOT' filesystem has ${root_space_available}K of space free, which is below the"
	echo "estimated ${root_space_needed}K of space needed to apply this patch safely."
	echo
	exit 1
    }
    
    return 0
}

install_bootblk()
{
	if [[ "$ROOTFSTYPE" != "ufs" && "$ROOTFSTYPE" != "zfs" ]]; then
		print "Unsupported root filesystem: $ROOTFSTYPE"
		exit 1
	else
		rootslice=`df -k $ROOTDIR | nawk 'NR > 1 { print $1 }' | \
			sed "s/\/dsk\//\/rdsk\//"`
	fi


	if [[ "$rootslice" = /dev/rdsk/* || "$rootslice" = /dev/md/rdsk/* || "$rootslice" = /dev/vx/rdsk/* || "$rootslice" = /dev/vd/vdisk* || "$rootslice" = /dev/sfdsk/* || "$rootslice" = /dev/FJSVmplb/rdsk/* || "$ROOTFSTYPE" = "zfs" ]] ; then
		:
	else
		print "unsupported root slice type, $rootslice"
		exit 1
	fi

	return 0
}


ExecuteALLCmds()
{
#
# MAIN
#
check_newboot_space
install_bootblk
return 0
}


determine_pkgcond () {

  if $PKGCOND  is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
      # Execute non-global whole root zone commands.
      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

}

#as we already require patches that require 119254-51 we already have pkgcond
determine_pkgcond || exit 1
}
fi
### END: overlay 137137-09/prepatch
