.PHONY: help lint typecheck format prepare build build-linux build-macos build-windows debian_package debian_changelog compress clean

PYTHON ?= poetry run python
PYINSTALLER ?= poetry run pyinstaller
VERSION ?= $(shell $(PYTHON) -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])")
SRC_DIR := src
DIST_DIR := dist
PYTHON_TARGETS := main.py scripts $(SRC_DIR)

ifeq ($(OS),Windows_NT)
HOST_PLATFORM := windows
else ifeq ($(shell uname -s),Darwin)
HOST_PLATFORM := macos
else ifeq ($(shell uname -s),Linux)
HOST_PLATFORM := linux
else
HOST_PLATFORM := unsupported
endif

ifeq ($(HOST_PLATFORM),macos)
ARCHIVE_BASE := macos/rocket.app
else
ARCHIVE_BASE := $(HOST_PLATFORM)
endif

define assert_host
$(if $(filter $(1),$(HOST_PLATFORM)),,$(error build-$(1) requires a $(1) host; current host is $(HOST_PLATFORM)))
endef

define assert_supported_host
$(if $(filter linux macos windows,$(HOST_PLATFORM)),,$(error unsupported build host: $(HOST_PLATFORM)))
endef

define build_bundle
$(PYINSTALLER) --clean -y \
	--distpath="$(DIST_DIR)/$(1)" \
	--workpath="build/$(1)" \
	packaging/rocket.spec
endef

help:
	@echo "Please use 'make <target>', where <target> is one of"
	@echo ""
	@echo "  lint                checks every Python source file with pylint"
	@echo "  typecheck           checks every Python source file with pyright"
	@echo "  format              formats every Python source file with autopep8"
	@echo "  prepare             updates the project version"
	@echo "  build               builds for the current operating system"
	@echo "  build-linux         builds on Linux into dist/linux"
	@echo "  build-macos         builds on macOS into dist/macos"
	@echo "  build-windows       builds on Windows into dist/windows"
	@echo "  debian_package      builds the Debian package"
	@echo "  debian_changelog    generates the Debian changelog"
	@echo "  compress            creates a zip archive of the built application"
	@echo "  clean               removes generated build artifacts"

lint:
	@poetry run pylint $(PYTHON_TARGETS)

typecheck:
	@poetry run pyright $(PYTHON_TARGETS)

format:
	@poetry run autopep8 --in-place --recursive $(PYTHON_TARGETS)

prepare:
	@read -p "Enter version number: " version; \
	$(PYTHON) scripts/version_manager.py --version $$version

build:
	@$(call assert_supported_host)
	@$(call build_bundle,$(HOST_PLATFORM))

build-linux:
	@$(call assert_host,linux)
	@$(call build_bundle,linux)

build-macos:
	@$(call assert_host,macos)
	@$(call build_bundle,macos)

build-windows:
	@$(call assert_host,windows)
	@$(call build_bundle,windows)

debian_package:
	@dpkg-buildpackage -us -uc

debian_changelog:
	@$(PYTHON) scripts/debian_changelog.py --version $(VERSION)

compress:
	@$(PYTHON) -c "import shutil; shutil.make_archive('rocket_$(VERSION)_$(HOST_PLATFORM)', 'zip', '$(DIST_DIR)', '$(ARCHIVE_BASE)')"

clean:
	@rm -rf build/ $(DIST_DIR)/
	@rm -rf debian/rocket debian/source/include-binaries debian/.debhelper debian/.pyinstaller
	@rm -f debian/debhelper-build-stamp debian/rocket.substvars debian/files
