VESC_TOOL ?= vesc_tool
PYTHON ?= python3
BASH ?= bash

GENERATED_DIR := generated
BINARY_STAMP := $(GENERATED_DIR)/binary_luts.stamp
LUT_BINARY := $(GENERATED_DIR)/display_lut.bin
DIST_FILES := README.dist.md ui.dist.qml

# Source files that should trigger a rebuild
SOURCE_FILES := blacktip_dpv.lisp ui.qml pkgdesc.qml README.md
ASSET_FILES := assets/display_lut.csv
BINARY_GENERATOR := tools/generate_lut_binary.py
VERSION_GENERATOR := tools/update_version.sh
LISP_STRIPPER := tools/strip_lisp.py

# The packaged Lisp is a comment-free, indentation-free copy of
# blacktip_dpv.lisp by default (see "Packaged Lisp is stripped" in
# DEVELOPMENT.md). STRIP_LISP=0 packages the unmodified source instead.
STRIP_LISP ?= 1
ifeq ($(STRIP_LISP),0)
PKG_MODE := unstripped
else ifeq ($(STRIP_LISP),1)
PKG_MODE := stripped
else
$(error STRIP_LISP must be 0 or 1, got '$(STRIP_LISP)')
endif

# vesc_tool resolves pkgLisp, pkgQml, pkgDescriptionMd and the import path
# relative to the working directory, so the stripped build runs it in a staging
# directory that mirrors the package layout with the stripped Lisp in place of
# the source. pkgdesc.qml is copied unchanged, so the package descriptor stored
# in the package is identical in both modes.
STAGE_DIR := $(GENERATED_DIR)/pkg
STRIPPED_LISP := $(STAGE_DIR)/blacktip_dpv.lisp
# make does not track variables: the stamp changes only when the mode changes,
# so switching between stripped and unstripped rebuilds the package.
MODE_STAMP := $(GENERATED_DIR)/pkg_mode.stamp

# Track the current git branch to detect branch changes
CURRENT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
BRANCH_FILE := .git_branch_cache

all: blacktip_dpv.vescpkg

$(BINARY_STAMP): $(ASSET_FILES) $(BINARY_GENERATOR)
	@mkdir -p $(GENERATED_DIR)
	$(PYTHON) $(BINARY_GENERATOR)
	@touch $@

$(LUT_BINARY): $(BINARY_STAMP)

# Update branch cache if it doesn't exist or branch has changed
$(BRANCH_FILE): FORCE
	@OLD_BRANCH=$$(cat $@ 2>/dev/null || echo ""); \
	if [ "$$OLD_BRANCH" != "$(CURRENT_BRANCH)" ]; then \
		echo "$(CURRENT_BRANCH)" > $@; \
	fi

# Generate distribution README when source files or branch changes
$(DIST_FILES) &: $(SOURCE_FILES) $(VERSION_GENERATOR) $(BRANCH_FILE)
	@$(BASH) $(VERSION_GENERATOR)

$(MODE_STAMP): FORCE
	@mkdir -p $(GENERATED_DIR)
	@if [ "$$(cat $@ 2>/dev/null)" != "$(PKG_MODE)" ]; then \
		echo "$(PKG_MODE)" > $@; \
	fi

# On failure the stripper removes any existing output, so make stops here and
# the package is never built from a stale stripped file.
$(STRIPPED_LISP): blacktip_dpv.lisp $(LISP_STRIPPER)
	@mkdir -p $(STAGE_DIR)
	$(PYTHON) $(LISP_STRIPPER) blacktip_dpv.lisp $@

ifeq ($(PKG_MODE),stripped)
blacktip_dpv.vescpkg: README.dist.md $(LUT_BINARY) $(MODE_STAMP) $(STRIPPED_LISP)
	@mkdir -p $(STAGE_DIR)/generated
	cp README.dist.md ui.dist.qml pkgdesc.qml icon.svg icon_light.svg $(STAGE_DIR)/
	cp $(LUT_BINARY) $(STAGE_DIR)/generated/
	cd $(STAGE_DIR) && $(VESC_TOOL) --buildPkgFromDesc pkgdesc.qml --testPkgDesc 'vesc:test'
	mv -f $(STAGE_DIR)/blacktip_dpv.vescpkg blacktip_dpv.vescpkg
else
blacktip_dpv.vescpkg: README.dist.md $(LUT_BINARY) $(MODE_STAMP)
	$(VESC_TOOL) --buildPkgFromDesc pkgdesc.qml --testPkgDesc 'vesc:test'
endif

# Unstripped package for bench work and debugging. This overwrites
# blacktip_dpv.vescpkg; run plain 'make' again for the stripped release package.
debug:
	@$(MAKE) --no-print-directory STRIP_LISP=0 blacktip_dpv.vescpkg

check-whitespace:
	@echo "Checking for trailing whitespace..."
	@if grep -nE '[[:space:]]+$$' blacktip_dpv.lisp; then \
		echo "Error: Trailing whitespace found (shown above)"; \
		exit 1; \
	else \
		echo "✓ No trailing whitespace found"; \
	fi

smoke-tests:
	@echo "Running smoke tests..."
	@$(PYTHON) tests/run_tests.py

binary: $(LUT_BINARY)

test: check-whitespace smoke-tests
	@echo "All checks passed"

clean:
	rm -f blacktip_dpv.vescpkg $(DIST_FILES) $(BRANCH_FILE)
	rm -rf $(GENERATED_DIR)

.PHONY: all test clean check-whitespace smoke-tests binary debug FORCE

FORCE:
