#!/bin/sh
# INIT script to check whether we're on batteries, and so start with laptop 
# mode etc enabled.

### BEGIN INIT INFO
# Provides:          acpi-fakekey
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      
# Short-Description: Start acpi_fakekey daemon
### END INIT INFO

test -f /lib/lsb/init-functions || exit 1
. /lib/lsb/init-functions

case "$1" in
  start)
    log_action_begin_msg "Starting acpi_fakekey daemon"
    if [ ! -d /sys/devices/virtual/misc/uinput ]; then
	 if ! modprobe -q uinput; then
		if ! find /lib/modules/`uname -r` -name "uinput\.*" 2>/dev/null|grep -q uinput; then
			log_action_end_msg 1 "No suitable uinput module for running kernel found"
		else
			log_action_end_msg 1 "Modprobe uinput failed. Please use 'dmesg' to find out why"
		fi
	fi
	# give udev time to create the devices
	sleep 1
    fi

    if [ -d /sys/devices/virtual/misc/uinput ]; then
	if start-stop-daemon --start --quiet --oknodo --exec /usr/sbin/acpi_fakekeyd
	then
		log_action_end_msg 0
	else
		log_action_end_msg 1 "Could not start /usr/sbin/acpi_fakekeyd"
	fi
    else
	log_action_end_msg 0
    fi
    ;;
  stop)
    log_action_begin_msg "Stopping acpi_fakekey daemon"
    if start-stop-daemon --stop --quiet --exec /usr/sbin/acpi_fakekeyd
    then
    	if [ -d /sys/devices/virtual/misc/uinput ]; then
		sleep 1 # give acpi_fakekeyd a chance to disappear
		if ! rmmod uinput 2>/dev/null; then
                        log_action_end_msg 1 "Cannot unload module uinput"
		else
			log_action_end_msg 0
                fi
	fi
    else
	log_action_end_msg 1 "Could not stop /usr/sbin/acpi_fakekeyd"
    fi
    ;;
  restart|force-reload)
    log_action_begin_msg "Restarting acpi_fakekey daemon"
    start-stop-daemon --stop --quiet --exec /usr/sbin/acpi_fakekeyd
    if start-stop-daemon --start --quiet --exec /usr/sbin/acpi_fakekeyd
    then
	log_action_end_msg 0
    else
	log_action_end_msg 1 "Could not restart /usr/sbin/acpi_fakekeyd"
    fi
    ;;
  *)
  ;;
esac
        

