#! /bin/sh
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian 
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#		Modified for aprsg
#		by Tapio Aaltonen <oh2gve@sral.fi>.
#
# Version:	@(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#

### BEGIN INIT INFO
# Provides:          aprsg
# Required-Start:    $syslog $named
# Required-Stop:     $syslog $named
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: APRS Gateway
# Description:       Transfers packets between RF networks and APRS-IS.
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/aprsg
NAME=aprsg
DESC="APRS Gateway"
DAEMON_OPTS="--daemon"

test -x $DAEMON || exit 0

PIDFILE=/var/run/$NAME.pid

# Include aprsg defaults if available
if [ -f /etc/default/aprsg ] ; then
	. /etc/default/aprsg
fi

set -e

# Check if given daemon is running as given pid.
running_pid()
{
    # Check if a given process pid's cmdline matches a given name
    pid=$1
    name=$2
    [ -z "$pid" ] && return 1
    [ ! -d /proc/$pid ] &&  return 1
    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
    # Is this the expected child?
    [ "$cmd" != "$name" ] &&  return 1
    return 0
}

running()
{
# Check if the process is running looking at /proc
# (works for all users)

    # No pidfile, probably no daemon present
    [ ! -f "$PIDFILE" ] && return 1
    # Obtain the pid and check it against the binary name
    pid=`cat $PIDFILE`
    running_pid $pid $DAEMON || return 1
    return 0
}


case "$1" in
  start)
	echo -n "Starting $DESC: "
	# Check if the file is valid.
	if running
	then
		# It's alive! Back out.
		echo "already running."
	else
	        # Start if configured.
        	if [ -n "$START_DAEMON" -a $START_DAEMON="yes" ]
	        then
	        	set +e
			start-stop-daemon --start --quiet --pidfile $PIDFILE \
				--exec $DAEMON -- $DAEMON_OPTS
			set -e
			echo "$NAME."
		else
			echo "not starting, daemon disabled in /etc/default/aprsg"
		fi
	fi
	;;
  stop)
	echo -n "Stopping $DESC: "
	# Exit if daemon is not running.
	if running
	then
		start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
		while test -r $PIDFILE
		do
			echo -n "."
			sleep 1
		done
		echo " $NAME."
	else
		echo "not running."
	fi
	;;
  restart|force-reload)
	echo -n "Restarting $DESC: "
	# Exit if daemon is not running.
	if running
	then
		start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
		echo -n "shutting down"
		while test -r $PIDFILE
		do
		   echo -n "."
		   sleep 1
		done
		echo -n " starting up: "
		set +e
		start-stop-daemon --start --quiet --pidfile $PIDFILE \
			--exec $DAEMON -- $DAEMON_OPTS
		set -e
		echo "$NAME."
	else
		echo "not running."
	fi
	;;
  reload)
	echo -n "Reopening $NAME log files: "
	# Exit if daemon is not running.
	if running
	then
		kill -HUP `cat $PIDFILE`
		echo "done."
	else
		echo "not running."
	fi
	;;
  *)
  	# Default.
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
