#!/bin/bash
#
# RedHat-like  init script
# copy to /etc/init.d and run chkconfig --add sonypi
#
# chkconfig: 2345 86 02
# description: Sonypi runs controlling program for /dev/sonypi device
#              related files (by default):
#              - /usr/local/sbin/sonykeyd daemon that listen to  /dev/sonypi and executes sonykey.sh on each event
#              - /usr/local/sbin/sonykey.sh bash script that configures actions for events

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

RETVAL=0
prog="sonykeyd"
exe="/usr/local/sbin/sonykeyd"

# Some functions to make the below more readable

start()
{
	# Create keys if necessary

	echo -n $"Starting $prog:"
  rm -f /var/local/sound/state/*
	initlog -c $exe  && success || failure 
	RETVAL=$?
	[ "$RETVAL" = 0 ] && touch /var/lock/subsys/$prog
	echo
}

stop()
{
	echo -n $"Stopping $prog:"
	killproc $exe -TERM
	RETVAL=$?
	[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/$prog
	echo
}

reload()
{
  true;
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	reload)
		reload
		;;
	condrestart)
		if [ -f /var/lock/subsys/$prog ] ; then
			stop
			# avoid race
			sleep 3
			start
		fi
		;;
	status)
		status $exe
		RETVAL=$?
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
		RETVAL=1
esac
exit $RETVAL
