# todo-zig. Zig comes from the repo's scripts/env.sh; every Zig cache is
# kept under .cache/ so clean-deps can remove compiled artifacts without
# touching anything outside this directory.

SHELL := bash
ZIG ?= zig
VERSION ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo dev)

export ZIG_GLOBAL_CACHE_DIR := $(CURDIR)/.cache/zig
export ZIG_LOCAL_CACHE_DIR := $(CURDIR)/.cache/zig-local
export ZIG_LOCAL_PKG_DIR := $(CURDIR)/.cache/pkg

BUILD = $(ZIG) build -Doptimize=ReleaseSafe -Dversion=$(VERSION)
SRC = build.zig build.zig.zon $(wildcard src/*.zig static/*) ../../shared/base.css ../../shared/index.css

.PHONY: build build-arm64 test clean clean-deps bump unbump deploy

build: $(SRC)
	$(BUILD) -Dtarget=native-linux-musl --prefix .cache/out-native
	install -D .cache/out-native/bin/app dist/app

build-arm64: $(SRC)
	$(BUILD) -Dtarget=aarch64-linux-musl --prefix .cache/out-arm64
	install -D .cache/out-arm64/bin/app dist/app-linux-arm64

test:
	$(ZIG) build test -Dtarget=native-linux-musl --summary all

clean:
	rm -rf dist .cache/out-native .cache/out-arm64

# Keeps the downloaded SQLite sources (.cache/zig/p and .cache/pkg) and
# drops everything compiled from them.
clean-deps: clean
	rm -rf .cache/zig-local .cache/zig/o .cache/zig/h .cache/zig/z .cache/zig/b .cache/zig/tmp

bump:
	sed -i 's/^const health_body = "ok";$$/const health_body = "ok (bumped)";/' src/app.zig

unbump:
	sed -i 's/^const health_body = "ok (bumped)";$$/const health_body = "ok";/' src/app.zig

deploy:
	carlos deploy --account bfj --app todo-zig --target linux-arm64 --version $(VERSION) \
		--message "$${MESSAGE:-todo-zig $(VERSION)}" dist/app-linux-arm64
