#! /bin/bash
# (c) 2008 Johannes Grosse
# This file is essentially Ubuntu's /etc/init.d/915resolution script,
# but the part reading a configuration file has been removed.
# I presume this implies that this file is under the GPL by force,
# which is fine with me, but which I cannot guarantee at the moment.
#
# This script will patch the highest available video mode
# to either a given resolution or if not specified to the
# native resolution of the panel.
#
# Usage:
#     StartTask 915Resolution            # automatic mode
#     StartTask 915Resolution "1440x900"
#
source GoboPath

AUTO=`basename ${0}`
PROG=${goboExecutables}/915resolution
PROGPACKAGE=915Resolution
VBETOOL=${goboExecutables}/vbetool
VBEPACKAGE=VBETool

prog_missing()
{
        echo "$AUTO: Cannot find ${1}."
        echo "$AUTO: Please Compile ${2}."
        exit 1
}

wrong_chipset()
{
        echo "$AUTO: Wrong chipset detected. ${PROG} only works with Intel 800/900 series graphic chipsets."
        exit 1
}

# ------------------------------------------------------
# The following is taken almost literally from Ubuntu's 
# /etc/init.d/915resolution script
# 

patch_biosmode()
{
    panelsize="$1"

# 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 "$AUTO: Panel-size is already available in BIOS, exiting."
      exit 0
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
}

# ------------------------------------------------------------------------
# main part processing Start and Stop
#

case "$1" in
    [Ss]top)
    # actions to stop 915Resolution go here
    # NOTHING to be done here
    ;;
    [Ss]tart)
    # actions to start 915Resolution

    # test if we find 915resolution program and if it can be used at all
    test -x $PROG || prog_missing "$PROG" "$PROGPACKAGE"
    $PROG -l >/dev/null 2>&1 || wrong_chipset

    # check if we use manual resolution or automatic
    if [ "$2" ] 
    then # manual
        patch_biosmode "$2"
    else # automatic, that is use panel resolution
        test -x $VBETOOL || prog_missing "$VBETOOL" "$VBEPACKAGE"
        PANELSIZE=`$VBETOOL vbefp panelsize`
        echo "$AUTO: Using physical panel size (${PANELSIZE}) for patching the graphics BIOS"
        patch_biosmode ${PANELSIZE}
    fi
    ;;
esac
