#! /bin/sh
# 915resolution	Set the resolution for special chipset.
#
# chkconfig: 345 90 10
# description:	Set the resolution for special chipset \
#		The right resolution mode will be set and given \
#		to the X Server.
# processname: 915resolution

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

PROG=/usr/sbin/915resolution
VBETOOL=/usr/sbin/vbetool
RETVAL=0

test -x $PROG || exit 0

# Include 915resolution defaults if available
SourceIfNotEmpty /etc/sysconfig/915resolution

wrong_chipset()
{
	echo "Wrong chipset detected. 915resolution only works with Intel 800/900 series graphic chipsets."
	RETVAL=1
	return $RETVAL
}

$PROG -l &>/dev/null || wrong_chipset

if [ "$MODE" = auto -a \! -x "$VBETOOL" ] || [ "$MODE" != auto -a \( -z "$MODE" -o -z "$XRESO" -o -z "$YRESO" \) ] ; then
   echo "*** Your 915resolution hasn't been configured! ***"
   echo "Please read /usr/share/doc/915resolution-0.5.2/README.ALT and define"
   echo "MODE, XRESO, and YRESO."
   exit 0
fi

auto_select_modes()
{
    if [ ! -x "$VBETOOL" ] || ! panelsize=`$VBETOOL vbefp panelsize` ; then
	echo "*** Your 915resolution was not automatically configured! ***"
	echo "Please read /usr/share/doc/915resolution-0.5.2/README.ALT then define"
        echo "MODE, XRESO, and YRESO manually in /etc/sysconfig/915resolution ."
	echo "For now a default will be set, which might be inappropiate."
	XRESO=1024
	YRESO=768
    fi

    # If the native panel-size is already in the BIOS mode list, we
    # don't have to do anything, yippee!
    bios_list=`$PROG -l`
    if echo "$bios_list" | grep -q "^Mode .* $panelsize" ; then
	#echo 'Correct panel-size is already listed, skipping'
	return
    fi

    # For want of a better approach to selecting modelines for stealing;...
    # this expression grabs us the highest *numbered* mode in each
    # bit-depth that the BIOS already lists.
    #
    # An argument for using the highest mode-number, rather than the
    # highest resolution is that if we've already reprogrammed the
    # mode to be a lower resolution it will no longer be the highest.
    # If this has changed over a suspend cycle, that consequences
    # might not be so good.  -Paul Sladen
    target_modes=`echo "$bios_list" | awk '/^Mode [^T]/{sub(",","",$4); print $5,$4,$2}' | tac | sort -n -u | cut -d' ' -f3`

    for m in $target_modes ; do
	# The 'tr' converts '1024x768' -> '1024' '768'
	# and the bitdepth is missed off because we have one of each depth
	$PROG $m $(echo $panelsize | tr x ' ')
    done
}

# See how we were called.
case "$1" in
  start|restart|condreload)
	msg_starting 915resolution
	if [ "$MODE" = "auto" ] ; then
	    auto_select_modes
	else
	    $PROG $MODE $XRESO $YRESO $BIT
	fi	
	;;
  stop)
	;;
  *)
	msg_usage "${0##*/} {start|stop|restart|condreload}"
	RETVAL=1
esac

exit $RETVAL

