#! /bin/sh
#
# pommed	Apple laptops hotkeys event handler
#
# chkconfig: 2345 40 60
# description: pommed handles the hotkeys found on the Apple MacBook Pro
#              and MacBook laptops and adjusts the LCD backlight, sound
#              volume, keyboard backlight or ejects the CD-ROM drive
#              accordingly.
# processname: pommed
# config: /etc/pommed.conf
# pidfile: /var/run/pommed.pid

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

DESC="Apple laptops hotkeys events handler"

test -x /usr/sbin/pommed || exit 0


check_macbook() {
	# Intel MacBooks
	if [ -f /sys/class/dmi/id/product_name ] ; then
	    if (grep -q "MacBook" /sys/class/dmi/id/product_name) ; then
		return 0
	    fi
	fi

	# PowerBooks/iBooks
	if [ -f /proc/device-tree/model ] ; then
	    if (grep -q "PowerBook" /proc/device-tree/model) ; then
		return 0
	    fi
	fi

	# other models
	return 1
}


case "$1" in
  start)
	echo -n "Starting $DESC: "
	check_macbook
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
	    daemon pommed
	    touch /var/lock/subsys/pommed
	else
	    passed "pommed effective only on MacBooks/PowerBooks/iBooks"
	fi
	echo
	;;
  stop)
	echo -n "Stopping $DESC: "
	if [ -f /var/run/pommed.pid ]; then
	    killproc pommed
	    rm -f /var/lock/subsys/pommed
	else
	    passed
	fi
	echo
	;;
  status)
	status pommed
	;;
  restart)
	$0 stop
	$0 start
	;;
  condrestart)
	[ -f /var/lock/subsys/pommed ] && $0 restart || :
	;;
  *)
	echo "Usage: pommed {start|stop|status|restart|condrestart}" >&2
	exit 1
	;;
esac

exit $?
