COMPOSE ?= docker compose

# Compose does not support nested ${VAR:-${HOME}/...}; set defaults here (override in .env).
-include .env
export GOMODCACHE ?= $(HOME)/go/pkg/mod
export GOCACHE ?= $(HOME)/.cache/go-build

# One-off commands in the dev image (same caches and ./app mount as `make dev`)
DEV_RUN := $(COMPOSE) run --rm --no-deps -T dev

.PHONY: dev build up down generate tidy logs thumbs sync-media publish convert-videos

.DEFAULT_GOAL := dev

dev:
	@test -f .env || cp -n .env.example .env
	$(COMPOSE) up --build dev

build:
	$(COMPOSE) build website

up:
	@test -f .env || cp -n .env.example .env
	$(COMPOSE) up --build website

down:
	$(COMPOSE) down

generate:
	$(DEV_RUN) sh -c "templ generate"

tidy:
	$(DEV_RUN) go mod tidy

logs:
	$(COMPOSE) logs -f dev

# Pre-build gallery thumbnails and video poster JPEGs (ffmpeg in dev image).
thumbs:
	$(DEV_RUN) go run ./cmd/server -thumbs

# Copy photos and convert videos from upload/ into app/images/.
sync-media:
	$(DEV_RUN) sh -c 'UPLOAD_DIR=/upload IMAGES_DIR=/app/images ./scripts/sync-media.sh $(ARGS)'

# sync-media then rebuild thumbs/hero derivatives under app/images/.
publish: sync-media thumbs

# Alias for sync-media (videos are converted as part of sync).
convert-videos: sync-media
