#!/sbin/openrc-run
#
# OpenRC init script for gpm.
# Based on Debian's /etc/init.d/gpm (CFG parsing by Friedhelm Mehnert).
#

description="General Purpose Mouse interface"

command="/usr/sbin/gpm"
pidfile="/run/gpm.pid"
CFG=/etc/gpm.conf
cmdln=

extra_commands="strace"

depend() {
	need localmount
	use logger
	after syslog
}

_build_cmdln() {
	cmdln=
	[ -f "$CFG" ] || return 0
	. "$CFG"
	if [ -n "$device" ]; then cmdln="$cmdln -m $device"; fi
	if [ -n "$type" ]; then cmdln="$cmdln -t $type"; fi
	if [ -n "$responsiveness" ]; then cmdln="$cmdln -r $responsiveness"; fi
	if [ -n "$sample_rate" ]; then cmdln="$cmdln -s $sample_rate"; fi
	if [ -n "$repeat_type" ] && [ "$repeat_type" != "none" ]; then
		cmdln="$cmdln -R$repeat_type"
	fi
	if [ -n "$append" ]; then cmdln="$cmdln $append"; fi
	if [ -n "$device2" ] && [ -n "$type2" ]; then
		cmdln="$cmdln -M -m $device2 -t $type2"
	fi
}

start() {
	[ -x "$command" ] || return 0
	_build_cmdln

	for _try in 1 2 3; do
		if :< ${device-/dev/mouse}; then break; fi
		sleep 1
	done

	ebegin "Starting gpm"
	start-stop-daemon -S -q --oknodo --nicelevel 0 \
		-p "$pidfile" -x "$command" -- $cmdln
	eend $?
}

stop() {
	[ -x "$command" ] || return 0
	ebegin "Stopping gpm"
	start-stop-daemon -K -q --oknodo -p "$pidfile" -x "$command"
	eend $?
}

strace() {
	[ -x "$command" ] || return 1
	_build_cmdln
	ebegin "Running gpm under strace"
	eval strace -T -o /root/gpm.strace "$command" -V -D -e $cmdln \
		>/root/gpm.out 2>&1
	eend $?
}
