# Veola build.
#
# Tool dependencies — the `*_VERSION` variables below are the single source of
# truth for every non-Go thing Veola pulls in at build time. To upgrade one,
# bump its version and run `make vendor` (vendored JS) or just `make css`
# (Tailwind) on a clean `bin/` to refetch.
#
# Go module dependencies live in go.mod and are bumped via `make update-deps`.

TAILWIND_VERSION := v3.4.17
HTMX_VERSION     := 2.0.4
CHARTJS_VERSION  := 4.4.6
TEMPL_VERSION    := v0.3.1020

TAILWIND_BIN     := bin/tailwindcss
# Override TAILWIND_URL for non-linux-x64 platforms — see README "Build-time
# tools" for the matching asset names on the Tailwind releases page.
TAILWIND_URL     ?= https://github.com/tailwindlabs/tailwindcss/releases/download/$(TAILWIND_VERSION)/tailwindcss-linux-x64

HTMX_URL    := https://unpkg.com/htmx.org@$(HTMX_VERSION)/dist/htmx.min.js
CHARTJS_URL := https://cdn.jsdelivr.net/npm/chart.js@$(CHARTJS_VERSION)/dist/chart.umd.min.js

TEMPL := $(shell go env GOPATH)/bin/templ

.PHONY: all generate css build run test clean tools vendor update-deps

all: build

$(TAILWIND_BIN):
	mkdir -p bin
	curl -sL --fail $(TAILWIND_URL) -o $(TAILWIND_BIN)
	chmod +x $(TAILWIND_BIN)

# Compile Tailwind utilities (scanned from the .templ sources) into
# static/css/tailwind.css. The hand-written component layer is app.css.
css: $(TAILWIND_BIN)
	$(TAILWIND_BIN) -c tailwind.config.js -i static/css/input.css -o static/css/tailwind.css --minify

# Regenerate templ Go from the .templ sources.
generate:
	$(TEMPL) generate

build: generate css
	go build -o veola-bin .

run: build
	./veola-bin -config config.toml

test:
	go test ./...

clean:
	rm -f veola-bin

# Install the pinned templ CLI into $(go env GOPATH)/bin. Pinning matters
# because two contributors on different templ versions can produce
# differently-formatted generated Go for the same .templ source.
tools:
	go install github.com/a-h/templ/cmd/templ@$(TEMPL_VERSION)

# Re-fetch vendored third-party JS at the currently pinned versions,
# overwriting whatever is on disk. Also clears bin/tailwindcss so the next
# `make css` pulls the pinned Tailwind binary. Use after bumping any of
# HTMX_VERSION / CHARTJS_VERSION / TAILWIND_VERSION.
vendor:
	curl -sL --fail $(HTMX_URL)    -o static/vendor/htmx.min.js
	curl -sL --fail $(CHARTJS_URL) -o static/vendor/chart.umd.min.js
	rm -f $(TAILWIND_BIN)
	$(MAKE) $(TAILWIND_BIN)

# Bump Go module dependencies to their newest compatible versions, then
# print upstream-release URLs for the non-Go tools so you can decide
# whether to bump those pins too. Does not modify the pinned versions.
update-deps:
	go get -u ./...
	go mod tidy
	@echo
	@echo "Pinned tool versions (current → upstream releases):"
	@printf "  Tailwind   %s  →  https://github.com/tailwindlabs/tailwindcss/releases/latest\n" "$(TAILWIND_VERSION)"
	@printf "  htmx       %s     →  https://github.com/bigskysoftware/htmx/releases/latest\n"  "$(HTMX_VERSION)"
	@printf "  Chart.js   %s     →  https://github.com/chartjs/Chart.js/releases/latest\n"      "$(CHARTJS_VERSION)"
	@printf "  templ      %s →  https://github.com/a-h/templ/releases/latest\n"                 "$(TEMPL_VERSION)"
