#!/bin/sh

# Daemon handling script for Tangent Hub
# SVN: $Revision: 20 $

# chkconfig:        2345 85 15
# description:      TangentHub is a daemon to communicate with Tangent panels
# processname:      TangentHub

### BEGIN INIT INFO
# Provides:         TangentHub
# Required-Start:
# Required-Stop:
# Default-Start:    2 3 4 5
# Description:      TangentHub is a daemon to communicate with Tangent panels
### END INIT INFO

# function helpers allowing for multiple distro types
if [ -f /etc/redhat-release ]; then
    . /etc/init.d/functions
elif [ -f /etc/SuSE-release ]; then
    . /etc/rc.status
elif [ -f /etc/lsb-release ]; then
    killproc() {
        start-stop-daemon --stop --exec $@
    }
elif [ -f /etc/debian_version ]; then
    killproc() {
        start-stop-daemon --stop --exec $@
    }
elif [ -f /etc/gentoo-release ]; then
    . /sbin/functions.sh
    killproc() {
        start-stop-daemon --stop --exec $@
    }
elif [ -f /etc/arch-release ]; then
    . /etc/rc.d/functions
    killproc() {
        killall $@
        rm_daemon `basename $@`
    }
elif [ -f /etc/slackware-version ]; then
    killproc() {
        killall $1
    }
elif [ -f /etc/lfs-release ]; then
    . /etc/rc.d/init.d/functions
fi

# default return value
RETVAL=0

# default niceness
NICE="nice -0"

# start service
start() {
	echo -n "starting tangenthub service: "
    /sbin/modprobe uinput > /dev/null 2>&1
	$NICE /opt/Tangent/bin/TangentHub > /dev/null 2>&1 &
	RETVAL=$?
    sleep 1
    echo "[  OK  ]"
	return $RETVAL
}

# stop service
stop() {
	echo -n "stopping tangenthub service: "
	killproc TangentHub
	RETVAL=$?
    echo
	return $RETVAL
}

case "$1" in
    start)
	    start
	    ;;
    stop)
	    stop
	    ;;
    restart|reload)
	    stop
	    start
	    RETVAL=$?
	    ;;
    status)
        # implement a simplistic but multi-distro friendly status check
        proccount=`ps -ef | grep "/opt/Tangent/bin/TangentHub" | grep -vc "grep"`
        if [ $proccount -ne 0 ]; then
            # service running
            echo "tangenthub is running"
            RETVAL=0
        else
            # service not running 
            echo "tangenthub is stopped"
            RETVAL=3
        fi
	    ;;
    *)
	echo "usage: $0 {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL
