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

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

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
   }

get_rootdev_list()
{
	if [ "$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/`/usr/bin/basename $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 $physlice
		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/vd/vdisk* ]]; then
		rootslice=`echo $rootslice | sed 's#/vdisk#/rvdisk#'`
		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
}

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
}
fi
### END: overlay 144500-19/postpatch
