#!/sbin/sh
# @(#) $Revision: 78.4 $
#
# This script first checks if the system is running from a local root or 
# is running as an NFSD client. For local root systems, it checks if the 
# boot filesystem must be mounted to /stand, as is the case for separate
# boot/root filesystems; for NFSD clients, it starts the nfs daemons.
# It then checks to see if /etc/ioconfig exists. ioinitrc will invoke a
# shell at the console if /etc/ioconfig is absent.  Otherwise ioinitrc
# invokes ioinit with the -i and -r options.
#
# NOTE: This script is not configurable!  Any changes made to this script
#       will be overwritten when you upgrade to the next release of HP-UX.
#
# ###########################################################################

# trap "" 2

#
# The variable DOECHO controls the echoing of NON-error messages.
# To turn off these "progress" messages set DOECHO=FALSE.
#
DOECHO="TRUE"


if [ "$DOECHO" = "TRUE" ]
then
   echo "/sbin/ioinitrc:"
fi

rval=0

#
# Do we have access to the boot file system?  Initially, we assume that
# we do (but we might decide differently below).
#
BOOTFS="TRUE"

#
# For local root systems, mount the boot partition so it can be read/updated.
# For NFSD (non-local root), necessary partitions are already mounted so
# just start necessary NFSD services.  The boot partition is first checked
# and repairs that can be made without operator interaction are made;
# if a more rigorous fsck is required it will be done later in bcheckrc.
#
if [ -x /sbin/is_local_root ]  
then 
   if /sbin/is_local_root

   then 	# local root disk
      NFS_DISKLESS=0
      FSTAB="/etc/fstab"
      BOOTDIR="/stand"
      while read LINE				# read the list of mounts
      do
         # skip blank or comment lines
         if [ "${LINE}" = "" -o "${LINE}" != "${LINE#\#}" ]
         then
            continue
         fi 
         set $LINE
         if [ "$2" = "$BOOTDIR" ] 		# separate boot partition
         then
            # Now try to check and then mount the /stand partition.
            # A non-rigorous and non-interactive fsck -P is performed,
            # this should catch and repair some instances of a dirty disk.
            # If the fsck (and then the mount) fails, the root partition 
            # will be more rigorously checked later in bcheckrc.
            # Note also that the fsck is done to the block-mode device
            # since that is all we know about from the entry in /etc/fstab.
            /sbin/fsck -F hfs -P $1     	# check+repair w/out data loss
            /sbin/mount -o $4 $1 $2		# mount  
            if [ $? != 0 ]
            then 
               echo "Unable to mount $BOOTDIR - please check entries in $FSTAB"
               BOOTFS="FALSE"
            else
               # if it was a separate /stand volume, then system is also
               # configured for LVM, so run LVM consistency checks now
               /sbin/lvlnboot -c
            fi
         fi
      done < $FSTAB

   else 	# NFSD client - start NFS daemons
      NFS_DISKLESS=1
      if [ -r /etc/TIMEZONE ]
      then 
        . /etc/TIMEZONE 2>/dev/null
      fi
      echo "\tStarting NFS lock daemons: \c"
      echo "portmap \c"
      /usr/sbin/portmap
      echo "statd \c"
      /usr/sbin/rpc.statd
      echo "lockd"
      /usr/sbin/rpc.lockd -g0
   fi
fi


# 
# check if this is a new installation
#
rmdir /etc/.first_boot 2>/dev/null
INSTALL=$?

# 
# check consistency of ioconfig file and update of necessary
#
if [ -f /etc/ioconfig ]
then
    #
    # Don't do anything if /stand isn't mounted, because we can't
    # even see /stand/ioconfig in that case.  The most likely reason
    # for this would be an LVM maintenance mode boot.  Otherwise,
    # invoke ioinit to compare /stand/ioconfig with /etc/ioconfig,
    # and to create new device files (if needed).
    #
    if [ "$BOOTFS" = "TRUE" ]
    then
        #
        # Call to get framebuf console created "correctly" for X
        #
        if [ ! -c /dev/crt ]
        then
            /sbin/insf -e -d framebuf
        fi
        /sbin/ioinit -i -r
        rval=$?
    fi
else
#
#   If nfs diskless and it's the first boot, /etc/ioconfig will
#   not exist.  We will create /etc/ioconfig in this case by a
#   calling ioinit -c
#
    if [ $NFS_DISKLESS -ne 0 -a $INSTALL -eq 0 ]
    then
	/sbin/ioinit -c
	/sbin/ioinit -i
	rval=$?
    else

        echo "/etc/ioconfig is missing.  Restore it from backup or "
        echo "invoke /sbin/ioinit -c to recreate it from the kernel."
	echo "When finished enter ^D to continue boot."
        PS1="(in ioinitrc)# "
        export PS1
        /sbin/sh
        #
        # Call to get framebuf console created "correctly" for X
        #
        if [ ! -c /dev/crt ]
        then
            /sbin/insf -e -d framebuf
        fi
    fi
fi

if [ "$DOECHO" = "TRUE" ]
then
   echo 		# for better console readability on bootup screens
fi

exit $rval
