#!/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.
postbackout()
{
PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH

KARCH=`uname -m`
ROOTFSTYPE=`df -n $ROOTDIR | awk '{print $3}'`
USR=${ROOTDIR%/}/usr
PKGCOND=/usr/bin/pkgcond

get_rootdev_list()
{
	if [ -f $ROOTDIR/etc/lu/GRUB_slice ]; then
		dev=`grep '^PHYS_SLICE' $ROOTDIR/etc/lu/GRUB_slice |
			cut -d= -f2`
		echo "$dev"
		return
	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()
{
	if [[ "$ROOTFSTYPE" = ufs ]]; then
		rootslice=`df -k $ROOTDIR | nawk 'NR > 1 { print $1 }' | \
		    sed "s/\/dsk\//\/rdsk\//"`
		bootblk=$USR/platform/$KARCH/lib/fs/ufs/bootblk
	fi

	if [[ "$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

}

remove_boot_archive()
{
	rm -f $ROOTDIR/platform/$KARCH/boot_archive > /dev/null 2>&1

	FAILSAFE_ARCHS="sun4u sun4v sun4us"
	for failsafe_platform in ${FAILSAFE_ARCHS} ; do
		rm -f "$ROOTDIR/platform/${failsafe_platform}/failsafe" > /dev/null 2>&1
	done
}

ExecuteALLCmds()
{

#
# MAIN
#
install_bootblk
remove_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

}
determine_pkgcond || exit 1
}

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

# execute the script function
postbackout "$@"

exit 0
