#!/bin/sh
#
# razerd       This shell script takes care of starting and stopping razerd.
#
# chkconfig: 2345 90 10
# description: razerd needed for razer mouses config utility
# processname: razerd
#
### BEGIN INIT INFO
# Provides: razerd
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Short-Description: razerd daemon config utility
# Description: razerd needed for razer mouses config utility
### END INIT INFO



RAZERD_BIN=/usr/sbin/razerd

[ -x $RAZERD_BIN ] || exit 0

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

RETVAL=0
prog=razerd
pidfile=/var/run/razerd.pid

start()
{
	gprintf "Starting %s: " "$prog"
	if [ -e "$pidfile" ] && [ -e /proc/`cat "$pidfile"` ]; then
		gprintf "already running.";
		success "%s is already running." "$prog";
		echo
		return 0
	fi
	daemon $RAZERD_BIN -B
	RETVAL=$?
	echo
	return $RETVAL
}

stop()
{
	gprintf "Shutting down %s: " "$prog"
	killproc $RAZERD_BIN
	RETVAL=$?
	echo
	rm -f $pidfile
	return $RETVAL
}

reload()
{
	gprintf "Reloading %s configuration: " "$prog"
	killproc $RAZERD_BIN -HUP
	RETVAL=$?
	echo
	return $RETVAL
}

force_reload()
{
	pid=`pidofproc razerd`
	if [ -n "$pid" ]; then
		reload
	else
		start
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		stop
		start
		;;
	try-restart)
		if [ -f $pidfile ]; then
			stop
			start
		fi
		;;
	force-reload)
		force_reload
		;;
	status)
		status $prog
		RETVAL=$?
		;;
	*)
		gprintf "Usage: %s {start|stop|reload|force-reload|restart|try-restart|status}\n" "$0"
		RETVAL=3
esac

exit $RETVAL
