#!/bin/zsh
set -e

script_dir=${0:A:h}
project_dir=${script_dir:h:h}
cd "$project_dir"

if [[ ! -f reports/260929/share_site/reports/260929/main.html ]]; then
  print -u2 "Report snapshot is missing. Run: .venv/bin/python reports/260929/build_share_site.py"
  exit 1
fi

if [[ ! -x .venv/bin/python ]]; then
  print -u2 "Project Python is missing: .venv/bin/python"
  exit 1
fi

local_ip=$(ifconfig en0 | awk '/inet / { print $2; exit }')
tailscale_ip=$(ifconfig | awk '$1 == "inet" && $2 ~ /^100\./ { print $2; exit }')
print "Serving reports on port 8766. Keep this Terminal window open."
print "Local: http://127.0.0.1:8766/reports/260929/main.html"
if [[ -n "$local_ip" ]]; then
  print "LAN:   http://$local_ip:8766/reports/260929/main.html"
fi
if [[ -n "$tailscale_ip" ]]; then
  print "Tailscale: http://$tailscale_ip:8766/reports/260929/main.html"
fi
print "Press Ctrl-C to stop."
exec .venv/bin/python reports/260929/serve_share.py --host 0.0.0.0 --port 8766
