#! /bin/sh
#
#	Based on skeleton file by Miquel van Smoorenburg <miquels@cistron.nl>.
#	Modified for Debian by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#	Modified for gpsd by Tilman Koschnick <til@subnetz.org>.
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/gpsd
NAME=gpsd
DESC="GPS daemon"
USER=gpsd
PIDFILE="/var/run/gpsd.pid"
SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)

test -x $DAEMON || exit 0

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

set -e

case "$1" in
  start)
	if [ "x$START_DAEMON" = "xtrue" ] ; then
		echo -n "Starting $DESC: $NAME"
		touch $PIDFILE
		chown $USER $PIDFILE
		start-stop-daemon --start --quiet --chuid $USER \
			--exec $DAEMON -- $DAEMON_OPTS -P $PIDFILE \
			&& echo "." \
			|| echo " - previous instance found."
	else
		echo "$NAME is configured not to start automatically."
		echo "  To change this, run 'dpkg-reconfigure gpsd'."
	fi		
	;;
  stop)
	echo -n "Stopping $DESC: $NAME"
	start-stop-daemon --stop --quiet --pidfile $PIDFILE
	echo "."
	;;
  reload|force-reload)
	echo "$DESC: Resetting connection to GPS device."
	start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE
	;;
  restart)
	set +e; $SELF stop; set -e
		$SELF start
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
