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

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

.PHONY = all clean install uninstall vendor

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

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

# HyprCosmic is a fork of COSMIC, not an add-on to it, so this binary goes where
# a cosmic-comp goes: $(bindir)/cosmic-comp. It is the compositor on a machine
# that installs it, and a HyprCosmic package accordingly conflicts with the
# distribution's cosmic-comp over this path -- which is the honest relationship
# between two builds of the same program, and is how every other distro-level
# fork declares itself.
#
# This used to install to a private $(libexecdir)/hyprcosmic so both sessions
# could coexist. That bought a fallback and cost coherence: two compositors on
# disk, a session that had to name absolute paths to find its own, and no
# answer to "which cosmic-comp is running". The fallback survives in a better
# form -- cosmic-session installs cosmic.desktop as well as hyprcosmic.desktop,
# so the greeter still offers a stock COSMIC shell, now served by this binary.
TARGET_BIN="$(DESTDIR)$(bindir)/$(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

# Upstream's install, restored. The two .ron files are the compositor's shared
# defaults and it will not find a shortcut or a tiling exception without them,
# so a build that means to be the only cosmic-comp on the machine has to place
# them. They were split out into `install-defaults` while this fork installed
# beside a COSMIC it could borrow them from; there is nothing to borrow from now.
#
# `bind` lines in cosmic.conf are still written to the Shortcuts *custom* key,
# which cosmic-comp merges over these defaults. That is unchanged and is what
# keeps `cosmic-conf apply` from having to edit the file installed here.
install:
	install -Dm0755 "$(CARGO_TARGET_DIR)/$(TARGET)/$(BINARY)" "$(TARGET_BIN)"
	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"

# Upstream's uninstall, restored along with the install it undoes. It leaves
# TILING_EXCEPTIONS_CONF behind, which is upstream's omission rather than a
# decision of this fork's, and is left alone so the two files stay diffable.
uninstall:
	rm "$(TARGET_BIN)" "$(KEYBINDINGS_CONF)"

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