#!/bin/bash
#
# kudzu		This scripts runs the kudzu hardware probe.
#
# chkconfig: 345 05 95
# description: 	This runs the hardware probe, and optionally configures \
#		changed hardware.

# Source function library.
. /etc/init.d/functions

# This is an interactive program, we need the current locale
SourceIfNotEmpty /etc/sysconfig/i18n

# Do we want to do a safe probe?
KUDZU_ARGS=
SourceIfNotEmpty /etc/sysconfig/kudzu

[ "$SAFE" = no ] || KUDZU_ARGS="-s"
[ -z "$aurora" ] || KUDZU_ARGS="$KUDZU_ARGS -q"

LOCKFILE=/var/lock/subsys/kudzu
HARDLOCKFILE=/var/lock/subsys/harddrake
RETVAL=0

case "$1" in
  start)
	if [ ! -f $HARDLOCKFILE ]; then
	echo -n "Checking for new hardware"
	rm -f /var/run/ranXconfig

	# Have a 20 second timeout.
 	/usr/sbin/kudzu $KUDZU_ARGS -t 20
	RETVAL=$?
	if [ "$RETVAL" -eq 0 ]; then
		action '' /bin/true
	else
		action '' /bin/false
		if [ "$RETVAL" -eq 5 ]; then
			echo -n "Hardware configuration timed out."
			echo -n "Run '/usr/sbin/kudzu' from the command line to re-detect."
			initlog -n kudzu -s "Hardware configuration timed out."
			initlog -n kudzu -s "Run '/usr/sbin/kudzu' from the command line to re-detect."
	   fi
	fi
	# We don't want to run this on random runlevel changes.
	touch "$LOCKFILE"
	# However, if they did configure X and want runlevel 5, let's switch to it...
	if [ -f /var/run/ranXconfig ]; then
		grep -qs "^id:5:initdefault:" /etc/inittab && telinit 5
		rm -f /var/run/ranXconfig
	fi
	fi
	;;

  status)
	[ -f "$LOCKFILE" ] && echo "Hardware have been configured" ||  echo "Hardware have not been configured"
	;;
  reload|restart|condrestart)
	;;
  stop|condstop)
 	rm -f "$LOCKFILE"
 	;;
 *)
 	echo "Usage: ${0##*/} {start|stop|status}"
	RETVAL=1
	;;
esac

exit $RETVAL
