SHELL := /bin/sh

CARGO ?= cargo
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share
WAYLAND_SESSIONS_DIR ?= /usr/share/wayland-sessions

BIN := target/release/compositron
DESKTOP_FILE := data/compositron.desktop

.PHONY: all build run test fmt clippy install uninstall clean

all: build

build:
	$(CARGO) build --release

# Run nested inside the current GNOME/Wayland session. This does not take
# over the seat or the display; it opens Compositron in a normal window.
run: build
	@test -n "$$WAYLAND_DISPLAY" || { \
		echo "No Wayland session detected. Run this from a GNOME Wayland session." >&2; \
		exit 1; \
	}
	@echo "Starting Compositron nested via WAYLAND_DISPLAY=$$WAYLAND_DISPLAY"
	@echo "The overview opens automatically. In GNOME use Ctrl+Alt+Space to reopen it."
	exec $(BIN) --winit

test:
	$(CARGO) test

fmt:
	$(CARGO) fmt

clippy:
	$(CARGO) clippy --all-targets -- -D warnings

# Build first, then run "sudo make install".
install:
	@test -x $(BIN) || { \
		echo "$(BIN) is missing; run 'make build' first." >&2; \
		exit 1; \
	}
	install -Dm755 $(BIN) $(DESTDIR)$(BINDIR)/compositron
	install -Dm644 $(DESKTOP_FILE) $(DESTDIR)$(WAYLAND_SESSIONS_DIR)/compositron.desktop
	@echo "Installed compositron and its session file."

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/compositron
	rm -f $(DESTDIR)$(WAYLAND_SESSIONS_DIR)/compositron.desktop
	@echo "Removed compositron."

clean:
	$(CARGO) clean
