PREFIX ?= $(HOME)/.config/systemd/user
BIN    ?= $(HOME)/eww-icons/bin/update-eww.sh
ICONDIR = $(HOME)/eww-icons/icons

.PHONY: all install enable update uninstall icons dev

all: install

install: icons
	@echo ">>> Installiere systemd Units..."
	mkdir -p $(PREFIX)
	systemctl --user link $(HOME)/eww-icons/systemd/*.service
	systemctl --user link $(HOME)/eww-icons/systemd/*.timer
	systemctl --user link $(HOME)/eww-icons/systemd/*.path
	systemctl --user daemon-reload
	$(MAKE) enable

enable:
	@echo ">>> Aktiviere Timer & Watcher..."
	systemctl --user enable --now eww-update.timer remmina-watch.path

update:
	@echo ">>> Update eww-Konfig..."
	$(BIN)

uninstall:
	@echo ">>> Deaktiviere Dienste..."
	systemctl --user disable --now eww-update.timer remmina-watch.path || true
	@echo ">>> Entferne Links..."
	for unit in $(HOME)/eww-icons/systemd/*; do \
		systemctl --user disable --now $$unit || true; \
		rm -f $(PREFIX)/$$(basename $$unit); \
	done
	systemctl --user daemon-reload

icons:
	@echo ">>> Erstelle Dummy-Icons (falls nicht vorhanden)..."
	mkdir -p $(ICONDIR)
	if [ ! -f $(ICONDIR)/remmina.png ]; then \
		convert -size 64x64 xc:lightblue -gravity center -pointsize 20 -annotate 0 "R" $(ICONDIR)/remmina.png; \
	fi
	if [ ! -f $(ICONDIR)/help.png ]; then \
		convert -size 64x64 xc:lightgreen -gravity center -pointsize 20 -annotate 0 "S" $(ICONDIR)/help.png; \
	fi
	if [ ! -f $(ICONDIR)/reboot.png ]; then \
		convert -size 64x64 xc:tomato -gravity center -pointsize 20 -annotate 0 "⟳" $(ICONDIR)/reboot.png; \
	fi

dev:
	@echo ">>> Starte eww-Dock für Tests..."
	eww close desktop-icons || true
	eww open desktop-icons
