#!/bin/ksh -p
## Patch is the minimum path number that is required
## This assumes that the patch also includes a regular SUNW_REQUIRES for patch

PATH=/usr/bin:/usr/sbin:$PATH; export PATH
PKGCOND=/usr/bin/pkgcond


not_installed() {
        driver=$1
        grep "^${driver} " $ROOTDIR/etc/name_to_major > /dev/null 2>&1

        return $?
}

backup_sdconf() {
  if [ -f $ROOTDIR/kernel/drv/sd.conf ]; then
    if [ -f $ROOTDIR/var/sadm/pkg/SUNckr/save/sd.conf ]; then
      cp $ROOTDIR/kernel/drv/sd.conf \
       $ROOTDIR/var/sadm/pkg/SUNWckr/save/sd.conf.$PatchNum.$$
      else
	cp $ROOTDIR/kernel/drv/sd.conf \
       $ROOTDIR/var/sadm/pkg/SUNWckr/save/sd.conf.$PatchNum
    fi
  fi
}


update_logindevperm() {
        cp $ROOTDIR/etc/logindevperm $ROOTDIR/etc/logindevperm.before-$PatchNum

}

load_mods() {
        ## load modules during patch installation per 6349240
        ## to prevent warnings from displaying on the console
        ## before the system is rebooted.

	cd  ${patchdir:-$PWD}

        ## find all kernel driver modules in patch
	cat */pkgmap|grep "\ kernel/drv"|grep "1 f"|nawk '{print $4}' |grep -v "\.conf" > /tmp/modules.$$

	while read line; do
                ## preload each module
		[ -f /$line ] && {
			echo "loading module $line"
			modload /$line > /dev/null 2>&1
		}
	done < /tmp/modules.$$

	rm -f /tmp/modules.$$
}


Execute_commands() {

   update_logindevperm
   backup_sdconf

   return 0
}

LocalZones () {
# commands specific to non-gloabl zones
return 0

}

ExecuteInProperEnvironment () {
   if $PKGCOND is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
       # Execute non-global whole root zone commands.
	update_logindevperm
       return 0
   fi

   if $PKGCOND is_nonglobal_zone > /dev/null 2>&1 ; then
       # Execute non-global zone commands. Should be no action here
	update_logindevperm
       return 0
   fi

   if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; then
       # Execute commands applicable to patching the mini-root.
	update_logindevperm
       return 0
   fi

   if $PKGCOND is_mounted_miniroot > /dev/null 2>&1 ; then
       # Execute commands specific to the mini-root
	update_logindevperm	
	return 0
   fi

   if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
       # Execute commands specific to diskless client
	Execute_commands
       return 0
   fi

   if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
       # Execute commands specific to an alternate root
       Execute_commands
       return 0
   fi

   if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
       # In a global zone and system is mounted on /.
     load_mods
     Execute_commands
       return 0
   fi
   return 1
} 

ExecuteInProperEnvironment && exit 0 || exit 1

exit 0

