#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_atm_boot.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1997,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)14 1.9 src/bos/usr/lib/nim/methods/c_atm_boot.sh, cmdnim, bos720 2/26/04 09:13:01
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_atm_boot.sh
#		
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993, 1995
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

#---------------------------- local defines     --------------------------------

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="server c_info boot_file"
OPTIONAL_ATTRS="no_client_boot"

# Define mount point for boot file.
boot_file_access="${TMPDIR}/bf_access"
pre52_booti=
MOVED_CUPATH=

PREP_DISKNET_BOOT="/usr/lib/boot/bin/prep_disknet_boot"


############################################################################
#
#  Name:        c_atm_boot
#
#  Function:    Sets up access to a remote boot image, then calls
#               prep_disknet_boot
#
#  Returns:     Nothing
#
#  Parameters:  None
#
############################################################################

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# set parameters from command line
while getopts a:pqvf c
do
  case ${c} in

    a)    # validate the attr ass
          parse_attr_ass "${OPTARG}"

	  # include the assignment for use in this environment
          eval ${variable}=\"${value}\"
          ;;

    p)    # pre AIX 5.2 boot image
          pre52_booti="yes"
          ;;

    q)    # show attr info
          cmd_what
          exit 0
          ;;

    v)    # verbose mode (for debugging)
          set -x
          for i in $(typeset +f)
          do
            typeset -ft $i
          done
          ;;

    \?)   # unknown option
          error ${ERR_BAD_OPT} ${OPTARG}
          ;;

  esac
done

# check for missing attrs
ck_attrs

# Mount the client boot image.
nim_mount ${server}:${boot_file} ${boot_file_access}

server_ip=`${HOSTCMD} ${server} | ${AWK} '{print $3}'`
server_ip=${server_ip%,}

# If the boot image is pre-AIX 5.2 and the client is AIX 5.2 or greater,
# then null out the CuPath so that savebase does not corrupt the boot image
if [[ -n $pre52_booti ]]
then
	get_fileset_level ${NIM_CLIENT_PACKAGE}
	if (( (( (( ${version} == 5 )) && (( ${release} >= 2 )) )) || (( ${version} > 5 )) ))
	then
		${CP} /etc/objrepos/CuPath /etc/objrepos/CuPath.orig >/dev/null 2>&1
		${CP} /etc/objrepos/CuPath.vc /etc/objrepos/CuPath.vc.orig >/dev/null 2>&1
		${ODMDELETE} -o CuPath >/dev/null 2>&1
		MOVED_CUPATH="yes"
	fi
fi


# Call prep_disknet_boot to get the machine ready for reboot and
# reinstall.
# We can assume that any output from prep_disknet_boot is an error
# message.  Make sure it goes to stderr so the master can pick it up.
${PREP_DISKNET_BOOT} -b ${boot_file_access} -c ${c_info} -s ${server_ip} 1>&2

# If an error was returned by prep_disknet_boot, then exit now.
# The problem will need to be fixed before we can continue.
if [[ $? -ne 0 ]]
then
  exit -1
fi

# Restore the CuPath database
if [[ -n $MOVED_CUPATH ]]
then
	${MV} /etc/objrepos/CuPath.orig /etc/objrepos/CuPath >/dev/null 2>&1
	${MV} /etc/objrepos/CuPath.vc.orig /etc/objrepos/CuPath.vc >/dev/null 2>&1
fi

# It is time to start the reboot stuff now.
# If the user forced a 'no_client_boot=yes' through then we should
# skip the reboot and just return.
if [[ ${no_client_boot} != 'yes' ]]
then

  #
  # Wall the users... 
  #

  ${C_ERRMSG} ${MSG_REBOOT_WARNING} ${C_ERRMSG_MSG} "" "" "" "" > /tmp/msg 2>&1
  ${CAT} /tmp/msg | ${WALL} 
  #
  ${SLEEP} 4
  #
  ${CAT} /tmp/msg | ${WALL} 
  #
  ${RM} /tmp/msg 

  # update the state of the client object
  ${NIMCLIENT} -S shutdown

  # Reboot
  # The reboot must occur in an at job because otherwise it will
  # kill the rsh and hang the master.  The model type is checked
  # because some models were hanging on the reboot command so a
  # shutdown must be performed for them instead.
  MAC_MODEL=`${UNAME} -m 2>/dev/null | ${CUT} -c 9-10 2>/dev/null`
  if [[ $MAC_MODEL = "4C" ]]
  then
    echo "/usr/sbin/shutdown -Fr &" > /tmp/__NIM_shutdown
  else
    echo "/usr/sbin/reboot -q &" > /tmp/__NIM_shutdown
  fi
  ${CHMOD} 755 /tmp/__NIM_shutdown
  ${AT} now + 1 minute < /tmp/__NIM_shutdown > /dev/null 2>&1

fi

# success
exit 0
