.DEFAULT_GOAL := build
.PHONY: help
help: ## Display this help message
	@echo "Usage: make [target]"
	@echo
	@echo "Targets:"
	@awk '/^[a-zA-Z0-9_-]+:.*## / {printf "%s : %s\n", $$1, substr($$0, index($$0, "##") + 3)}' $(MAKEFILE_LIST) | sort | column -t -s ':'
	@echo

.PHONY: build
build: ## Build the escript
	mix escript.build

.PHONY: compile
compile: ## Compile the project
	mix compile --all-warnings --warnings-as-errors
	@echo

.PHONY: test
test: ## Run tests
	mix test
	@echo

.PHONY: cover
cover: ## Run tests with coverage
	mix coveralls
	@echo

.PHONY: dialyzer
dialyzer: ## Run Dialyzer
	MIX_ENV=dev mix dialyzer
	@echo

.PHONY: docs
docs: md-lint ## Generate documentation
	mix docs
	@echo

.PHONY: check
check: compile test dialyzer md-lint ## Run all checks
	@echo "All checks passed"

.PHONY: md-lint
md-lint: ## Lint markdown files
	markdownlint-cli2 --config .markdownlint.json {test,lib,docs}/**/*.md README.md

.PHONY: md-format
md-format: ## Format markdown files
	@git diff --exit-code >/dev/null || (printf "!! Uncommitted changes found\n!! Commit or stash first\n\n" >&2 && exit 1)
	@markdownlint-cli2 --config .markdownlint.json --fix {test,lib,docs}/**/*.md README.md

.PHONY: format
format: ## Format the code
	mix format

.PHONY: release
release: publish ## Alias for publish because apparently this is what my fingers' muscle memory prefers

.PHONY: publish
publish: check docs ## Publish the package
	mix hex.publish

.PHONY: republish
republish: check docs ## Republish the package (within like 20m of publishing the last version)
	mix hex.publish --replace

.PHONY: find-bak
find-bak: ## Find all backup files generated by fnord itself (**/*.*.bak)
	find . -name "*.*.bak" -print

.PHONY: clean-bak
clean-bak: ## Clean up backup files generated by fnord itself (**/*.*.bak)
	find . -name "*.*.bak" -print -exec rm -f {} \;

.PHONY: clean
clean: clean-bak ## Clean up ALL build artifacts, including dependencies and build directories
	mix clean
	rm -rf _build
	rm -rf deps
	rm -rf .elixir_ls
