#!/bin/bash
# Run script for MeshDash - Generated by install.sh on Mon  9 Feb 00:01:41 UTC 2026

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

CONFIG_FILE="$SCRIPT_DIR/.mesh-dash_config"
VENV_PATH="$SCRIPT_DIR/mesh-dash_venv"
PYTHON_EXEC="$VENV_PATH/bin/python"
MAIN_APP_SCRIPT="$SCRIPT_DIR/meshtastic_dashboard.py"

RUN_BLUE='\033[0;34m'; RUN_YELLOW='\033[0;33m'; RUN_RED='\033[0;31m'; RUN_BOLD='\033[1m'; RUN_NC='\033[0m'

run_echoinfo() { printf "${RUN_BLUE}[INFO]${RUN_NC} %s\n" "$1"; }
run_echoerror() { printf "${RUN_RED}${RUN_BOLD}[ERROR]${RUN_NC} %s\n" "$1" >&2; }

if [ ! -d "$VENV_PATH" ]; then
    run_echoerror "Virtual environment not found at: $VENV_PATH"
    exit 1
fi
if [ ! -x "$PYTHON_EXEC" ]; then
    run_echoerror "Python executable in virtual environment not found or not executable: $PYTHON_EXEC"
    exit 1
fi
if [ ! -f "$MAIN_APP_SCRIPT" ]; then
    run_echoerror "Main application script not found: $MAIN_APP_SCRIPT"
    exit 1
fi

run_echoinfo "Starting MeshDash..."
run_echoinfo "Using Python: $PYTHON_EXEC"
run_echoinfo "Application working directory: $SCRIPT_DIR"
run_echoinfo "Press Ctrl+C to stop the application if running in foreground."

cd "$SCRIPT_DIR" || { run_echoerror "Failed to change directory to $SCRIPT_DIR"; exit 1; }

"$PYTHON_EXEC" "$MAIN_APP_SCRIPT"

RUN_EXIT_CODE=$?
run_echoinfo "MeshDash finished with exit code $RUN_EXIT_CODE."
exit $RUN_EXIT_CODE
