#!/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.

# This is an interactive program, we need the current locale

[ -f /etc/profile.d/lang.sh ] && . /etc/profile.d/lang.sh
[ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="`/sbin/consoletype`"

if [ "$CONSOLETYPE" != "pty" ]; then
	case "${LANG:-}" in
		ja_JP*|ko_KR*|zh_CN*|zh_TW*)
			export LANG=en_US
			;;
		*)
			export LANG
			;;
	esac
fi

if [ "$CONSOLETYPE" = "serial" ]; then
	export TERM=vt100
fi

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

KUDZU_ARGS=

# Do we want to do a safe probe?
. /etc/sysconfig/kudzu

if [ "$SAFE" != "no" ]; then
	KUDZU_ARGS="-s"
fi

RETVAL=$?

case "$1" in
 start)
	echo -n $"Checking for new hardware"
	rm -f /var/run/Xconfig
	rm -f /var/run/Xconfig-failed

        # Have a 30 second timeout.
 	/usr/sbin/kudzu $KUDZU_ARGS -t 30
	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 can now update the file system table w/o a hardware refresh.
	action $"Updating /etc/fstab" /usr/sbin/updfstab --skipprobe

	# We don't want to run this on random runlevel changes.
	touch /var/lock/subsys/kudzu
	# However, if they did configure X and want runlevel 5, let's
	# switch to it...
	if [ -f /var/run/Xconfig ]; then
	    grep -q "^id:5:initdefault:" /etc/inittab && telinit 5
	    rm -f /var/run/Xconfig
	fi
	# If X configuration failed, give them a sane default.
	if [ -f /var/run/Xconfig-failed ]; then
	    telinit 3
	    rm -f /var/run/Xconfig-failed
	fi
	;;
 status)
 	base=kudzu
 	if [ -f /var/lock/subsys/kudzu ]; then
	  echo $"${base} has run"
	  exit 0
	fi
	echo $"${base} is stopped"
	exit 3
	;;
 stop)
 	rm -f /var/lock/subsys/kudzu
 	;;
 *)
 	echo $"Usage: $0 {start|stop}"
	exit 1
	;;
esac

exit $RETVAL