#!/bin/sh
#
# lirc daemon
# chkconfig: S 36 80
#

[ -e /dev/lirc ] || ln -s lirc0 /dev/lirc
[ -d /var/run/lirc ] || mkdir -p /var/run/lirc
boxver()
{
	STB_ID=`sed 's/.*STB_ID=//;s/ .*//;/[:]/!d;s/:/_/g' /proc/cmdline`
	[ -z "$STB_ID" ] && STB_ID=`sed 's/.*ethaddr://;s/ .*//;/[:]/!d;s/^24/09/;s/:/_/g' /proc/cmdline`
	BOX_ID=${STB_ID:0:8}
	echo "STB_ID = $STB_ID" 
	echo "BOX_ID = $BOX_ID"
}

case $1 in
	start)
		echo "Starting lircd..."
		boxver	# determine boxtype
		# if a /etc/lircd.conf.$BOX_ID exists, use it...
		[ -z "$LIRCD_OPTS" ] && [ -e /etc/lircd.conf.$BOX_ID ] && LIRCD_OPTS=/etc/lircd.conf.$BOX_ID
		start-stop-daemon --start --quiet --exec /usr/bin/lircd -- -u --device=/dev/lirc $LIRCD_OPTS
		echo "LIRCD_OPTS = $LIRCD_OPTS"
		;;
	stop)
		echo "Stopping lircd..."
		start-stop-daemon --stop --quiet --exec /usr/bin/lircd
		;;
	reload|force-reload)
		start-stop-daemon --stop --quiet --signal 1 --exec /usr/bin/lircd
		;;
	restart)
		echo "Restarting lircd... "
		$0 stop
		sleep 1
		$0 start
		;;
	*)
		echo   "usage:	$0 {start|stop|reload|restart|force-reload}"
		exit 1
		;;
esac

exit 0
