#!/bin/sh
# Startup script for hardware auto configuration
#
# chkconfig: 35 43 06
# description: Auto setup hardware
WITHOUT_RC_COMPAT=1

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

[ -f /usr/bin/x11createconfig ] || exit
SETGL=/usr/bin/x11setupdrv

LOCKFILE=/var/lock/subsys/x11createconfig
RETVAL=0

start() {
	RETVAL=1
	if [ ! -f /etc/X11/xorg.conf ];then
	    local STRING="Setting up Xorg ..."
	    echo -n "$STRING "
	    /usr/bin/x11createconfig >/etc/X11/xorg.conf.auto
	    local rc=$?
	    [ $rc -eq 0 ] && success "$STRING" || failure "$STRING"
	    echo
	    ln -s xorg.conf.auto /etc/X11/xorg.conf
	    touch $LOCKFILE
	fi
        [ -x "$SETGL" ] && $SETGL ||:
	RETVAL=0
	return $RETVAL
}

stop() {
	if [ -h /etc/X11/xorg.conf ] && [ "`readlink /etc/X11/xorg.conf`" == "xorg.conf.auto" ];then
	    action "Remove Xorg configuration" rm -f /etc/X11/xorg.conf /etc/X11/xorg.conf.auto
	fi
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status hardware
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
