.DEFAULT_GOAL:=help
-include .makerc

# --- Config -----------------------------------------------------------------

export PATH := ./bin:$(PATH)

# --- Targets -----------------------------------------------------------------

# This allows us to accept extra arguments
%: brew .lefthook
	@:

.PHONY: .lefthook
# Configure git hooks for lefthook
.lefthook:
	@bin/lefthook install --reset-hooks-path

### Tasks

.PHONY: check
## Run lint & tests
check: tidy generate lint.fix test.race audit

.PHONY: brew
## Install project binaries
brew: build
	@bin/ownbrew install

.PHONY: lint
## Run linter
lint:
	@echo "〉golangci-lint run"
	@bin/golangci-lint run --max-same-issues 0 --max-issues-per-linter 0

.PHONY: lint.fix
## Fix lint violations
lint.fix:
	@echo "〉golangci-lint run fix"
	golangci-lint run --fix --max-same-issues 0 --max-issues-per-linter 0

.PHONY: generate
## Run go generate
generate:
	@echo "〉go generate"
	@go generate ./...

.PHONY: test
## Run tests
test:
	@echo "〉go test"
	@GO_TEST_TAGS=-skip go test -coverprofile=coverage.out -tags=safe ./...

.PHONY: test.race
## Run tests with -race
test.race:
	@echo "〉go test -race"
	@GO_TEST_TAGS=-skip go test -coverprofile=coverage.out -tags=safe -race ./...

.PHONY: build
## Build binary
build:
	@echo "〉go build bin/ownbrew"
	@mkdir -p bin
	@go build -o ./bin/ownbrew main.go

.PHONY: install
## Install binary
install: GOTPATH=$(shell go env GOPATH)
install:
	@go build -o ${GOPATH}/bin/ownbrew main.go

.PHONY: install.debug
## Install debug binary
install.debug: GOTPATH=$(shell go env GOPATH)
install.debug:
	@go build -gcflags "all=-N -l" -o ${GOPATH}/bin/ownbrew main.go

### Security

.PHONY: audit
## Run security audit
audit:
	@echo "〉security audit"
	@go install golang.org/x/vuln/cmd/govulncheck@latest
	@govulncheck ./...

### Dependencies

.PHONY: tidy
## Run go mod tidy
tidy:
	@echo "〉go mod tidy"
	@go mod tidy

.PHONY: outdated
## Show outdated direct dependencies
outdated:
	@echo "〉go mod outdated"
	@go list -u -m -json all | go-mod-outdated -update -direct

.PHONY: upgrade
## Show outdated direct dependencies
upgrade:
	@echo "〉go mod upgrade"
	@go list -u -m -f '{{if and (not .Indirect) .Update}}{{.Path}}{{end}}' all | xargs -n1 -I{} go get {}@latest
	@$(MAKE) tidy

### Documentation

.PHONY: docs
## Open docs
docs:
	@echo "〉starting docs"
	@cd docs && bun install && bun run dev

.PHONY: docs.build
## Build docs site
docs.build:
	@echo "〉building docs"
	@cd docs && bun install && bun run build

.PHONY: godocs
## Open go docs
godocs:
	@echo "〉starting go docs"
	@go doc -http

### Utils

.PHONY: help
# https://patorjk.com/software/taag/#p=display&f=Tmplr&t=ownbrew&x=none&v=4&h=4&w=80&we=false
## Show help text
help: g=\033[0;32m
help: b=\033[0;34m
help: w=\033[0;90m
help: e=\033[0m
help:
	@echo "$(g)"
	@echo "       ┓"
	@echo "┏┓┓┏┏┏┓┣┓┏┓┏┓┓┏┏"
	@echo "┗┛┗┻┛┛┗┗┛┛ ┗ ┗┻┛"
	@echo "with ❤ foomo by bestbytes"
	@echo "$(e)"
	@echo "$(b)Usage:$(e)\n  make [task]"
	@awk '{ \
		if($$0 ~ /^### /){ \
			if(help) printf "  %-21s $(w)%s$(e)\n\n", cmd, help; help=""; \
			printf "$(b)\n%s:$(e)\n", substr($$0,5); \
		} else if($$0 ~ /^[a-zA-Z0-9._-]+:/){ \
			cmd = substr($$0, 1, index($$0, ":")-1); \
			if(help) printf "  %-21s $(w)%s$(e)\n", cmd, help; help=""; \
		} else if($$0 ~ /^##/){ \
			help = help ? help "\n                        " substr($$0,3) : substr($$0,3); \
		} else if(help){ \
			print "\n                        $(w)" help "$(e)\n"; help=""; \
		} \
	}' $(MAKEFILE_LIST)
	@echo ""

