export prefix ?= /usr
sysconfdir ?= /etc
bindir = $(prefix)/bin
libdir = $(prefix)/lib
libexecdir ?= $(prefix)/libexec
sharedir = $(prefix)/share

BINARY = cosmic-comp
CARGO_TARGET_DIR ?= target
TARGET = debug
DEBUG ?= 0

.PHONY = all clean install install-defaults uninstall vendor

ifeq ($(DEBUG),0)
	TARGET = release
	ARGS += --release
endif

VENDOR ?= 0
ifneq ($(VENDOR),0)
	ARGS += --offline --locked
endif

# HyprCosmic installs *beside* the distro's COSMIC rather than over it. Both
# sessions stay on the greeter's menu -- /usr/share/wayland-sessions holds
# cosmic.desktop and hyprcosmic.desktop at the same time -- which only works
# while this binary stays out of $(bindir)/cosmic-comp, where it would replace
# the compositor the stock session runs. A private directory is what keeps
# installing HyprCosmic from being a one-way door: if the fork misbehaves, the
# stock session is still there to log into and fix it from.
compdir ?= $(libexecdir)/hyprcosmic
TARGET_BIN="$(DESTDIR)$(compdir)/$(BINARY)"

KEYBINDINGS_CONF="$(DESTDIR)$(sharedir)/cosmic/com.system76.CosmicSettings.Shortcuts/v1/defaults"
TILING_EXCEPTIONS_CONF="$(DESTDIR)$(sharedir)/cosmic/com.system76.CosmicSettings.WindowRules/v1/tiling_exception_defaults"

all: extract-vendor
	cargo build $(ARGS)

clean:
	cargo clean

distclean:
	rm -rf .cargo vendor vendor.tar target

vendor:
	mkdir -p .cargo
	cargo vendor | head -n -1 > .cargo/config
	echo 'directory = "vendor"' >> .cargo/config
	[ -n "$(SOURCE_GIT_HASH)" ] && printf '\n[env]\nGIT_HASH = "%s"\n' "$(SOURCE_GIT_HASH)" >> .cargo/config || true
	tar pcf vendor.tar vendor
	rm -rf vendor

extract-vendor:
ifeq ($(VENDOR),1)
	rm -rf vendor; tar pxf vendor.tar
endif

# The binary only. The two .ron files are COSMIC's *shared* defaults, and they
# sit at paths the distro's own cosmic-comp package already owns, so installing
# them from here would make a HyprCosmic package conflict with that one over two
# files it has no reason to change. The fork reads the stock copies instead --
# which is honest, because a HyprCosmic session needs the rest of COSMIC present
# anyway for the greeter, the portals and the settings daemon.
#
# `install-defaults` keeps them reachable for anyone building this fork on its
# own, without a COSMIC install to borrow them from.
install:
	install -Dm0755 "$(CARGO_TARGET_DIR)/$(TARGET)/$(BINARY)" "$(TARGET_BIN)"

install-defaults:
	install -Dm0644 "data/keybindings.ron" "$(KEYBINDINGS_CONF)"
	install -Dm0644 "data/tiling-exceptions.ron" "$(TILING_EXCEPTIONS_CONF)"

install-bare-session: install
	install -Dm0644 "data/cosmic.desktop" "$(DESTDIR)$(sharedir)/wayland-sessions/cosmic.desktop"
	install -Dm0644 "data/cosmic-session.target" "$(DESTDIR)$(libdir)/systemd/user/cosmic-session.target"
	install -Dm0644 "data/cosmic-session-pre.target" "$(DESTDIR)$(libdir)/systemd/user/cosmic-session-pre.target"
	install -Dm0644 "data/cosmic-comp.service" "$(DESTDIR)$(libdir)/systemd/user/cosmic-comp.service"
	install -Dm0755 "data/cosmic-service" "$(DESTDIR)/$(bindir)/cosmic-service"

# Only what `install` placed. Removing KEYBINDINGS_CONF here would delete a file
# belonging to the distro's cosmic-comp, breaking the stock session on the way
# out -- the exact failure the private compdir above exists to prevent.
uninstall:
	rm "$(TARGET_BIN)"

uninstall-bare-session:
	rm "$(DESTDIR)$(sharedir)/wayland-sessions/cosmic.desktop"
