#!/bin/bash
#
# go2rtc
# Start the go2rtc stream server
#

NAME=go2rtc
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/go2rtc
DAEMON_OPTS="-c /var/lib/motionui/go2rtc/go2rtc.yml"
PIDFILE=/run/go2rtc/go2rtc.pid

trap "" 1
export LANG=C
export PATH

test -f $DAEMON || exit 0

#
# Start the go2rtc stream server
#
function start()
{
    # If go2rtc is running
    if ps aux | grep "$DAEMON" | grep -q -v "grep"; then
        echo "go2rtc is already running"
        exit 0
    fi

    # Clean any existing PID file
    rm -f "$PIDFILE"

    LOGFILE="/var/lib/motionui/go2rtc/logs/$(date +'%Y-%m-%d')_go2rtc.log"

    # Create PID directory
    mkdir -p /run/go2rtc
    chmod 755 /run/go2rtc
    chown www-data:www-data /run/go2rtc

    # Create log directory
    mkdir -p /var/lib/motionui/go2rtc/logs -m 750
    chown www-data:www-data /var/lib/motionui/go2rtc/logs

    # If /var/lib/motionui/go2rtc/logs/go2rtc.log is a file, remove it
    if [ -f "/var/lib/motionui/go2rtc/logs/go2rtc.log" ]; then
        rm -f /var/lib/motionui/go2rtc/logs/go2rtc.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

    # Set permissions on log file
    if ! chown www-data:www-data "$LOGFILE"; then
        echo "Could not properly set permissions on log file $LOGFILE"
        exit 1
    fi

    # Create symlink to log file
    if ! ln -sf "$LOGFILE" /var/lib/motionui/go2rtc/logs/go2rtc.log; then
        echo "Could not create symlink to log file"
        exit 1
    fi

    # Start go2rtc detection
    # start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --exec $DAEMON -c /var/lib/motionui/go2rtc/config.yml --chuid www-data --umask 002 -- $DAEMON_OPTS
    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile \
    --chuid www-data --umask 002 \
    --startas /bin/bash -- -c "exec $DAEMON $DAEMON_OPTS > /var/lib/motionui/go2rtc/logs/go2rtc.log 2>&1"

    # If no PID file was created, the process did not start properly
    if [ ! -f "$PIDFILE" ]; then
        echo "Error starting go2rtc: PID file is missing"
        exit 1
    fi

    chmod 600 "$PIDFILE"
    chown www-data:www-data /run/go2rtc/go2rtc.pid

    PID=$(cat "$PIDFILE")

    # If the PID is empty, the process is not running
    if [ -z "$PID" ]; then
        echo "Error starting go2rtc: PID file is empty"
        exit 1
    fi

    echo "go2rtc started, PID: $PID"
}

#
# Stop the go2rtc stream server
#
function stop()
{
    i="0"

    # If go2rtc is running
    if ps aux | grep "$DAEMON" | grep -q -v "grep"; then
        echo "Stopping go2rtc"

        # If a PID file exists, get the PID and kill the process
        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 ps -p "$PID" > /dev/null; then
                # echo "Force stopping go2rtc (kill)"
                kill "$PID"
            fi
        fi

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

            if [ "$i" -gt "10" ]; then
                echo "Could not stop go2rtc (timeout 10s)"
                exit 1
            fi

            (( i++ ))
            sleep 1
        done
    fi

    # Remove PID file
    rm -f "$PIDFILE"
}

#
# Get go2rtc service status
#
function status()
{
    # If go2rtc is not running, exit
    if ! ps aux | grep "$DAEMON" | grep -q -v "grep"; then
        echo "go2rtc is not running"
        exit 1
    fi

    # If the process is running with no PID file, exit with an error
    if [ ! -f "$PIDFILE" ]; then
        echo "go2rtc is running but PID file is missing"
        exit 1
    fi

    # Get PID from PID file
    PID=$(cat "$PIDFILE")

    # If the PID is empty, exit with an error
    if [ -z "$PID" ]; then
        echo "go2rtc is running but PID file is empty"
        exit 1
    fi

    echo "go2rtc is running, PID: $PID"
    exit 0
}

case "$1" in
    start)
        start
    ;;

    stop)
        stop
    ;;

    restart)
        stop && start
    ;;
    status)
        status



        echo "Status go2rtc"
        if [ -f $PIDFILE ]; then
            echo -n "Running process for $NAME : "
            pidof $NAME  
        else
            echo "Stopped"
        fi
    ;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}"
        exit 1
    ;;
esac
