PREFIX ?= $(HOME)/.local
BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man
GO     ?= go

# Static, reproducible binaries: no cgo, no local paths embedded.
BUILD = CGO_ENABLED=0 $(GO) build -trimpath

.PHONY: all build check test lint install uninstall clean

all: build

build:
	$(BUILD) -o build/x ./cmd/x
	$(BUILD) -o build/c ./cmd/c

check: lint test

test:
	$(GO) test -count=1 ./...

lint:
	@unformatted=$$(gofmt -l .); if [ -n "$$unformatted" ]; then \
		echo "gofmt needed:"; echo "$$unformatted"; exit 1; fi
	$(GO) vet ./...
	@if command -v staticcheck >/dev/null 2>&1; then staticcheck ./...; \
	else echo "staticcheck not installed; skipped"; fi

install: build
	install -d '$(DESTDIR)$(BINDIR)'
	install -m 755 build/x build/c '$(DESTDIR)$(BINDIR)/'
	install -d '$(DESTDIR)$(MANDIR)/man1'
	install -m 644 man/x.1 man/c.1 '$(DESTDIR)$(MANDIR)/man1/'

uninstall:
	rm -f '$(DESTDIR)$(BINDIR)/x' '$(DESTDIR)$(BINDIR)/c'
	rm -f '$(DESTDIR)$(MANDIR)/man1/x.1' '$(DESTDIR)$(MANDIR)/man1/c.1'

clean:
	rm -rf build
