#!/usr/bin/env bash
###
# File: sunshine-stop
# Project: bin
# File Created: Tuesday, 23rd August 2023 3:28:52 pm
# Author: Josh.5 (jsunnex@gmail.com)
# -----
# Last Modified: Friday, 23rd August 2023 4:21:00 pm
# Modified By: Josh.5 (jsunnex@gmail.com)
###
set -e

exec >> >(tee -a ${USER_HOME:?}/.cache/log/sunshine_stop.log) 2>&1
echo
echo "-------------------------------"
echo
date
echo
echo "-------------------------------"
echo
echo "**** Execute sunshine-stop ****"
echo

# Read the run config
if [ -f /tmp/sunshine-exec-run.conf ]; then
    echo "  - Reading sunshine-run config."
    . /tmp/sunshine-exec-run.conf
else
    echo "  - WARNING: No sunshine-run config found for import."
fi

# Read the process PID
if [ -f /tmp/sunshine-exec-run.pid ]; then
    main_process_pid="$(cat /tmp/sunshine-exec-run.pid)"
    echo "  - Found initial sunshine-run PID '${main_process_pid}'."
else
    echo "  - WARNING: No sunshine-run PID found."
fi

# Send a INT signal to the PID (if it is found)
if [ "X${main_process_pid:-}" != "X" ]; then
    echo "  - Sending SIGINT to PID '${main_process_pid}'."
    kill -INT "${main_process_pid}" || true
    sleep 1
fi

# Run a loop to check for any other "sunshine-run" processes and send them also a INT signal
echo "  - Checking for other sunshine-run processes..."
for process_pid in $(ps aux | grep -v grep | grep sunshine-run | awk '{print $2}'); do
    # Ensure this process is still running
    if ! kill -0 "$process_pid" 2>/dev/null; then
        continue
    fi
    echo "  - Stopping PID '${process_pid}' - $(ps -p ${process_pid} -o command | awk 'NR>1')"
    # Send this process a SIGINT
    echo "      - Sending SIGINT to PID '${process_pid}'."
    kill -INT "${process_pid}" 2>/dev/null || true
    sleep 0.5
    # Send this process a SIGTERM every 2 seconds util stopped or until it has sent 3 signals
    counter=0
    while kill -0 "$process_pid" 2>/dev/null; do
        echo "      - Sending SIGTERM to PID '${process_pid}'."
        kill -TERM "$process_pid" 2>/dev/null || true
        counter=$((counter + 1))
        [ "$counter" -gt 2 ] && break
        sleep 2
    done
    # Send this process a SIGKILL every second until stopped or until it has sent 3 signals
    counter=0
    while kill -0 "$process_pid" 2>/dev/null; do
        echo "      - Sending SIGKILL to PID '${process_pid}'."
        kill -KILL "$process_pid" 2>/dev/null || true
        counter=$((counter + 1))
        [ "$counter" -gt 2 ] && break
        sleep 1
    done
    if ! kill -0 "$process_pid" 2>/dev/null; then
        echo "      - PID '${process_pid}' stopped."
    else
        echo "      - ERROR: Failed to kill PID '${process_pid}'."
    fi
done
echo "  - All sunshine-run related processes are stopped."

# Check if the required information was stored for resetting the display resolution and refresh rate
if ([ "X${primary_output:-}" != "X" ] && [ "X${display_mode:-}" != "X" ] && [ "X${display_rate:-}" != "X" ]); then
    # Reset display resolution and refresh rate
    echo "  - Resetting display resolution to ${display_mode:?} ${display_rate:?}Hz"
    xrandr --output "${primary_output:?}" --mode "${display_mode:?}" --rate "${display_rate:?}"
else
    echo "  - WARNING: original display config not found. Resolution will not be reset."
fi

# Clean up config file
if [ -f /tmp/sunshine-exec-run.conf ]; then
    echo "  - Removing sunshine-run config."
    rm -f /tmp/sunshine-exec-run.conf
else
    echo "  - WARNING: No sunshine-run config found for removal."
fi

# Clean up PID file
if [ -f /tmp/sunshine-exec-run.pid ]; then
    echo "  - Removing sunshine-run pidfile."
    rm -f /tmp/sunshine-exec-run.pid
else
    echo "  - WARNING: No sunshine-run pidfile found for removal."
fi

echo "DONE"
