#!/bin/sh

# Copyright (c) 2005 by Sun Microsystems, Inc.
# All rights reserved

# Global declarations
PKGCOND=/usr/bin/pkgcond
NAWK="/bin/nawk"
DIRNAME="/bin/dirname"
PDO_REBOOT_LOC="/var/run/.solpatchsys/.pdo.patch-reboot-required.lock"

## save kbtrans pkgmap 
mypatchid=${PatchNum}
rmpkg=SUNWusb
kb_pkgmap=$rmpkg.pkgmap.$mypatchid
if  test -s /tmp/$kb_pkgmap ; then
        mv /tmp/$kb_pkgmap $ROOTDIR/var/sadm/patch/$mypatchid/$kb_pkgmap
fi

AlternateRoot()
# returns 0 if $PKG_INSTALL_ROOT -
{
	if [ -n "$ROOTDIR" ]; then
		if  [ "/" = "`echo $ROOTDIR|$NAWK '{gsub(/\/+/,"/"); print}'`" ]; then
			return 1
		else
			return 0
		fi
	else
		return 1
	fi
}

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

CheckZones()
{
        if [ "$ROOTDIR" = "/" -a -x /usr/bin/zonename ]; then
                ZONENAME=`/usr/bin/zonename`
                if [ ${ZONENAME} = "global" ]; then
                        GLOBAL_ZONE=true
                else
                        GLOBAL_ZONE=false
                fi
        else
                # Unable to determine zone
                GLOBAL_ZONE=true
        fi
}

LocalZones()
{
# Any local zone stuff here,
# typically this fuction is not used
        return 0
}

restart_services() {

	platform=`uname -i`
	starcat="SUNW,Sun-Fire-15000"
	if [ ${platform} != "${starcat}" ]; then
		return
	fi

	svcadm restart svc:/platform/sun4u/dcs:default

	return 0
}
 

## mount_libc_psr () {
## 	# If architecture is not sun4v then do not mount the libc_psr_hwcap1 library.
## 	ARCH=`uname -m`
## 	if [ $ARCH != "sun4v" ];then
## 		return
## 	fi
## 
## 	PLAT=`uname -i`
## 	LIBC_MOE_32=`moe -32 /platform/$PLAT/lib/libc_psr/'$HWCAP'`
## 	if [ -n "$LIBC_MOE_32" ]; then
## 		mount | egrep -s "^/platform/[^/]*/lib/libc_psr.so.1 on "
## 		if [ $? -ne 0 ]; then
## 			mount -O -F lofs $LIBC_MOE_32 /platform/$PLAT/lib/libc_psr.so.1
## 		fi
## 	fi
## 
## 	LIBC_MOE_64=`moe -64 /platform/$PLAT/lib/sparcv9/libc_psr/'$HWCAP'`
## 	if [ -n "$LIBC_MOE_64" ]; then
## 		mount | egrep -s "^/platform/[^/]*/lib/sparcv9/libc_psr.so.1 on "
## 		if [ $? -ne 0 ]; then
## 			mount -O -F lofs $LIBC_MOE_64 \
## 			    /platform/$PLAT/lib/sparcv9/libc_psr.so.1
## 		fi
## 	fi
## 
## }

ExecuteAlternateCmds()
{
##  mount_libc_psr

 # Add any global zone specific stuff here
 # ie add_drv, or mucking about with /etc/system, or similar

        EXIT=0

        not_installed us ||
            add_drv -b ${ROOTDIR} -i 'SUNW,UltraSPARC-III "SUNW,UltraSPARC-III+" SUNW,UltraSPARC-IIIi "SUNW,UltraSPARC-IIIi+" SUNW,UltraSPARC-IIe' -n us || \
            EXIT=1

        if [ -f /tmp/backoutmods.$PatchNum ]; then
           mv /tmp/backoutmods.$PatchNum $ROOTDIR/var/sadm/patch/$PatchNum/backoutmods
        fi

        return ${EXIT}

}


ExecuteWholeNonGlobalCmds()
{
 restart_services
## mount_libc_psr

 if [ -f /tmp/backoutmods.$PatchNum ]; then
   mv /tmp/backoutmods.$PatchNum $ROOTDIR/var/sadm/patch/$PatchNum/backoutmods
 fi

}


ExecuteALLCmds()
{
 restart_services
## mount_libc_psr

 # Add any global zone specific stuff here
 # ie add_drv, or mucking about with /etc/system, or similar

        EXIT=0

        not_installed us ||
            add_drv -b ${ROOTDIR} -i 'SUNW,UltraSPARC-III "SUNW,UltraSPARC-III+" SUNW,UltraSPARC-IIIi "SUNW,UltraSPARC-IIIi+" SUNW,UltraSPARC-IIe' -n us || \
            EXIT=1

        if [ -f /tmp/backoutmods.$PatchNum ]; then
           mv /tmp/backoutmods.$PatchNum $ROOTDIR/var/sadm/patch/$PatchNum/backoutmods
        fi

        return ${EXIT}

}

determine_pkgcond () {

  if $PKGCOND  is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
      # Execute non-global whole root zone commands.
      # Should be same action as the default action.
      ExecuteWholeNonGlobalCmds
      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
      ExecuteAlternateCmds
      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

}

# Main

ZONENAME=global

[ -x /sbin/zonename ] && ZONENAME=`/sbin/zonename`

if [ -x "$PKGCOND" ] ; then
  determine_pkgcond
else
  CheckZones
  if [ "${GLOBAL_ZONE}" = "true" ]; then
       ExecuteALLCmds
  else 
       LocalZones
  fi 
fi

##
## Disable pdo, so after this patch installation reboot *MUST* happen
##
if AlternateRoot; then
	:
else
	cat > /var/run/nopatch <<EOF
#!/sbin/sh
echo "You must perform a reconfigure reboot before invoking any additional patch commands." >&2
exit 1
EOF
	chmod +x /var/run/nopatch
	mount -O -F lofs /var/run/nopatch /usr/lib/patch/pdo
fi

exit 0 
