shutdown
anyparm inparm=!['']
# option help
# uncomment the above line to allow hpcmdtrace to work
#---------------
#--                                               Version A.00
#-- This udc is designed to automate the shutdown and restart of
#-- 7.x mpe systems.  With 7.0, an enhanced shutdown command was
#-- introduced that allows the system to be shutdown and optionally
#-- restarted without doing a control-a (ie, any manual intervention).
#-- This capability becomes especially important in the event of a
#-- powerfailure where the ups is getting close to failing.  When
#-- the ups is configured, the system can be safely shutdown with
#-- no one at the console.  Of course, this udc can also be user for
#-- normal reboots.
#--
#-- Since this is a 'rather powerful' script, it supports the use
#-- of a special boolean variable 'shutdown_testing'.  When set to
#-- true, no 'destructive' commands will actually be performed.
#--
#-- Three types of shutdowns are supported in this script.  A 'normal'
#-- shutdown is performed by the operator and allows for the orderly
#-- halting of all background jobs prior to doing the actual shutdown.
#-- Its important to note that the type of shutdown cannot be specified.
#-- A 'shutdown system;restart' will be done automatically.
#--
#-- The second type of shutdown can only be done by the system manger
#-- and only if some kind of parameter is specified.  This is a
#-- drastic option because nothing is done to try to shutdown any
#-- background jobs or halt the network.  It is meant to be used by
#-- a well-informed system manager in an emergency.  You have been
#-- warned.
#--
#-- The third shutdown type is requested via a job.  Again, nothing
#-- is done to attempt an orderly shutdown.  The assumption is that
#-- it has already been handled.  Its worth noting that the shut-
#-- down command must be allowed if its not being performed on the
#-- actual console.  This implies that the command must be allowed
#-- to the user-id in the job.
#--
#-- To accomplish an orderly shutdown, this udc will ALPHABETICALLY
#-- XEQ all files found in the default group ".SDCMD.SYS".
#-- To override the command location, set the CI variable
#-- SHUTDOWN_CMD_LOC to the group (and optional account) holding your
#-- files.  Do take into consideration that Operator.Sys must
#-- have eXecute access to the group.  The timing of this phase is
#-- controlled by the scripts or programs in the shutdown command
#-- group.  Additionally, all error handling must happen inside
#-- the scripts or programs.  Finally, this udc is designed to
#-- use file in MPE-namespace.  Files in HFS-namespace will be
#-- ignored.
#---------------

#-- must be a sys user
if dwns(hpaccount) <> 'sys'
  return
endif

#-- capability check
setvar _sd_sm_or_op false
if (pos("OP",hpusercapf)>0) or (pos("SM",hpusercapf)>0)
  setvar _sd_sm_or_op true
endif

if not _sd_sm_or_op
  deletevar _sd_@
#-- note the silent return.  let's keep things mysterious....
  return
endif

#-- additional check for manager.sys
setvar _sd_sysman   false
if dwns(hpuser) = 'manager'
  setvar _sd_sysman true
endif

#-- special handling for input parameter
setvar _sd_inparm anyparm(!inparm)

#-- setting this variable to true allows for safe testing
#-- nothing destructive will be executed....neither will you
setvar _sd_testing false
if bound(shutdown_testing) and shutdown_testing
  setvar _sd_testing true
  echo
  echo *****************
  echo * T E S T I N G *
  echo *****************
  echo inparm = !_sd_inparm
  echo
endif

#-- a very specific combination is required
#-- designed to allow the system manager to kill the system
#-- in the event of a real emergency
#-- note that inparm must have some value in it
if hpinteractive          and &
   _sd_sysman             and &
   (hpldevin = hpconsole) and &
   _sd_inparm > ''
  echo
  echo WARNING: System Manager possibly bypassing normal shutdown
  if _sd_testing
    echo ...shutdown !_sd_inparm...
  else
    echo shutdown !_sd_inparm
    errclear
    continue
    shutdown !_sd_inparm
#-- the error handling here is important....
    if hpcierr <> 0
      echo
      echo ERROR: Shutdown command failed
      echo        !hpcierrmsg
      deletevar _sd_@
      escape 99
    endif
  endif
endif

#-- set up the location of the shutdown command group
setvar _sd_cmd_loc '.sdcmd.sys'
if bound(shutdown_cmd_loc)
  if str(shutdown_cmd_loc,1,1) = '.'
    setvar _sd_cmd_loc shutdown_cmd_loc
  else
    setvar _sd_cmd_loc '.'+shutdown_cmd_loc
  endif
endif

#-- this is where all the normal action takes place.  if the user
#-- is operator (regardless of any input parameter) or manager (with
#-- no input parameter(s) supplied), you'll be in this logic block.
if hpinteractive
  if hpldevin = hpconsole
    setvar _sd_reply ''
    while _sd_reply = ''
#-- one last sanity check....
      input _sd_reply;prompt='Confirm shutdown (Y/N) ';console
      setvar _sd_reply dwns(ltrim(str(_sd_reply,1,1)))
      if _sd_reply = 'y'
        file sdlf;temp
        if finfo('*sdlf','exists')
          purge sdlf,temp > $null
        endif
        errclear
        continue
        setvar _sd_listf '@'+_sd_cmd_loc
        listfile !_sd_listf,6>*sdlf
#-- udc 'sdproc' is at the bottom of this file
        if hpcierr = 0
          SDPROC < *sdlf
        else
          echo
          echo ERROR: Trouble doing listfile for !_sd_listf
          echo        !hpcierrmsg
          deletevar _sd_@
          escape !hpcierr
        endif
        echo *******************************************
        echo * ShutDown processing has completed       *
        echo * System will be halted and restarted now *
        echo *******************************************
        if _sd_testing
          echo ...shutdown system;restart...
        else
          echo shutdown system;restart
          errclear
          continue
          shutdown system;restart
#-- the error trapping here is probably not needed since we should
#-- never get here.  however, the belt-n-suspenders approach is
#-- still a good idea.
          if hpcierr <> 0
            echo
            echo ERROR: Shutdown command error
            echo        !hpcierrmsg
            deletevar _sd_@
            escape !hpcierr
          endif
        endif
      elseif _sd_reply = 'n' or _sd_reply = '0'
        echo
        echo WARNING: ShutDown was cancelled
      else
        echo
        echo ERROR: Unexpected reply given. No action taken.
        echo        To confirm: use 'Y'
        echo        To cancel:  use 'N' or '0'
        setvar _sd_reply ''
      endif
    endwhile
  else
    echo
    echo ERROR: Command can only be used on the console
    deletevar _sd_@
    escape 99
  endif
else
#-- we've gotten here because we're in a job...
  echo
  echo Shutting system down thru job....
  shutdown !_sd_inparm
endif

deletevar _sd_@
*****
sdproc
#---------
#-- This udc reads the listfile,6 of the group holding the shutdown
#-- scripts.
#---------
setvar _sp_eof finfo(hpstdin,'eof')
while setvar(_sp_eof,_sp_eof-1) >= 0
  if setvar(_sp_rec,rtrim(input())) > ''
    errclear
    continue
    xeq !_sp_rec
    if hpcierr <> 0
      echo
      echo ERROR: Trouble with !_sp_rec
      echo        !hpcierrmsg
      deletevar _sp_@
      escape !hpcierr
    endif
  endif
endwhile
deletevar _sp_@
*****