MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
PROJECT = $(subst @,,$(notdir $(subst /workspace,,$(PROJECT_PATH))))

.DEFAULT_GOAL := help

CARGO_HOME = $(PROJECT_PATH)/vendor

ambient.qcow2:
	curl -s https://files.liw.fi/ambient/ambient.qcow2.xz | unxz -v > ambient.qcow2

.PHONY: test
test: ambient.qcow2
test: ## Run checks as they will run in CI
	make test "AMBIENT_IMAGE=$$(pwd)/ambient.qcow2"

.PHONY: vendor
vendor: ## Pre-fetch dependencies
	cargo --version
	CARGO_HOME=$(CARGO_HOME) cargo fetch --locked -v

.PHONY: build
build: ## Build the binary using zig
	CARGO_HOME=$(CARGO_HOME) cargo install cargo-zigbuild@0.19.3
	CARGO_HOME=$(CARGO_HOME) cargo zigbuild --locked --release \
		--target=aarch64-unknown-linux-musl \
		--target=x86_64-unknown-linux-musl


# Check http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
help: ## Print this help.
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort
