#!/bin/bash

# Original file contributed by Gesslar, adapted by Tsath 2024 for LIMA

# Change to the directory containing the script
cd "$(dirname "$0")"

# Get the full OS path to the driver executable
driver="$(pwd)/bin/driver"
config="$(pwd)/config.mud"

# Function to run the driver and check its exit status
run_driver() {
    echo "Running driver..."
    $driver $config
    local exit_status=$?
    echo "Driver finished running with exit status: $exit_status"
    return $exit_status
}

# Function to stop any existing driver processes
stop_driver() {
    echo "Stopping all running driver instances..."
    pids=$(pgrep -f "$driver")
    if [[ -n "$pids" ]]; then
        echo "Found driver processes with PIDs: $pids"
        kill $pids
        echo "Driver processes stopped."
    else
        echo "No running driver processes found."
    fi
}

# Handle script arguments
case "$1" in
    "")
        # Loop to restart the driver if it exits with status 0
        while true; do
            run_driver
            exit_status=$?

            echo "Exit status: $exit_status"

            if [[ $exit_status -eq 0 ]]; then
                echo "Driver exited with reboot status code. Restarting."
                sleep 1  # Optional: Add a small delay before restarting
            else
                echo "Driver exited with status $exit_status. Stopping."
                break
            fi
        done
        ;;
    bg)
        echo "Starting driver in the background..."
        nohup bash -c "$0" > run.log 2>&1 &
        echo "Driver started in the background with PID $!"
        exit 0
        ;;
    stop)
        stop_driver
        exit 0
        ;;
    *)
        echo "Usage: $0 [bg|stop]"
        exit 1
        ;;
esac