PREFIX    ?= /usr
DESTDIR   ?=

BINDIR       = $(DESTDIR)$(PREFIX)/bin
SHAREDIR     = $(DESTDIR)$(PREFIX)/share/crustace
WAYLANDDIR   = $(DESTDIR)$(PREFIX)/share/wayland-sessions
SYSTEMDDIR   = $(DESTDIR)/lib/systemd/user
PORTALCFGDIR = $(DESTDIR)/etc/xdg/xdg-desktop-portal
LOGINDDROPIN = $(DESTDIR)/etc/systemd/logind.conf.d

.PHONY: all install uninstall clean distclean

all:
	cargo build --release

FORGE_TEST_DIR = /tmp/forge-test-config/forge

# Pass `THEME=light` or `THEME=dark` on the make line to force a mode.
THEME_FLAG = $(if $(THEME),--$(THEME),)

# Pass `SCALE=2` on the make line to force the compositor output scale
# (HiDPI testing under winit). Unset leaves forge's computed default of 1.
ifdef SCALE
export FORGE_SCALE := $(SCALE)
endif

arm64-deps:
	sudo apt-get install libstd-rust-dev:arm64 libstd-rust-1.85:arm64 libwayland-dev:arm64 libwayland-egl1:arm64 libxkbcommon-dev:arm64 libegl-dev:arm64 libgles2-mesa-dev:arm64 libpam0g-dev:arm64 libdbus-1-dev:arm64 libssl-dev:arm64

arm64:
	cargo build --release --target aarch64-unknown-linux-gnu

start-software: all
	mkdir -p $(FORGE_TEST_DIR)
	printf '#!/bin/sh\nLTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/crustace --light $(THEME_FLAG) 2>/tmp/crustace.log &\n' > $(FORGE_TEST_DIR)/initrc
	chmod +x $(FORGE_TEST_DIR)/initrc
	FORGE_MOBILE=1 LTK_FORCE_SOFTWARE=1 LTK_THEMES_DIR=$(CURDIR)/../ltk/themes XDG_CONFIG_HOME=/tmp/forge-test-config forge --winit --size 480x960

start-desktop-software: all
	mkdir -p $(FORGE_TEST_DIR)
	printf '#!/bin/sh\nLTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/crustace --light $(THEME_FLAG) 2>/tmp/crustace.log &\n' > $(FORGE_TEST_DIR)/initrc
	chmod +x $(FORGE_TEST_DIR)/initrc
	FORGE_MOBILE=0 LTK_FORCE_SOFTWARE=1 LTK_THEMES_DIR=$(CURDIR)/../ltk/themes XDG_CONFIG_HOME=/tmp/forge-test-config forge --winit --size 1600x900

start: all
	mkdir -p $(FORGE_TEST_DIR)
	printf '#!/bin/sh\nLTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/crustace --light $(THEME_FLAG) 2>/tmp/crustace.log &\n' > $(FORGE_TEST_DIR)/initrc
	chmod +x $(FORGE_TEST_DIR)/initrc
	LTK_SCALE_DEBUG=1 FORGE_MOBILE=1 LTK_THEMES_DIR=$(CURDIR)/../ltk/themes XDG_CONFIG_HOME=/tmp/forge-test-config forge --winit --size 480x960 2>/tmp/winit-scale.log

start-dark: all
	mkdir -p $(FORGE_TEST_DIR)
	printf '#!/bin/sh\nLTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/crustace --dark $(THEME_FLAG) 2>/tmp/crustace.log &\n' > $(FORGE_TEST_DIR)/initrc
	chmod +x $(FORGE_TEST_DIR)/initrc
	FORGE_MOBILE=1 LTK_THEMES_DIR=$(CURDIR)/../ltk/themes XDG_CONFIG_HOME=/tmp/forge-test-config forge --winit --size 480x960

start-desktop: all
	mkdir -p $(FORGE_TEST_DIR)
	printf '#!/bin/sh\nforge msg orientation --lock normal\nLTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/crustace --light $(THEME_FLAG) &\n' > $(FORGE_TEST_DIR)/initrc
	chmod +x $(FORGE_TEST_DIR)/initrc
	FORGE_MOBILE=0 LTK_THEMES_DIR=$(CURDIR)/../ltk/themes XDG_CONFIG_HOME=/tmp/forge-test-config forge --winit --size 1600x900

start-desktop-dark: all
	mkdir -p $(FORGE_TEST_DIR)
	printf '#!/bin/sh\nforge msg orientation --lock normal\nLTK_THEMES_DIR=$(CURDIR)/../ltk/themes $(CURDIR)/target/release/crustace --dark $(THEME_FLAG) &\n' > $(FORGE_TEST_DIR)/initrc
	chmod +x $(FORGE_TEST_DIR)/initrc
	FORGE_MOBILE=0 LTK_THEMES_DIR=$(CURDIR)/../ltk/themes XDG_CONFIG_HOME=/tmp/forge-test-config forge --winit --size 1600x900

install:
	install -Dm755 target/release/crustace      $(BINDIR)/crustace
	install -Dm755 data/crustace-session.sh     $(BINDIR)/crustace-session
	install -Dm755 data/gnome-systemd-autostart-condition $(BINDIR)/gnome-systemd-autostart-condition
	install -Dm755 data/session-init            $(SHAREDIR)/session-init
	install -Dm644 data/crustace.desktop        $(WAYLANDDIR)/crustace.desktop
	install -Dm644 data/crustace.service        $(SYSTEMDDIR)/crustace.service
	install -Dm644 data/crustace-shutdown.target $(SYSTEMDDIR)/crustace-shutdown.target
	install -Dm644 data/crustace-portals.conf   $(PORTALCFGDIR)/crustace-portals.conf
	install -Dm644 data/lid-handling.conf       $(LOGINDDROPIN)/50-crustace.conf

uninstall:
	rm -f $(BINDIR)/crustace
	rm -f $(BINDIR)/crustace-session
	rm -f $(BINDIR)/gnome-systemd-autostart-condition
	rm -f $(SHAREDIR)/session-init
	rm -f $(WAYLANDDIR)/crustace.desktop
	rm -f $(SYSTEMDDIR)/crustace.service
	rm -f $(SYSTEMDDIR)/crustace-shutdown.target
	rm -f $(PORTALCFGDIR)/crustace-portals.conf
	rm -f $(LOGINDDROPIN)/50-crustace.conf

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

distclean: clean
	rm -f Cargo.lock
