#!/sbin/openrc-run
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# Init script for g15daemon

pidfile="/var/run/g15daemon.pid"
command="/usr/sbin/g15daemon"
start_stop_daemon_args="--wait 30"
command_args=""

depend() {
	after hotplug
	after usb
	after modules
}

start_pre() {
	# Does the input device already exist?
	if [ -e "/proc/modules" ] ; then
		if [ ! -e "/dev/input/uinput" ] && [ ! -e "/dev/uinput" ] ; then
			# We can load modules, but uinput device does not exist
			einfo "Loading uinput module"
			/sbin/modprobe uinput > /dev/null 2> /dev/null \
				|| return 1
		fi
	fi

	if [ "${CLIENT_SWITCH_L1}" = "yes" ] ; then
		command_args="--switch"
	fi
}

stop() {
	ebegin "Stopping g15daemon"
	local KILLOPT="-k"
	if [ $BACKLIGHT_OFF = "yes" ]; then
		KILLOPT="-K"
	fi
	while [ -f "${pidfile}" ] && kill -0 $(cat ${pidfile}) ; do
		${command} ${KILLOPT}
		sleep 1
	done
	eend $?
}
