deps = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
.PHONY: test
test:
	go test ./...

.PHONY: test-golangci
test-golangci: $(deps-go)
	# https://www.jvt.me/posts/2024/09/30/go-tools-module/
	go run -mod=mod -modfile=tools/go.mod github.com/golangci/golangci-lint/v2/cmd/golangci-lint run ./...

.PHONY: test-pre-commit
test-pre-commit: test test-golangci
	go mod tidy -diff

.PHONY: build
build: staticomm
staticomm: $(deps)
	go build ./cmd/staticomm/

ifdef SYSTEM
PREFIX := $(DESTDIR)/usr
else ifdef USER_LOCAL
PREFIX := $(DESTDIR)$(HOME)/.local
else
PREFIX := $(DESTDIR)/usr/local
endif

BIN_DIR := $(PREFIX)/bin
LICENSES_DIR := $(PREFIX)/share/licenses/staticomm
COMPLETION_DIR_BASH := $(PREFIX)/share/bash-completion/completions
COMPLETION_DIR_FISH := $(PREFIX)/share/fish/vendor_completions.d
COMPLETION_DIR_ZSH := $(PREFIX)/share/zsh/site-functions

.PHONY: install
install: staticomm
	install -vDm 0755 staticomm -t $(BIN_DIR)
	install -vDm 0644 LICENSE.md -t $(LICENSES_DIR)
	bash -c 'install -vDm 0644 <(./staticomm completion bash) $(COMPLETION_DIR_BASH)/staticomm'
	bash -c 'install -vDm 0644 <(./staticomm completion fish) $(COMPLETION_DIR_FISH)/staticomm.fish'
	bash -c 'install -vDm 0644 <(./staticomm completion zsh) $(COMPLETION_DIR_ZSH)/_staticomm'

.PHONY: uninstall
uninstall:
	rm -vf $(BIN_DIR)/staticomm
	rm -vfr $(LICENSES_DIR)
	rm -vf $(COMPLETION_DIR_BASH)/staticomm
	rm -vf $(COMPLETION_DIR_FISH)/staticomm.fish
	rm -vf $(COMPLETION_DIR_ZSH)/_staticomm

.PHONY: link
link:
	ln -sf $(CURDIR)/staticomm $(BIN_DIR)/staticomm
	mkdir -pv $(LICENSES_DIR)
	ln -sf $(CURDIR)/LICENSE.md $(LICENSES_DIR)/LICENSE.md

.PHONY: release
release:
	./bin/generate_release_notes
	sh -c "git tag $$(./bin/generate_release_notes | head -n1) --annotate --sign --file <(./bin/generate_release_notes)"
	./bin/generate_obs_changes > ~/code/home:jcgl/staticomm/staticomm.changes
