#!/usr/bin/env bash
#MISE description="Run examples/gastro with Tailwind CSS rebuild on change"

# Hot-reload loop for the gastro-website example.
#
#   tailwindcss        rebuilds static/styles.css when tailwind.css or
#                      any scanned .gastro file changes. Wired as --asset
#                      (not --build) so it ALSO runs on RELOAD-class
#                      changes (template-body edits). --build only fires
#                      on RESTART-class changes, so putting Tailwind
#                      there would silently miss new utility classes
#                      introduced by body-only edits.
#   go build           rebuilds the binary against the regenerated
#                      .gastro/ package after gastro watch's own codegen
#   gastro watch       owns the .gastro -> .gastro/ codegen step itself,
#                      which is why we don't list `gastro generate` as a
#                      separate --build (it would double-run).
#
# We invoke tailwindcss without --minify here so dev rebuilds stay fast
# and the un-minified output is easier to read in the browser inspector.
# The committed static/styles.css is the *minified* output produced by
# `go generate ./...` (see generate.go).

set -euo pipefail
cd "$(dirname "$0")/../../examples/gastro"

# Bootstrap: static/styles.css is gitignored, so a fresh clone has
# no CSS file. `gastro watch`'s first Generate copies static/ into
# .gastro/static/ BEFORE the --asset chain has a chance to run, so
# without this priming step the initial build would embed an empty
# CSS asset and the first page load would 404. Subsequent .gastro
# edits would recover, but that's a bad first impression. One sync
# tailwindcss run here means the .gastro/static/ copy is correct
# from the very first Generate.
if [ ! -f static/styles.css ]; then
  echo "bootstrap: generating static/styles.css"
  tailwindcss -i tailwind.css -o static/styles.css
fi

exec gastro watch \
  --asset 'tailwindcss -i tailwind.css -o static/styles.css' \
  --build 'go build -o tmp/app .' \
  --run   'tmp/app'
