#!/bin/ksh

# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

# Definitions for override safety mechanism
PATCH_OVERRIDE_LIB="${ROOTDIR}/usr/lib/patch/patch_override_lib"


# script in a fucntion so that it can be overridden if needed.
prepatch()
{
PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
PKGCOND=/usr/bin/pkgcond
KARCH=`uname -m`
ROOTFSTYPE=`df -n $ROOTDIR | awk '{print $3}'`

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/sfdsk/* || "$rootslice" = /dev/FJSVmplb/rdsk/* || "$ROOTFSTYPE" = "zfs" ]] ; then
		:
	else
		print "unsupported root slice type, $rootslice"
		exit 1
	fi

	return 0
}


ExecuteALLCmds()
{
#
# MAIN
#
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
}

# load the override lib if it exists
[ -f "${PATCH_OVERRIDE_LIB}" ] && . "${PATCH_OVERRIDE_LIB}"

# execute the script function
prepatch "$@"

exit 0
