#!/bin/sh
# x11_autosetup		Automatic x11 setup using x11_autosetup and x11setupdrv
#
# chkconfig: 35 43 06
# description: Automatic x11 setup using x11_autosetup and x11setupdrv

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

start()
{
	RETVAL=1
	if [ ! -f /etc/X11/xorg.conf -o -L /etc/X11/xorg.conf ]; then
		action 'Creating Xorg configuration:' x11_autosetup
		[ $? = 0 ] || return
		sed -i 's/DefaultDepth \+-1/DefaultDepth 16/' /etc/X11/xorg.conf.auto
		case "$(sed -n 's/.*\<vga=\([^ ]\+\).*/\1/p' /proc/cmdline)" in
			'0x317') sed -i 's/Modes.*/Modes "1024x768"/g' /etc/X11/xorg.conf.auto ;;
			'0x163') sed -i 's/Modes.*/Modes "1152x864"/g' /etc/X11/xorg.conf.auto ;;
			'0x31A') sed -i 's/Modes.*/Modes "1280x1024"/g' /etc/X11/xorg.conf.auto ;;
			'0x31E') sed -i 's/Modes.*/Modes "1600x1200"/g' /etc/X11/xorg.conf.auto ;;
		esac
		ln -s xorg.conf.auto /etc/X11/xorg.conf
		RETVAL=0
        else
		action 'Updating Xorg configuration:' x11setupdrv
		[ $? = 0 ] && RETVAL=0
        fi
	return $RETVAL
}

stop()
{
	if [ "$(readlink /etc/X11/xorg.conf)" = xorg.conf.auto ]; then
		action 'Removing 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
		;;
	condreload|condrestart)
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart}"
		RETVAL=1
esac

exit $RETVAL
