PREFIX    ?= /usr
DESTDIR   ?=

# Rust target triple for cross-builds (empty = native). debian/rules sets it
# when cross-building; pass it on the command line otherwise (see `arm64`).
CARGO_TARGET ?=
CARGO_TARGET_FLAG = $(if $(CARGO_TARGET),--target $(CARGO_TARGET),)
TARGETDIR = target/$(if $(CARGO_TARGET),$(CARGO_TARGET)/,)release

BINDIR     = $(DESTDIR)$(PREFIX)/bin

.PHONY: all arm64 arm64-deps start start-configure start-root start-desktop start-desktop-root start-full install uninstall clean distclean

all:
	cargo build --release $(CARGO_TARGET_FLAG)

arm64-deps:
	sudo apt-get install crossbuild-essential-arm64 libstd-rust-dev:arm64 libstd-rust-1.85:arm64 libwayland-dev:arm64 libwayland-egl1:arm64 libxkbcommon-dev:arm64 libegl1-mesa-dev:arm64 libgles2-mesa-dev:arm64

arm64:
	$(MAKE) CARGO_TARGET=aarch64-unknown-linux-gnu

start: all
	LTK_THEMES_DIR=/home/pechanove/liberux/git/ltk/themes ./target/release/eydos-installer --test --windowsize 480x960

start-configure: all
	LTK_THEMES_DIR=/home/pechanove/liberux/git/ltk/themes ./target/release/eydos-installer --test --configure --windowsize 480x960

start-root: all
	sudo -E env \
		WAYLAND_DISPLAY="$$WAYLAND_DISPLAY" \
		XDG_RUNTIME_DIR="$$XDG_RUNTIME_DIR" \
		LTK_THEMES_DIR=/home/pechanove/liberux/git/ltk/themes \
		./target/release/eydos-installer --test --windowsize 480x960

start-desktop: all
	LTK_THEMES_DIR=/home/pechanove/liberux/git/ltk/themes ./target/release/eydos-installer --test --windowsize 1600x900

start-desktop-root: all
	sudo -E env \
		WAYLAND_DISPLAY="$$WAYLAND_DISPLAY" \
		XDG_RUNTIME_DIR="$$XDG_RUNTIME_DIR" \
		LTK_THEMES_DIR=/home/pechanove/liberux/git/ltk/themes \
		./target/release/eydos-installer --test --windowsize 1600x900


start-full: all
	LTK_THEMES_DIR=/home/pechanove/liberux/git/ltk/themes ./target/release/eydos-installer --test

install:
	install -Dm755 $(TARGETDIR)/eydos-installer $(BINDIR)/eydos-installer
	install -Dm755 data/eydos-installer-session $(BINDIR)/eydos-installer-session
	install -Dm755 data/eydos-installer-kiosk $(BINDIR)/eydos-installer-kiosk
	install -Dm755 data/eydos-firstboot-session $(BINDIR)/eydos-firstboot-session
	install -Dm755 data/eydos-reencrypt-hook $(DESTDIR)/etc/initramfs-tools/hooks/eydos-reencrypt
	install -Dm755 data/eydos-reencrypt-script $(DESTDIR)/etc/initramfs-tools/scripts/local-top/eydos-reencrypt

uninstall:
	rm -f $(BINDIR)/eydos-installer
	rm -f $(BINDIR)/eydos-installer-session
	rm -f $(BINDIR)/eydos-installer-kiosk
	rm -f $(BINDIR)/eydos-firstboot-session
	rm -f $(DESTDIR)/etc/initramfs-tools/hooks/eydos-reencrypt
	rm -f $(DESTDIR)/etc/initramfs-tools/scripts/local-top/eydos-reencrypt

clean:
	rm -rf debian/.cargo-home
	cargo clean

distclean: clean
	rm -f Cargo.lock
