XDG_CONFIG_HOME ?= $(HOME)/.config
CONFIG_DIR := $(XDG_CONFIG_HOME)/tinted-theming/tinty
BIN_DIR := $(HOME)/.local/bin
HERDR_DIR := $(HOME)/.config/herdr
HERDR_CONFIG := $(HERDR_DIR)/config.toml
HERDR_MARKER := $(HERDR_DIR)/.tint-created-config
LAUNCH_AGENT_DIR := $(HOME)/Library/LaunchAgents
LAUNCH_AGENT_LABEL := dev.tinty.dark-notify
LAUNCH_AGENT := $(LAUNCH_AGENT_DIR)/$(LAUNCH_AGENT_LABEL).plist
DARK_NOTIFY_LOG := $(HOME)/Library/Logs/dark-notify.log
CONFIG_MARKER := $(CONFIG_DIR)/.tint-installed-config
STATE_MARKER := $(CONFIG_DIR)/.tint-installed-state

.PHONY: install uninstall
install: default-state.toml config.toml
	@command -v tinty >/dev/null || { echo "tinty is required" >&2; exit 1; }
	@command -v dark-notify >/dev/null || { echo "dark-notify is required" >&2; exit 1; }
	mkdir -p "$(BIN_DIR)" "$(CONFIG_DIR)" "$(HERDR_DIR)" "$(LAUNCH_AGENT_DIR)" "$(dir $(DARK_NOTIFY_LOG))"
	go build -o "$(BIN_DIR)/tint" .
	@set -e; if [ "$(CURDIR)" != "$(CONFIG_DIR)" ]; then \
		if [ -e "$(CONFIG_MARKER)" ] || [ ! -e "$(CONFIG_DIR)/config.toml" ]; then \
			cp config.toml "$(CONFIG_DIR)/config.toml"; \
			touch "$(CONFIG_MARKER)"; \
		fi; \
	fi; \
	if [ ! -e "$(CONFIG_DIR)/state.toml" ]; then \
		cp default-state.toml "$(CONFIG_DIR)/state.toml"; \
		touch "$(STATE_MARKER)"; \
	fi
	@if [ ! -e "$(HERDR_CONFIG)" ]; then \
		touch "$(HERDR_CONFIG)" "$(HERDR_MARKER)"; \
	fi
	XDG_CONFIG_HOME="$(XDG_CONFIG_HOME)" tinty sync
	@dark_notify="$$(command -v dark-notify)"; \
	tinty_dir="$$(dirname "$$(command -v tinty)")"; \
	printf '%s\n' \
		'<?xml version="1.0" encoding="UTF-8"?>' \
		'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' \
		'<plist version="1.0">' \
		'<dict>' \
		'  <key>Label</key>' \
		'  <string>$(LAUNCH_AGENT_LABEL)</string>' \
		'  <key>ProgramArguments</key>' \
		'  <array>' \
		"    <string>$$dark_notify</string>" \
		'    <string>-c</string>' \
		'    <string>$(BIN_DIR)/tint</string>' \
		'  </array>' \
		'  <key>EnvironmentVariables</key>' \
		'  <dict>' \
		'    <key>HOME</key>' \
		'    <string>$(HOME)</string>' \
		'    <key>PATH</key>' \
		"    <string>$$tinty_dir:/usr/bin:/bin:/usr/sbin:/sbin</string>" \
		'    <key>XDG_CONFIG_HOME</key>' \
		'    <string>$(XDG_CONFIG_HOME)</string>' \
		'  </dict>' \
		'  <key>RunAtLoad</key>' \
		'  <true/>' \
		'  <key>KeepAlive</key>' \
		'  <true/>' \
		'  <key>StandardOutPath</key>' \
		'  <string>$(DARK_NOTIFY_LOG)</string>' \
		'  <key>StandardErrorPath</key>' \
		'  <string>$(DARK_NOTIFY_LOG)</string>' \
		'</dict>' \
		'</plist>' > "$(LAUNCH_AGENT)"
	plutil -lint "$(LAUNCH_AGENT)"
	@launchctl bootout "gui/$$(id -u)/$(LAUNCH_AGENT_LABEL)" 2>/dev/null || true
	launchctl bootstrap "gui/$$(id -u)" "$(LAUNCH_AGENT)"

uninstall:
	@launchctl bootout "gui/$$(id -u)/$(LAUNCH_AGENT_LABEL)" 2>/dev/null || true
	rm -f "$(BIN_DIR)/tint" "$(LAUNCH_AGENT)" "$(DARK_NOTIFY_LOG)"
	@if [ -e "$(STATE_MARKER)" ]; then \
		rm -f "$(CONFIG_DIR)/state.toml" "$(STATE_MARKER)"; \
	fi
	@if [ -e "$(CONFIG_MARKER)" ]; then \
		rm -f "$(CONFIG_DIR)/config.toml" "$(CONFIG_MARKER)"; \
	fi
	@if [ -e "$(HERDR_MARKER)" ]; then \
		rm -f "$(HERDR_CONFIG)" "$(HERDR_MARKER)"; \
	fi
	@rmdir "$(CONFIG_DIR)" "$(dir $(CONFIG_DIR))" "$(HERDR_DIR)" "$(BIN_DIR)" "$(LAUNCH_AGENT_DIR)" "$(dir $(DARK_NOTIFY_LOG))" "$(XDG_CONFIG_HOME)" "$(HOME)/.config" "$(HOME)/.local" 2>/dev/null || true
