#!/bin/bash
### BEGIN INIT INFO
# Provides:          airpipe
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts the airpipe service
# Description:       AirPipe service daemon
### END INIT INFO

USER="UserPlaceholder"
AIRPIPE_API_KEY="ApiKeyPlaceholder"
AIRPIPE_CONFIG_DIR="ConfigDirPlaceholder"
AIRPIPE_DATA_DIR="DataDirPlaceholder"
AIRPIPE_LOG_DIR="LogDirPlaceholder"

case "$1" in
    start)
        echo "Starting AirPipe..."
        su $USER -c "AIRPIPE_API_KEY='$AIRPIPE_API_KEY' AIRPIPE_CONFIG_DIR='$AIRPIPE_CONFIG_DIR' AIRPIPE_DATA_DIR='$AIRPIPE_DATA_DIR' AIRPIPE_LOG_DIR='$AIRPIPE_LOG_DIR' /usr/local/bin/airpipe server >> '$AIRPIPE_LOG_DIR/airpipe-service.log' 2>&1 & echo \$! > /var/run/airpipe.pid"
        #su - $USER -c "AIRPIPE_API_KEY=$AIRPIPE_API_KEY AIRPIPE_CONFIG_DIR=$AIRPIPE_CONFIG_DIR AIRPIPE_DATA_DIR=$AIRPIPE_DATA_DIR AIRPIPE_LOG_DIR=$AIRPIPE_LOG_DIR /usr/local/bin/airpipe server >> $AIRPIPE_LOG_DIR/airpipe-service.log 2>&1 &"
        #echo $! > /var/run/airpipe.pid
        ;;
    stop)
        echo "Stopping AirPipe..."
        if [ -f /var/run/airpipe.pid ]; then
            PID=$(cat /var/run/airpipe.pid)
            if [ -n "$PID" ] && kill -0 $PID 2> /dev/null; then
                # Verify it's the correct process
                PROC_NAME=$(ps -p $PID -o comm=)
                if [[ "$PROC_NAME" == "airpipe" ]]; then
                    kill $PID
                    echo "AirPipe stopped successfully."
                else
                    echo "Process with PID $PID is not AirPipe, ignoring."
                fi
            else
                echo "No running process matches the PID; may have already stopped or crashed."
            fi
            # Remove the PID file to clean up state
            rm -f /var/run/airpipe.pid
            echo "PID file removed."
        else
            echo "AirPipe is not running (no PID file)."
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    status)
        if [ -f /var/run/airpipe.pid ]; then
            PID=$(cat /var/run/airpipe.pid)
            if [ -n "$PID" ] && kill -0 $PID 2> /dev/null; then
                # Check if the process name matches 'airpipe'
                PROC_NAME=$(ps -p $PID -o comm=)
                if [[ "$PROC_NAME" == "airpipe" ]]; then
                    echo "AirPipe is running (PID: $PID)"
                else
                    echo "Process with PID $PID is not AirPipe, PID file might be stale"
                fi
            else
                echo "AirPipe is not running but PID file exists"
                # Consider cleaning up the stale PID file here
                rm /var/run/airpipe.pid
                echo "Stale PID file removed."
            fi
        else
            echo "AirPipe is not running (no PID file)"
        fi
        ;;
esac

exit 0
