# Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
#
# ident	"@(#)144501_postpatch.sh	1.1	11/09/16 SMI"
#
# Provide fixed definitions on patchadd execution.

### BEGIN: overlay 144501-19/postpatch
if [ "${PatchNum}" = "144501-19" -a `/usr/bin/basename $0` = "postpatch" ] ; then
postpatch()
{
PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
PKGCOND=/usr/bin/pkgcond
ARCH=`uname -p`

is_pcfs_boot=yes
is_zfs_boot=no

fix_cputrak () {
	 cd $ROOTDIR/usr/bin
	/usr/bin/rm -f $ROOTDIR/usr/bin/cputrack
	ln ../lib/isaexec cputrack
}
/usr/bin/touch $ROOTDIR/usr/bin/.test.$$ > /dev/null 2>&1 && {
	fix_cputrak
       /usr/bin/rm $ROOTDIR/usr/bin/.test.$$ > /dev/null 2>&1
}

[ -s "$ROOTDIR/platform/i86pc/multiboot" -a -s "$ROOTDIR/boot/multiboot" ] && cp -p "$ROOTDIR/platform/i86pc/multiboot" "$ROOTDIR/boot/multiboot"




check_pcfs_boot()
{
	bootdev=`grep -v "^#" "${ROOTDIR}/etc/vfstab" | grep pcfs \
		| grep "[ 	]/stubboot[ 	]" | nawk '{print $1}'`
	if [ X"$bootdev" = "X" ]; then
		is_pcfs_boot=no
	fi
}

check_zfs_boot()
{
	if [ -f "${ROOTDIR}/etc/lu/GRUB_slice" ]; then
		dev=`grep '^PHYS_SLICE=' "${ROOTDIR}/etc/lu/GRUB_slice" | \
			cut -d= -f2`
		if [ "`fstyp $dev`" = "zfs" ]; then
			is_zfs_boot=yes
		fi
	else
		rootfstype=`df -n ${ROOTDIR:-/} | awk '{print $NF}'`
		if [ "$rootfstype" = "zfs" ]; then
			is_zfs_boot=yes
		fi
			
	fi
}

#
# Detect SVM root and return the list of raw devices under the mirror
#
get_rootdev_list()
{
	if [ -f "${ROOTDIR}/etc/lu/GRUB_slice" ]; then
		dev=`grep '^PHYS_SLICE' "${ROOTDIR}/etc/lu/GRUB_slice" | \
			cut -d= -f2`
		if [ "$is_zfs_boot" = "yes" ]; then
			fstyp -a "$dev" | grep 'path: ' | grep -v phys_path: | \
				cut -d"'" -f2 | sed 's+/dsk/+/rdsk/+'
		else
			echo "$dev"
		fi
		return
	elif [ "$is_zfs_boot" = "yes" ]; then
		rootpool=`df -k ${ROOTDIR:-/} | tail +2 | cut -d/ -f1`
		rootdevlist=`LC_ALL=C  zpool iostat -v "$rootpool" | tail +5 | \
			grep -v "mirror|spare|replacing" | 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=`/usr/bin/basename "$metadev"`
		elif [[ $metadev = /dev/md/rdsk/* ]]; then
			metavol=`/usr/bin/basename "$metadev"`
			rootdevlist=`metastat -p $metavol | \
				grep -v "^$metavol[	 ]" | nawk '{print $4}'`
		fi
	fi
	for rootdev in $rootdevlist
	do
		echo /dev/rdsk/`/usr/bin/basename $rootdev`
	done
}

#
# multiboot: install grub on the boot slice
#
install_grub()
{
	# Stage 2 blocks must remain untouched
	STAGE1="${ROOTDIR}/boot/grub/stage1"
	STAGE2="${ROOTDIR}/boot/grub/stage2"

	if [ $is_pcfs_boot = yes ]; then
		#
		# Note: /stubboot/boot/grub/stage2 must stay untouched.
		#
		mkdir -p "${ROOTDIR}/stubboot/boot/grub"
		cp "${ROOTDIR}/boot/grub/menu.lst" "${ROOTDIR}/stubboot/boot/grub"
		bootdev=`grep -v "^#" "${ROOTDIR}/etc/vfstab" | grep pcfs | \
			grep "[	 ]/stubboot[ 	]" | nawk '{print $1}'`
		rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`
		if [ X"$rpcfsdev" != X ]; then
			print "Installing grub on $rpcfsdev"
			"${ROOTDIR}/sbin/installgrub" $STAGE1 $STAGE2 $rpcfsdev
		fi
	fi

	grubdevlist=`get_rootdev_list`
	zfsarg=""
	if [ "$is_zfs_boot" = "yes" ]; then
		zfsarg="-Z"
	fi

	for rootdev in $grubdevlist
	do
		if [ X"$rpcfsdev" != X ]; then
			echo "Creating GRUB menu in ${MENUDIR}/stubboot"
			"${MENUDIR}/sbin/bootadm" update-menu $zfsarg \
				-R "${MENUDIR}/stubboot" \
				-o "$rootdev,${MENUDIR}" 2>/dev/null
		else
			echo "Creating GRUB menu in ${MENUDIR:-/}"
			"${MENUDIR}/sbin/bootadm" update-menu -R "${MENUDIR:-/}" \
				$zfsarg -o $rootdev 2>/dev/null
		fi
		print "Installing grub on $rootdev"
		"${ROOTDIR}/sbin/installgrub" $STAGE1 $STAGE2 $rootdev
	done
}


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

if [ -f "${MENUDIR}/platform/i86pc/multiboot" ] ; then
	check_pcfs_boot
	check_zfs_boot
	install_grub
fi

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

} # end of determine_pkgcond function
#as we already require patches that require 119254-51 we already have pkgcond
determine_pkgcond || exit 1
}
fi
### END: overlay 144501-19/postpatch
