#!/bin/bash
#
# motion
# Start the motion detection
#

NAME=motion
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/motion
PIDFILE=/run/motion/motion.pid

trap "" 1
export LANG=C
export PATH

test -f $DAEMON || exit 0

case "$1" in
    start)
        LOGFILE="/var/log/motion/$(date +'%Y-%m-%d_%H-%M-%S')_motion.log"

        echo -n "Starting motion detection: "

        # If /var/log/motion/motion.log is a file, remove it
        if [ -f "/var/log/motion/motion.log" ]; then
            rm -f /var/log/motion/motion.log
        fi

        # Create log file with date and time
        if ! touch "$LOGFILE" > /dev/null 2>&1; then
            echo "could not create log file"
            exit 1
        fi

        chown www-data:www-data "$LOGFILE"
        
        if ! ln -sf "$LOGFILE" /var/log/motion/motion.log; then
            echo "could not create symlink to log file"
            exit 1
        fi

        # Start motion detection
        start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON --chuid www-data --umask 002

        if [ $? -ne 0 ]; then
            echo "failed"
            exit 1
        fi

        echo "done"
    ;;

  stop)
    echo -n "Stopping motion detection: "
    # start-stop-daemon --stop --pidfile $PIDFILE --oknodo --exec $DAEMON --retry 30
    if [ -f "$PIDFILE" ]; then
        PID=$(cat "$PIDFILE")

        # If the PID is empty, the process is not running
        if [ -z "$PID" ]; then
            exit 0
        fi

        # Check if a process with this PID is still running
        if /usr/bin/ps -p "$PID" > /dev/null; then
            kill "$PID"
        fi

        # Wait for the process to stop (timeout 10s)
        i="0"
        while true; do
            # Check if the process is still running, quit if not
            if ! ps aux | grep "/usr/bin/motion" | grep -q -v "grep"; then
                break
            fi

            # If timeout is reached, force kill the process
            if [ "$i" -gt "10" ]; then
                echo "forcing the process to stop (kill -9)"

                # Process name is "mo00"
                killall -9 mo00

                # Check if the process is still running
                if ps aux | grep "/usr/bin/motion" | grep -q -v "grep"; then
                    echo "could not stop motion detection (timeout 10s)"
                    exit 1
                fi
            fi

            (( i++ ))
            sleep 1
        done

        # Wait 8sec to make sure motion is truly stopped even if there is no more process
        sleep 8

        echo "done"
    fi
    ;;

  status)
    echo -n "Status motion detection: "

    # Check if a pid file exists
    if [ -f $PIDFILE ]; then
        echo "process is running with PID $(pidof $NAME)"
        exit 0
    fi

    # Also check if a motion process is running
    if ps aux | grep "/usr/bin/motion" | grep -q -v "grep"; then
        echo "process is running but no pid file found"
        exit 0
    fi

    echo "stopped"
    exit 1
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|status}"
    exit 1
    ;;
esac
