#!/bin/sh
# LSB compatible service control script; see http://www.linuxbase.org/spec/
#
# Note: This template uses functions rc_XXX defined in /etc/rc.status on
# UnitedLinux/SUSE/Novell based Linux distributions. If you want to base your
# script on this template and ensure that it works on non UL based LSB
# compliant Linux distributions, you either have to provide the rc.status
# functions from UL or change the script to work without them.
# See skeleton.compat for a template that works with other distros as well.
#
### BEGIN INIT INFO
# Provides:          hotkey-setup
# Required-Start:    $syslog $remote_fs kbd
# Required-Stop:     $syslog $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: hotkey-setup setup mapping for laptop keys
# Description:       This init script sets up vendor specific laptop
#                    keys mapping to standard Microsoft multimedia
#                    keyboard keycodes, which can be used by user space
#                    applications to trigger actions.
### END INIT INFO

. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0       - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

SAVED_STATE=/var/run/hotkey-setup
THINKPAD_LOCKFILE=$SAVED_STATE.thinkpad-keys

# This is here because it needs to be executed both if we have a
# Lenovo that also IDs as a ThinkPad, or if we have a real IBM one.
do_thinkpad () {
    . /usr/share/hotkey-setup/ibm.hk
    if [ -x /usr/sbin/thinkpad-keys ]; then
        if [ ! -c /dev/input/uinput ]; then
            modprobe uinput
        fi
        if [ ! -c /dev/nvram ]; then
            modprobe nvram
        fi
        /usr/sbin/thinkpad-keys && touch $THINKPAD_LOCKFILE
    fi
}

run_hotkey_setup()
{
manufacturer=`/usr/sbin/dmidecode --string system-manufacturer`
name=`/usr/sbin/dmidecode --string system-product-name`
version=`/usr/sbin/dmidecode --string system-version`

/usr/sbin/dumpkeycodes >$SAVED_STATE

if [ $? -gt 0 ]; then
rm -f $SAVED_STATE
fi

. /usr/share/hotkey-setup/key-constants

case "$manufacturer" in
    Acer*)
    . /usr/share/hotkey-setup/acer.hk
    case "$name" in
        Aspire\ 16*)
        . /usr/share/hotkey-setup/acer-aspire-1600.hk
        ;;
    esac
    ;;

    ASUS*)
    . /usr/share/hotkey-setup/asus.hk
    ;;

    Compaq*)
    case "$name" in
        Armada*E500*)
        . /usr/share/hotkey-setup/compaq.hk
        ;;
    esac
    ;;

    Dell*)
    . /usr/share/hotkey-setup/dell.hk
    ;;

    Hewlett-Packard*)
    # Load this _first_, so that it can be overridden
    . /usr/share/hotkey-setup/hp.hk
    case "$name" in
        *Tablet*)
        . /usr/share/hotkey-setup/hp-tablet.hk
        ;;
    esac
    ;;

    IBM*)
    do_thinkpad
    ;;

    LENOVO*)
    case "$version" in
        *Think[pP]ad*)
        do_thinkpad
        ;;
    esac
    ;;
    
    MEDION*)
    case "$name" in
        *FID2060*)
        . /usr/share/hotkey-setup/medion-md6200.hk
        ;;
    esac
    ;;

    Samsung*)
    . /usr/share/hotkey-setup/samsung.hk
    ;;

    Sony*)
    modprobe sonypi; # Needed to get hotkey events
    ;;

    *)
    . /usr/share/hotkey-setup/default.hk
esac

. /usr/share/hotkey-setup/generic.hk

}

case "$1" in
    start)
        echo -n "Starting hotkey-setup "
        ## Start daemon with startproc(8). If this fails
        ## the return value is set appropriately by startproc.
        run_hotkey_setup

        # Remember status and be verbose
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down hotkey-setup "

        if [ -f $THINKPAD_LOCKFILE ]; then
                killproc -TERM /usr/sbin/thinkpad-keys && rm -f $THINKPAD_LOCKFILE
        fi

        if [ -f $SAVED_STATE ]; then
                setkeycodes $(cat $SAVED_STATE)
                rm -f $SAVED_STATE
        fi

        # Remember status and be verbose
        rc_status -v
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop || true
        $0 start

        # Remember status and be quiet
        rc_status
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac
rc_exit
