#!/usr/bin/env bash

# EXTREME DETAIL: 'set -e' is a critical safety feature in Bash. 
# It tells the script to immediately exit if any command returns a non-zero (error) status code.
# This replaces the need for the verbose 'try/except' blocks we had to write in Python.
set -e

echo ">>> [1/4] Running roi.py to generate updated JSON data..."
# We use 'uv run' here so the Python script is correctly executed within its virtual environment.
uv run roi.py

echo ">>> [2/4] Installing frontend dependencies via Bun..."
# Bash natively handles standard streams (stdin/stdout), so Bun will not trigger phantom 
# SIGINTs when drawing its interactive truck emojis here.
bun install

echo ">>> [3/4] Building the TypeScript frontend using Bun..."
# Even though this invokes a nested script from package.json, Bash correctly handles the 
# process group hierarchy, preventing the TTY driver crashes we saw in Python.
bun run build

echo ">>> [4/4] Starting local development server..."
echo ">>> Serving at http://localhost:8000"
echo ">>> Press Ctrl+C to stop the server."

# EXTREME DETAIL: We must change directories before starting the web server so that the 
# root of the localhost server is correctly mapped to the compiled files in the 'www' folder.
cd www

# We can call python3 directly here instead of using uv because the http.server module 
# is built into Python's standard library and does not require third-party dependencies.
python3 -m http.server 8000