#! /bin/sh
#
# Author: Michael Conrad <do5mc@friggleware.de>
#

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/bin:/usr/sbin:/usr/bin

DESC="APRS4R"
NAME=aprs4r
DAEMON=/usr/bin/$NAME
DAEMON_ARGS=

PID_FILE=/var/run/$NAME.pid
DEFAULT_FILE=/etc/default/$NAME


# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r "$DEFAULT_FILE" ] && . $DEFAULT_FILE


case "$1" in

    start)
	if [ "$START_DAEMON" != "true" ]; then
	    echo "$DESC disabled (see $DEFAULT_FILE)"
	    exit 1
	fi
	
	PROFILE_FILE=/etc/aprs4r/$PROFILE
	
	if [ ! -f "$PROFILE_FILE" ]; then
	    echo "configuration file $PROFILE_FILE not found"
	    exit 1
	fi
	
	$DAEMON $PROFILE_FILE
	exit 0 
	;;
    
    stop)
	if [ -s "$PID_FILE" ]; then 
	    PID=`cat $PID_FILE`
	    
	    if kill $PID; then
		echo "$NAME was stopped (id $PID)"
		exit 0 
	    else 
		echo "$PID_FILE exists, but $NAME is not running"
		exit 1
	    fi
	else
	    echo "$NAME is not running"
	    exit 3
	fi

	;;

    restart)
	$0 stop
	$0 start
	exit $?
	;;

    status)
	if [ -s "$PID_FILE" ]; then
	    PID=`cat $PID_FILE`

	    if kill -0 $PID; then
		echo "$NAME is running (id $PID)"
		exit 0 
	    else 
		echo "$PID_FILE exists, but $NAME is not running"
		exit 1
	    fi
	else
	    echo "$NAME is not running"
	    exit 3
	fi

	;;

    *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 3
	;;
    
esac
