#!/usr/bin/env bash
set -euo pipefail

BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$BASE_DIR"

PYTHON_BIN="${PYTHON_BIN:-python3}"
LOG_DIR="${LOG_DIR:-$BASE_DIR/output/logs}"
mkdir -p "$LOG_DIR"
STAMP="$(date +%Y%m%d_%H%M%S)"
LOG_PATH="$LOG_DIR/gateway_monitor_${STAMP}.log"

echo "[$(date --iso-8601=seconds)] Starting home network monitor run." | tee -a "$LOG_PATH"
"$PYTHON_BIN" "$BASE_DIR/scripts/probe_gateway.py" >>"$LOG_PATH" 2>&1
"$PYTHON_BIN" "$BASE_DIR/scripts/capture_gateway_login.py" >>"$LOG_PATH" 2>&1 || true
"$PYTHON_BIN" "$BASE_DIR/scripts/render_status.py" >>"$LOG_PATH" 2>&1
EXIT_CODE=$?
echo "[$(date --iso-8601=seconds)] Finished with exit code $EXIT_CODE." | tee -a "$LOG_PATH"
exit "$EXIT_CODE"
