VERSION ?= 0.1.0
# No -X main.version here: cmd/haul has no version var yet, and a -X against a
# symbol that doesn't exist is silently a no-op rather than an error.
LDFLAGS := -s -w

# Go is installed at ~/.local/go on this box and deliberately kept off PATH, so
# prefer that toolchain and fall back to whatever `go` is in PATH elsewhere.
GO ?= $(shell test -x $(HOME)/.local/go/bin/go && echo $(HOME)/.local/go/bin/go || echo go)

# The packaging tool lives outside the Fyne library repo since v2.5. Pinned and
# run through `go run` so no separate install step is needed; override with
# FYNE=/path/to/fyne if you'd rather use an installed copy.
FYNE ?= $(GO) run fyne.io/tools/cmd/fyne@v1.7.2

.PHONY: build install test vet check run run-xvfb dist package-darwin package-windows clean

build: ## Build a native binary into ./bin/haul
	$(GO) build -trimpath -ldflags "$(LDFLAGS)" -o bin/haul ./cmd/haul

install: ## Install haul into $GOBIN / $GOPATH/bin
	$(GO) install -trimpath -ldflags "$(LDFLAGS)" ./cmd/haul

test: ## Run tests
	$(GO) test ./...

vet: ## Run go vet
	$(GO) vet ./...

# GOOS=js typechecks every package, Fyne API usage included, without needing the
# cgo/X11 toolchain. It proves nothing about layout or runtime behaviour.
check: ## Typecheck the whole tree without cgo
	GOOS=js GOARCH=wasm $(GO) build ./...

run: build ## Build and run (needs a DISPLAY)
	./bin/haul

# This box is headless. Xvfb gives haul a display to render into; software GL
# because there's no GPU.
#
# Cleanup is a trap on a PID held in a shell variable, never a pidfile and never
# pkill. A pidfile survives Ctrl-C and goes stale, and PIDs get recycled, so a
# later `kill $(cat .xvfb.pid)` can hit an unrelated process. `pkill -f Xvfb` is
# worse still: the pattern also matches make's own command line, and this box
# runs inside tmux, where a stray match can take out the session.
run-xvfb: build ## Build and run against a virtual X display
	@if [ -e /tmp/.X99-lock ]; then \
		echo "display :99 is already in use — close that first"; exit 1; \
	fi; \
	Xvfb :99 -screen 0 1500x900x24 >/dev/null 2>&1 & \
	xvfb_pid=$$!; \
	trap 'kill $$xvfb_pid 2>/dev/null || true' INT TERM EXIT; \
	sleep 1; \
	DISPLAY=:99 LIBGL_ALWAYS_SOFTWARE=1 ./bin/haul || true

# haul is cgo (Fyne needs OpenGL + X11), so `GOOS=windows go build` does NOT
# work the way it does for dial. Cross-targets need a matching C toolchain:
#   linux/arm64  -> apt install gcc-aarch64-linux-gnu
#   windows/amd64-> apt install gcc-mingw-w64
#   darwin/*     -> a Mac, or osxcross
# Docker is present, so `fyne-cross` is the practical route for the others:
#   go install github.com/fyne-io/fyne-cross@latest
#   fyne-cross windows -arch=amd64
dist: ## Build the release binary for this host into ./dist
	@mkdir -p dist
	$(GO) build -trimpath -ldflags "$(LDFLAGS)" -o dist/haul-linux-amd64 ./cmd/haul
	@echo "built dist/haul-linux-amd64 (see the Makefile for cross-compiling)"

# Finder hands a bare Unix executable to Terminal.app, so double-clicking the
# raw binary opens a terminal window alongside the GUI. Nothing in the code can
# stop that — the fix is to ship a .app bundle instead, which Finder launches
# directly. `fyne package` writes the Info.plist (CFBundlePackageType APPL and
# friends) and converts cmd/haul/Icon.png into Contents/Resources/icon.icns; the
# rest of the metadata comes from cmd/haul/FyneApp.toml.
#
# Run this ON a Mac: it compiles the binary with the host cgo toolchain, and it
# builds for the host architecture, so an Intel Mac gives an amd64 bundle and
# Apple silicon an arm64 one. The .app is unsigned, so first launch needs
# right-click -> Open (or `xattr -dr com.apple.quarantine haul.app`).
package-darwin: ## Package haul.app for macOS (run this on a Mac)
	$(FYNE) package --target darwin --src ./cmd/haul --release
	@echo "built ./haul.app — drag it into /Applications"

# A plain `go build` produces a haul.exe with no icon resource, so Explorer, the
# taskbar and the Start menu all draw the generic executable icon. Nothing in
# the installer can fix that — Inno's SetupIconFile only skins setup.exe itself.
# `fyne package` generates a .syso from cmd/haul/Icon.png (it converts the PNG
# itself; cmd/haul/Icon.ico is for Inno, not for this) and links it in, so the
# icon travels with the binary. It also adds -H=windowsgui on its own, so this
# needs none of the manual linker flags the README's build command does.
#
# Run this ON Windows, from the MSYS2 UCRT64 shell if you installed make there:
# haul is cgo, so it needs the host MinGW-w64 toolchain. Like package-darwin it
# rewrites cmd/haul/FyneApp.toml in place, bumping Build.
#
# fyne writes the binary beside the source, as cmd/haul/haul.exe. Move it into
# bin/, which is where Inno-compile.iss looks for it.
package-windows: ## Package haul.exe with an embedded icon (run this on Windows)
	$(FYNE) package --target windows --src ./cmd/haul --release
	@mkdir -p bin
	mv ./cmd/haul/haul.exe ./bin/haul.exe
	@echo "built ./bin/haul.exe — compile Inno-compile.iss to wrap it in an installer"

clean: ## Remove build artifacts
	rm -rf bin dist haul.app Output
