
## Configurables.
# The thing we run!
export BIN ?= junctor
# This project currently targets nRF52s.
export ARCH ?= thumbv7em-none-eabi
export CHIP ?= nRF52840_xxAA
# Toggle this env if you want to use release mode.
export RELEASE ?= false

# Pretty stuff!
FORMATTING_BEGIN_TITLE = \e[0;40m
FORMATTING_BEGIN_SETTING = \e[1;40m
FORMATTING_END = \e[0m

help:
	@printf -- "\n"
	@printf -- "                                      ${FORMATTING_BEGIN_TITLE}Junctor${FORMATTING_END}\n"
	@printf -- "                         ${FORMATTING_BEGIN_SETTING}A Hoverbear Consulting Experiment${FORMATTING_END}\n"
	@printf -- "\n"
	@printf -- "Usage:\n"
	@printf -- "  make <target> \n"
	@printf -- "\n"
	@printf -- "Tweakables:            (Default)            (Configured)\n"
	@egrep '^export [a-zA-Z_-]+ \?= .*?$$' $(MAKEFILE_LIST) | awk -f hack/variables.awk
	@printf -- "\n"
	@printf -- "Targets:               (Description)\n"
	@egrep '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk -f hack/targets.awk

## Non-configurables.
# Touch these and you will start mysteriously breaking things and I will not help you.
override ARTIFACT_BIN = target/${ARCH}/${BUILD_MODE}/${BIN}
override BUILD_MODE = $(if $(findstring true,$(RELEASE)),release,debug)
override MAYBE_RELEASE_FLAG = $(if $(findstring true,$(RELEASE)),--release,)
override ROOT_DIR = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

##@ Operations:
.PHONY := embed flash clean build update recover fetch

flash: ${ARTIFACT_BIN}
flash: ## Flash the device.
	cargo flash ${MAYBE_RELEASE_FLAG} --chip ${CHIP}

embed: ${ARTIFACT_BIN} ## Embed into the device.
	cargo embed ${MAYBE_RELEASE_FLAG}

build: ${ARTIFACT_BIN} ## Build the binary.

update: ## Update all dependencies.
	cargo update

fetch: ## Fetch a local copy of the dependencies.
	cargo fetch

clean: ## Clean up the working environment.
	cargo clean

recover: ## Recover the nRF device.
	nrf-recover -y

prerequisites: ## Bootstrap the Windows machine.
	pwsh -File distribution/bootstraps/windows.ps1

## Non-phony targets.
${ARTIFACT_BIN}:
	cargo build --target ${ARCH} ${MAYBE_RELEASE_FLAG} 

