#!/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.
postpatch()
{
PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
PKGCOND=/usr/bin/pkgcond
KARCH=`uname -m`
ROOTFSTYPE=`df -n $ROOTDIR | awk '{print $3}'`

get_rootdev_list()
{
	if [ -f $ROOTDIR/etc/lu/GRUB_slice ]; then
		dev=`grep '^PHYS_SLICE' $ROOTDIR/etc/lu/GRUB_slice | \
			cut -d= -f2`
		if [ "$rootfstype" = "zfs" ]; then
			fstyp -v "$dev" | grep 'path: ' | grep -v phys_path: | \
				cut -d"'" -f2 | sed 's+/dsk/+/rdsk/+'
		else
			echo "$dev"
		fi
		return
	elif [ "$rootfstype" = "zfs" ]; then
		rootpool=`df -k ${ROOTDIR:-/} | tail +2 | cut -d/ -f1`
		rootdevlist=`zpool iostat -v "$rootpool" | tail +5 | \
			grep -v mirror | sed -n -e '/--/q' -e p | awk '{print $1}'`
	else
		metadev=`grep -v "^#" $ROOTDIR/etc/vfstab | \
			grep "[	 ]/[	 ]" | nawk '{print $2}'`
		if [[ $metadev = /dev/rdsk/* ]]; then
			rootdevlist=`echo "$metadev" | sed -e "s#/dev/rdsk/##"`
		elif [[ $metadev = /dev/md/rdsk/* ]]; then
			metavol=`echo "$metadev" | sed -e "s#/dev/md/rdsk/##"`
			rootdevlist=`metastat -p $metavol |\
				grep -v "^$metavol[	 ]" |\
				nawk '{print $4}' | sed -e "s#/dev/rdsk/##"`
		fi
	fi
	for rootdev in $rootdevlist ; do
		echo /dev/rdsk/$rootdev
	done
}

install_bootblk()
{
	bootblk=$ROOTDIR/platform/$KARCH/lib/fs/$ROOTFSTYPE/bootblk
	rootslice=`df -k $ROOTDIR | nawk 'NR > 1 { print $1 }' | \
		sed 's#/dsk/#/rdsk/#'`
	if [[ "$ROOTFSTYPE" = "zfs" ]]; then
		print "Detected ZFS root."
		get_rootdev_list | while read physlice
		do
			print "Installing bootblk on $physlice"
			$ROOTDIR/usr/sbin/installboot -F zfs $bootblk
		done
	elif [[ "$rootslice" = /dev/rdsk/* ]]; then
		print "Installing boot block on $rootslice."
		installboot $bootblk $rootslice
	elif [[ "$rootslice" = /dev/md/rdsk/* ]]; then
		print "Detected SVM root."
		get_rootdev_list | while read physlice
		do
			print "Installing bootblk on $physlice"
			installboot $bootblk $physlice
		done
	elif [[ "$rootslice" = /dev/vx/rdsk/* ]]; then
		print "Installing boot block on $rootslice."
		installboot $bootblk $rootslice
	elif [[ "$rootslice" = /dev/sfdsk/* ]]; then
		print "Installing boot block on $rootslice."
		installboot $bootblk $rootslice
	elif [[ "$rootslice" = /dev/FJSVmplb/rdsk/* ]]; then
		print "Installing boot block on $rootslice."
		installboot $bootblk $rootslice
	fi
}

build_boot_archive()
{
   if [ "$SAFEMODE_INSTALL" = "true" ] ; then
      REALROOT=/
   else
      REALROOT=${ROOTDIR}
   fi

   $ROOTDIR/boot/solaris/bin/create_ramdisk -R $ROOTDIR

   FAILSAFE_ALSO_FOR="sun4v sun4us"
   if [ ! -f "${REALROOT}/platform/sun4u/failsafe" ]; then
      cp  "$patchdir/failsafe_archive" "${REALROOT}/platform/sun4u/failsafe"
      for failsafe_platform in ${FAILSAFE_ALSO_FOR} ; do
         if [ -d "${REALROOT}/platform/${failsafe_platform}" ] ; then
           ln -f "${REALROOT}/platform/sun4u/failsafe" "${REALROOT}/platform/${failsafe_platform}/failsafe"
         fi
      done
   fi 

   if [ "$SAFEMODE_INSTALL" = "true" ] ; then
      PATCH_COMMON_LIB="/usr/lib/patch/patch_common_lib"
      . $PATCH_COMMON_LIB
      InitSafemode
      cp $patchdir/bootadm $SAFEMODE_OVERLAY_DIR/sbin/bootadm
   fi

   return 0
}

ExecuteALLCmds()
{
#
# MAIN
#
   install_bootblk
   build_boot_archive
   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
postpatch "$@"

exit 0
