#! /bin/sh -e
#
# /etc/init.d/motion: Start the motion detection
#
### BEGIN INIT INFO
# Provides:	  motion
# Required-Start: $local_fs $syslog
# Required-Stop:
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Motion detection
# Description: loads motion and assigns privileges
### END INIT INFO

# Ported to new debian way using sh and /lib/lsb/init-functions
# by Angel Carpintero <ack@telefonica.net>
# Modify by Juan Angulo Moreno <juan@apuntale.com>

NAME=motion
PATH_BIN=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/bin/motion
PIDFILE=/var/run/$NAME.pid

ENV="env -i LANG=C PATH=$PATH_BIN"

. /lib/lsb/init-functions

test -x $DAEMON || exit 0

case "$1" in
  start)
    echo "Starting motion detection : $NAME"
    start-stop-daemon --start --exec $DAEMON -b --chuid motion || true
    ;;

  stop)
    echo "Stopping motion detection : $NAME"
    start-stop-daemon --stop --oknodo --exec $DAEMON --retry 30
    ;;

  reload-config)
    echo "Reloading $NAME configuration"
    start-stop-daemon --stop --signal HUP --exec $DAEMON
    ;;

  restart-motion)
    echo "Restarting $NAME"
    $0 stop
    $0 start
    ;;

  restart|force-reload)
    $0 restart-motion
    exit $?
    ;;

  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|reload-config|restart|force-reload}"
    exit 1
    ;;
esac

if [ $? -eq 0 ]; then
	echo .
	exit 0
else
	echo failed
	exit 0
fi
