.PHONY: default bootstrap setup test lint fmt fmt-check check docker hooks build clean try

# The version `make build` stamps into the binary: the git tag or short
# commit, -dirty with uncommitted changes. `make build VERSION=x` stamps x,
# which is how the Dockerfile passes its version in.
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)

# Standard targets are thin shims; the implementations live in script/
# per the scripts-to-rule-them-all pattern.

default: check build

bootstrap:
	@script/bootstrap

setup:
	@script/setup

test:
	@script/test

lint:
	@script/lint

fmt:
	@script/fmt

fmt-check:
	@script/fmt-check

check:
	@script/check

docker:
	@script/docker

hooks:
	@script/install-precommit

build: clean
	@go build -ldflags "-X main.Version=$(VERSION)" .

clean:
	@rm -f attrsum

# Runs the binary on three small files in a new temporary directory, which
# the trap removes when the recipe ends, also when a step fails. The check
# after clear is expected to fail, so its exit status is ignored.
try: build
	@set -ex; \
	dir=$$(mktemp -d); \
	trap 'rm -rf "$$dir"' EXIT; \
	echo one >"$$dir/a"; \
	echo two >"$$dir/b"; \
	echo three >"$$dir/c"; \
	./attrsum sum add -v "$$dir"; \
	./attrsum check -v "$$dir"; \
	./attrsum clear -v "$$dir"; \
	./attrsum check -v "$$dir" || true; \
	./attrsum sum add -v "$$dir"; \
	./attrsum check -v "$$dir"; \
	touch "$$dir"/*; \
	./attrsum sum update -v "$$dir"; \
	./attrsum check -v "$$dir"
