.PHONY: all build ui test deb deb-dev repo-push r2-push publish release-check source-release bump-major bump-minor bump-patch clean

VERSION := $(shell tr -d '[:space:]' < VERSION)
COMMIT  := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
LDFLAGS := -X github.com/maliqua/proxdock/internal/version.Version=$(VERSION) \
           -X github.com/maliqua/proxdock/internal/version.Commit=$(COMMIT)

all: build

# The webui package embeds internal/webui/build; make sure it exists.
internal/webui/build:
	./scripts/build-ui.sh

ui:
	./scripts/build-ui.sh

build: internal/webui/build
	CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o proxdock ./cmd/proxdock

test: internal/webui/build
	go test ./...

# A release build: the public source tarball first, then the package
# built inside that exported tree with the tarball's checksum linked in
# (scripts/release.sh). deb-dev is the direct, unpublished build for
# quick local iteration and never reaches /var/builds or the repo.
deb:
	@if [ -x scripts/release.sh ]; then ./scripts/release.sh; else ./scripts/build-deb.sh; fi

deb-dev:
	NO_PUBLISH=1 ./scripts/build-deb.sh

# Push the built deb to the apt repository's testing channel. The
# endpoint and token are private config outside the tree (see
# scripts/push-repo.sh). `make publish` = build + push.
repo-push:
	./scripts/push-repo.sh

# Upload the source tarball to the R2 bucket the download page links to
# (credentials in ~/.config/proxdock-release/r2.env; skipped if absent).
r2-push:
	./scripts/push-r2.sh

publish: deb repo-push r2-push

# Public source export (AGPL corresponding source): sanitized, built,
# tested, tarred with the prebuilt UI. See scripts/export-source.sh.
source-release: ui
	./scripts/export-source.sh

# Release gate, not part of `test`: stamps are cleared on purpose during
# development, so this blocks shipping rather than blocking the build.
release-check:
	./scripts/check-template-stamps.sh

bump-major:
	./scripts/bump-version.sh major
bump-minor:
	./scripts/bump-version.sh minor
bump-patch:
	./scripts/bump-version.sh patch

clean:
	rm -rf proxdock dist internal/webui/build web/build web/.svelte-kit
