UUID := coredesktop@federated.computer
ZIP := $(UUID).shell-extension.zip

.PHONY: all pack install enable disable lint release clean

all: pack

# Builds $(ZIP). The settings schema is compiled when it's installed.
pack:
	gnome-extensions pack --force \
		--extra-source=lib --extra-source=bin --extra-source=icons \
		--schema=schemas/org.gnome.shell.extensions.coredesktop.gschema.xml \
		--out-dir=. $(UUID)

# On Wayland, GNOME Shell finds a newly installed extension only at the next
# login, so install also enables it: it loads when you log back in
install: pack
	gnome-extensions install --force $(ZIP)
	@$(MAKE) --no-print-directory enable
	@echo "Installed. Log out and back in to load it."

# gnome-extensions enable only knows extensions the running shell has found.
# Until the next login, add the extension to enabled-extensions directly,
# keeping the others in the list.
enable:
	@gnome-extensions enable $(UUID) 2> /dev/null || \
	python3 -c 'import ast, subprocess, sys; \
	uuid = sys.argv[1]; \
	key = ["org.gnome.shell", "enabled-extensions"]; \
	out = subprocess.run(["gsettings", "get", *key], capture_output=True, text=True, check=True).stdout.strip(); \
	ids = ast.literal_eval(out.removeprefix("@as ")); \
	ids.append(uuid) if uuid not in ids else None; \
	subprocess.run(["gsettings", "set", *key, str(ids)], check=True)' $(UUID)
	@echo "Enabled $(UUID). Now: $$(gsettings get org.gnome.shell enabled-extensions)"

disable:
	gnome-extensions disable $(UUID)

# Syntax-check the modules, the wrapper script and the settings schema
lint:
	for f in $(UUID)/*.js $(UUID)/lib/*.js; do node --check --input-type=module < $$f || exit 1; done
	bash -n $(UUID)/bin/run-tracked
	glib-compile-schemas --strict --dry-run $(UUID)/schemas
	bash -n build-aux/arch/release.sh

# Build the pacman package from main on Gitea and upload it to the [federated]
# repo, so CoreDesktop's update installs it. Needs GITEA_TOKEN.
release:
	bash build-aux/arch/release.sh

clean:
	rm -f $(ZIP) $(UUID)/schemas/gschemas.compiled
