FN := http

VERSION := $(shell git describe --always --dirty=-dirty)
ARCH := $(shell uname -m)
UNAME_S := $(shell uname -s)
GOLDFLAGS += -X main.Version=$(VERSION)
GOFLAGS := -ldflags "$(GOLDFLAGS)"

default: clean debug

commit: fmt lint
	git commit -a

# get gofumpt with:
# go install mvdan.cc/gofumpt@latest
fmt:
	gofumpt -l -w .
	golangci-lint run --fix

fmt-check:
	@test -z "$$(gofmt -l .)" || { echo "gofmt found unformatted files:"; gofmt -l .; exit 1; }

lint:
	golangci-lint run

test:
	go test ./...

check: fmt-check lint test

build: ./$(FN)d

hooks:
	@mkdir -p .git/hooks
	@printf '#!/bin/sh\nmake fmt-check lint\n' > .git/hooks/pre-commit
	@chmod +x .git/hooks/pre-commit
	@echo "Pre-commit hook installed."

debug: ./$(FN)d
	DEBUG=1 GOTRACEBACK=all ./$(FN)d

debugger:
	cd cmd/$(FN)d && dlv debug main.go

run: ./$(FN)d
	./$(FN)d

clean:
	-rm -f ./$(FN)d debug.log

docker:
	docker build --progress plain .

./$(FN)d: cmd/$(FN)d/main.go internal/*/*.go templates/* static/*
	cd ./cmd/$(FN)d && \
		go build -o ../../$(FN)d $(GOFLAGS) .

tools:
	go install mvdan.cc/gofumpt@latest

.PHONY: default commit fmt fmt-check lint test check build hooks debug debugger run clean docker tools
