#!/bin/sh
# $Header: autoconfig 1.5 90/09/06 $
# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.
#
# Determine if any new devices are supported by the kernel and create
# the necessary device nodes in /dev.

ACTION=$1

#
# get  current state of the init process
#
set `who -r`

#
# only allow processing from SINGLE and MULTI-USER states to occur
#
case $9 in
	S | 2)
		;;
	*)
		exit 0
		;;
esac

OLDDEVS=/etc/.dumpconf.out
NEWDEVS=/etc/.dumpconf.new
#
# /etc/.bootstring is created by /etc/init.d/bootflags with the booted
# values before the permanent bootflags get reset.
#
if [ -f /etc/.bootstring ]; then
	UNIX=/`cat /etc/.bootstring | sed -n '/^n0/s/.*)\([^ 	]*\).*/\1/p'`
fi
#
# just in case the bootstring file exists but is hosed...
#
UNIX=${UNIX:=/unix}

#
# First argument is name of output file
# If no first argument specified, $OLDDEVS will be used
#
get_devs() {
	/etc/dumpconf -u $UNIX -d 2> /dev/null |
	/bin/sed -n '/^[^#]/ s/^\([^ 	]*\)[ 	]*\([^ 	]*\).*/\1\2/p' |
	/bin/sort > ${1:-$OLDDEVS}
}

case $ACTION in

#
# Check for devices configured in the kernel that
# did not exist at last powerdown
#
start)
	if [ -s $OLDDEVS ]; then

		get_devs $NEWDEVS

		# Create necessary device nodes
		MK_LIST=`comm -13 $OLDDEVS $NEWDEVS`
		if [ "$MK_LIST" ]; then
			echo Creating device nodes for $MK_LIST
			cd /dev
			sh ./MAKEDEV $MK_LIST 2> /dev/null
		fi

		# Remove the useless device nodes
		RM_LIST=`comm -23 $OLDDEVS $NEWDEVS`
		if [ "$RM_LIST" ]; then
			echo Removing device nodes for $RM_LIST
			cd /dev
			sh ./MAKEDEV -n $RM_LIST 2> /dev/null |
			egrep '^mknod' |
			while read mknod dev junk
			do
				echo $dev
			done |
			xargs rm -f
		fi
		
		rm -f $OLDDEVS $NEWDEVS
	fi
	;;

#
# Save the list of devices configured in the kernel to compare
# with devices at next boot.
#
stop)
	get_devs $OLDDEVS
	;;

esac
