.DEFAULT_GOAL := build
.NOTPARALLEL:

ifeq ($(OS),Windows_NT)
TOOL_BIN := out/fwtool.exe
else
TOOL_BIN := out/fwtool
endif
TOOL = ./$(TOOL_BIN)

RELEASE_ID ?=
SECURITY_VERSION ?= 1
PORT ?=
DEVICE_ID ?=
USB_VID ?= 0x303a
USB_PID ?= 0x1001
CDN_CONFIG ?= .secrets/cdn.json
CDN_CHANNEL ?= release
CDN_PREFIX ?= $(CDN_CHANNEL)
CDN_PACKAGE_BASE_URL ?= $(if $(filter release,$(CDN_CHANNEL)),https://s3.ybilly.com:6843/wheel,)
CDN_PUBLIC_URL ?=

.PHONY: tool init-security init-cdn prepare build build-recovery build-recovery-usb build-all buildfs ota ota-app ota-recovery-usb update update-app flash-full flash-plan test test-go test-native desktop package-desktop cdn-preflight publish-cdn publish-cdn-app publish-cdn-firmware publish-cdn-desktop

tool:
	go build -o "$(TOOL_BIN)" ./tools/fwtool

init-security: tool
	$(TOOL) init-security

init-cdn: tool
	$(TOOL) init-cdn --cdn-config "$(CDN_CONFIG)" $(if $(strip $(CDN_PUBLIC_URL)),--public-url "$(CDN_PUBLIC_URL)",)

prepare: tool
	$(eval BUILD_RELEASE := $(if $(RELEASE_ID),$(RELEASE_ID),$(shell $(TOOL) version)))
	$(if $(BUILD_RELEASE),,$(error Failed to resolve release identity))
	$(TOOL) prepare --release "$(BUILD_RELEASE)" --security-version "$(SECURITY_VERSION)" --init-if-missing

build: prepare
	$(TOOL) pio run -e wheel
	$(TOOL) check-app

build-recovery: build-recovery-usb

build-recovery-usb: prepare
	$(TOOL) pio run -e recovery-usb
	$(TOOL) check-recovery-usb

ota-recovery-usb: build-recovery-usb
	$(TOOL) pack --type recovery-usb --release "$(BUILD_RELEASE)" --security-version "$(SECURITY_VERSION)"

build-all: build build-recovery-usb

buildfs: prepare
	$(TOOL) pio run -e wheel -t buildfs

ota-app: build
	$(TOOL) pack --type app --release "$(BUILD_RELEASE)" --security-version "$(SECURITY_VERSION)"

ota: ota-app

update: update-app

update-app: ota-app
	$(if $(DEVICE_ID),,$(error DEVICE_ID must be the 12-digit USB device ID))
	go run ./tools/usbctl -device "$(DEVICE_ID)" -vid "$(USB_VID)" -pid "$(USB_PID)" -update out/firmware_pkg.bin

flash-full: build-all buildfs
	$(if $(PORT),,$(error PORT is required for explicit provisioning))
	$(if $(DEVICE_ID),,$(error DEVICE_ID is required to verify the target MAC))
	$(TOOL) flash-full --port "$(PORT)" --device "$(DEVICE_ID)"

flash-plan: build-all buildfs
	$(TOOL) flash-plan

test-go:
	go test ./tools/... ./desktop/...

test-native: tool
	$(TOOL) test-native

test: test-go test-native

NODE ?= C:/Users/billy/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/bin/node.exe

desktop: tool
	cd desktop/frontend && "$(NODE)" node_modules/typescript/bin/tsc --noEmit && "$(NODE)" node_modules/vite/bin/vite.js build
	go build -o out/wheel-usbctl.exe ./tools/usbctl
	$(TOOL) build-desktop --cdn-config "$(CDN_CONFIG)" --channel "$(CDN_CHANNEL)" $(if $(strip $(CDN_PUBLIC_URL)),--public-url "$(CDN_PUBLIC_URL)",) $(if $(RELEASE_ID),--release "$(RELEASE_ID)",)

package-desktop: desktop
	$(TOOL) pack-desktop $(if $(RELEASE_ID),--release "$(RELEASE_ID)",)

cdn-preflight: tool
	$(TOOL) cdn-preflight --cdn-config "$(CDN_CONFIG)" --channel "$(CDN_CHANNEL)" --prefix "$(CDN_PREFIX)" $(if $(strip $(CDN_PACKAGE_BASE_URL)),--package-base-url "$(CDN_PACKAGE_BASE_URL)",)

publish-cdn-firmware: publish-cdn-app

publish-cdn-app: cdn-preflight ota-app
	$(TOOL) publish-cdn --cdn-config "$(CDN_CONFIG)" --channel "$(CDN_CHANNEL)" --prefix "$(CDN_PREFIX)" $(if $(strip $(CDN_PUBLIC_URL)),--public-url "$(CDN_PUBLIC_URL)",) $(if $(strip $(CDN_PACKAGE_BASE_URL)),--package-base-url "$(CDN_PACKAGE_BASE_URL)",) --app-in out/firmware_pkg.bin

publish-cdn-desktop: cdn-preflight package-desktop
	$(TOOL) publish-cdn --cdn-config "$(CDN_CONFIG)" --channel "$(CDN_CHANNEL)" --prefix "$(CDN_PREFIX)" $(if $(strip $(CDN_PUBLIC_URL)),--public-url "$(CDN_PUBLIC_URL)",) $(if $(strip $(CDN_PACKAGE_BASE_URL)),--package-base-url "$(CDN_PACKAGE_BASE_URL)",) --desktop-zip out/wheel-desktop-pkg.zip

publish-cdn: cdn-preflight ota-app package-desktop
	$(TOOL) publish-cdn --cdn-config "$(CDN_CONFIG)" --channel "$(CDN_CHANNEL)" --prefix "$(CDN_PREFIX)" $(if $(strip $(CDN_PUBLIC_URL)),--public-url "$(CDN_PUBLIC_URL)",) $(if $(strip $(CDN_PACKAGE_BASE_URL)),--package-base-url "$(CDN_PACKAGE_BASE_URL)",) --app-in out/firmware_pkg.bin --desktop-zip out/wheel-desktop-pkg.zip
