PREFIX  ?= /usr
DESTDIR ?=
# Rust target triple. Empty builds for this machine, into target/release.
RUST_TARGET ?=

CARGO_FLAGS = --release $(if $(RUST_TARGET),--target $(RUST_TARGET))
RELEASE_DIR = target/$(if $(RUST_TARGET),$(RUST_TARGET)/)release

BINDIR      = $(DESTDIR)$(PREFIX)/bin
APPSDIR     = $(DESTDIR)$(PREFIX)/share/applications
METAINFODIR = $(DESTDIR)$(PREFIX)/share/metainfo

LAUNCH      = LTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/eydos-viewer

# hayro needs a newer Rust than Debian's, so the build takes the one rust-toolchain.toml names from rustup, inside the tree unless RUSTUP_HOME and CARGO_HOME name a rustup to reuse.
RUST_VERSION = $(shell sed -n 's/^channel *= *"\(.*\)"/\1/p' rust-toolchain.toml)
RUST_BUILD   = $(shell uname -m)-unknown-linux-gnu
export RUSTUP_HOME ?= $(CURDIR)/debian/.rustup
export CARGO_HOME  ?= $(CURDIR)/debian/.cargo
export RUSTUP_TOOLCHAIN = $(RUST_VERSION)
export PATH := $(CARGO_HOME)/bin:$(PATH)

# Viewer in a browser, for forge/web: the module and its JavaScript in target/web. wasm-bindgen-cli must be the version of the wasm-bindgen crate ltk pins.
WEB_TARGET    = wasm32-unknown-unknown
WEB_OUT       = target/web
WEB_TOOLS     = target/tools
WASM_BINDGEN_VERSION = 0.2.117
WASM_BINDGEN ?= $(or $(wildcard $(WEB_TOOLS)/bin/wasm-bindgen),$(shell command -v wasm-bindgen 2>/dev/null))
WEBDIR        = $(DESTDIR)$(PREFIX)/share/eydos-viewer/web

.PHONY: all arm64 arm64-deps run start start-desktop test install uninstall deb deb-arm64 clean distclean web web-tools web-deps install-web rust-toolchain

all: rust-toolchain
	$(if $(RUST_TARGET),rustup target add $(RUST_TARGET))
	cargo build $(CARGO_FLAGS)

# Idempotent: rustup is fetched only when missing, and an installed toolchain is kept.
rust-toolchain:
	if [ ! -x "$(CARGO_HOME)/bin/rustup" ]; then \
		mkdir -p debian && \
		curl -sSf -o debian/rustup-init https://static.rust-lang.org/rustup/dist/$(RUST_BUILD)/rustup-init && \
		curl -sSf -o debian/rustup-init.sha256 https://static.rust-lang.org/rustup/dist/$(RUST_BUILD)/rustup-init.sha256 && \
		echo "$$(cut -d' ' -f1 debian/rustup-init.sha256)  debian/rustup-init" | sha256sum -c - && \
		chmod +x debian/rustup-init && \
		debian/rustup-init -y --no-modify-path --default-toolchain none --profile minimal && \
		rm -f debian/rustup-init debian/rustup-init.sha256; \
	fi
	rustup toolchain install $(RUST_VERSION) --profile minimal

# An installed ltk keeps its package's timestamps, so cargo cannot tell it changed. Stripping at link time would drop the custom sections wasm-bindgen reads.
web: rust-toolchain
	@test -n "$(WASM_BINDGEN)" || { echo "No wasm-bindgen; run make web-deps"; exit 1; }
	rustup target add $(WEB_TARGET)
	cargo clean --release --target $(WEB_TARGET) -p ltk
	cargo build --release --target $(WEB_TARGET) --config profile.release.strip=false
	rm -rf $(WEB_OUT)
	$(WASM_BINDGEN) --target web --no-typescript --out-dir $(WEB_OUT) target/$(WEB_TARGET)/release/eydos-viewer.wasm

web-tools: rust-toolchain
	test -x $(WEB_TOOLS)/bin/wasm-bindgen || cargo install wasm-bindgen-cli --version $(WASM_BINDGEN_VERSION) --locked --root $(WEB_TOOLS)

web-deps: web-tools

# For the eydos-viewer-web package, which forge-web-server serves at /viewer.
install-web:
	install -d $(WEBDIR)
	install -m644 $(WEB_OUT)/* $(WEBDIR)/

arm64: export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = aarch64-linux-gnu-gcc
arm64: export PKG_CONFIG = aarch64-linux-gnu-pkg-config
arm64: export PKG_CONFIG_ALLOW_CROSS = 1
arm64:
	$(MAKE) all RUST_TARGET=aarch64-unknown-linux-gnu

arm64-deps:
	sudo dpkg --add-architecture arm64
	sudo apt-get update
	sudo apt-get install -y crossbuild-essential-arm64 libwayland-dev:arm64 libxkbcommon-dev:arm64 libegl-dev:arm64 libgles2-mesa-dev:arm64

run: all
	$(LAUNCH)

start: all
	$(LAUNCH) --mode mobile

start-desktop: all
	$(LAUNCH) --mode desktop

test: rust-toolchain
	cargo test

install:
	install -Dm755 $(RELEASE_DIR)/eydos-viewer         $(BINDIR)/eydos-viewer
	install -Dm644 data/io.eydos.Viewer.desktop        $(APPSDIR)/io.eydos.Viewer.desktop
	install -Dm644 data/io.eydos.Viewer.metainfo.xml   $(METAINFODIR)/io.eydos.Viewer.metainfo.xml

uninstall:
	rm -f $(BINDIR)/eydos-viewer
	rm -f $(APPSDIR)/io.eydos.Viewer.desktop
	rm -f $(METAINFODIR)/io.eydos.Viewer.metainfo.xml

# `deb-arm64` cross-builds the arm64 package from amd64; see `make arm64-deps`.
deb:
	dpkg-buildpackage -us -uc -b

deb-arm64:
	dpkg-buildpackage -us -uc -b -a arm64

clean:
	cargo clean

distclean: clean
	rm -f Cargo.lock
	dh_clean
