VALAC = valac
VALAFLAGS = --pkg gtk+-3.0 --pkg json-glib-1.0
TARGET = mu-calendar
SRC = mu-calendar.vala

VERSION = 1.0.1
DIST_NAME = $(TARGET)-$(VERSION)

# Determine the real user's home directory even when run via sudo
REAL_USER ?= $(SUDO_USER)
ifeq ($(REAL_USER),)
    REAL_USER := $(USER)
endif
REAL_HOME := $(shell getent passwd $(REAL_USER) | cut -d: -f6)
ifeq ($(REAL_HOME),)
    REAL_HOME := $(HOME)
endif

all: $(TARGET)

$(TARGET): $(SRC)
	$(VALAC) $(VALAFLAGS) -X -DVERSION=\"$(VERSION)\" $(SRC) -o $(TARGET)

clean:
	rm -f $(TARGET) *.deb *.tar.gz mu-calendar.desktop
	rm -rf deb_stage $(DIST_NAME)

mu-calendar.desktop:
	@echo "[Desktop Entry]" > mu-calendar.desktop
	@echo "Type=Application" >> mu-calendar.desktop
	@echo "Name=Mu Calendar" >> mu-calendar.desktop
	@echo "Comment=Lightweight GTK3 Calendar and Notes App" >> mu-calendar.desktop
	@echo "Exec=/usr/local/bin/mu-calendar" >> mu-calendar.desktop
	@echo "Terminal=false" >> mu-calendar.desktop
	@echo "Categories=Office;Calendar;" >> mu-calendar.desktop

install: $(TARGET) mu-calendar.desktop
	@echo "Installing binary and desktop file..."
	install -d $(DESTDIR)/usr/local/bin
	install -m 755 $(TARGET) $(DESTDIR)/usr/local/bin/
	
	install -d $(DESTDIR)/usr/local/share/applications
	install -m 644 mu-calendar.desktop $(DESTDIR)/usr/local/share/applications/
	@if command -v update-desktop-database >/dev/null 2>&1; then \
		update-desktop-database -q; \
	fi

	@echo "Installing configuration directory setup for user $(REAL_USER)..."
	install -d -o $(REAL_USER) -g $(REAL_USER) $(REAL_HOME)/.config/mu-calendar
	@echo "Installation complete."

source: mu-calendar.desktop
	@echo "Creating source distribution archive..."
	mkdir -p $(DIST_NAME)
	cp $(SRC) Makefile README.md mu-calendar.desktop $(DIST_NAME)/ 2>/dev/null || true
	tar -czf $(DIST_NAME).tar.gz $(DIST_NAME)
	rm -rf $(DIST_NAME)
	@echo "Source package created: $(DIST_NAME).tar.gz"

deb: $(TARGET) mu-calendar.desktop
	@echo "Building Debian package structure..."
	mkdir -p deb_stage/usr/local/bin
	mkdir -p deb_stage/usr/local/share/applications
	mkdir -p deb_stage/DEBIAN

	cp $(TARGET) deb_stage/usr/local/bin/
	cp mu-calendar.desktop deb_stage/usr/local/share/applications/

	@echo '#!/bin/bash' > deb_stage/DEBIAN/postinst
	@echo 'TARGET_USER=$${SUDO_USER:-$$USER}' >> deb_stage/DEBIAN/postinst
	@echo 'if [ "$$TARGET_USER" = "root" ] && [ -n "$$PKEXEC_UID" ]; then TARGET_USER=$$(getent passwd "$$PKEXEC_UID" | cut -d: -f1); fi' >> deb_stage/DEBIAN/postinst
	@echo 'TARGET_HOME=$$(getent passwd "$$TARGET_USER" | cut -d: -f6)' >> deb_stage/DEBIAN/postinst
	@echo '[ -z "$$TARGET_HOME" ] && TARGET_HOME=$$HOME' >> deb_stage/DEBIAN/postinst
	@echo 'mkdir -p "$$TARGET_HOME/.config/mu-calendar"' >> deb_stage/DEBIAN/postinst
	@echo 'chown -R "$$TARGET_USER:$$TARGET_USER" "$$TARGET_HOME/.config/mu-calendar"' >> deb_stage/DEBIAN/postinst
	@echo 'which update-desktop-database >/dev/null 2>&1 && update-desktop-database -q || true' >> deb_stage/DEBIAN/postinst
	chmod +x deb_stage/DEBIAN/postinst

	@echo "Package: mu-calendar\nVersion: $(VERSION)\nSection: utils\nPriority: optional\nArchitecture: amd64\nMaintainer: Mark Ulrich <markulrich@gmx.de>\nDepends: libgtk-3-0, libjson-glib-1.0\nDescription: Lightweight GTK3 calendar and note-taking application." > deb_stage/DEBIAN/control

	dpkg-deb --build deb_stage $(TARGET)_$(VERSION)_amd64.deb
	rm -rf deb_stage
	@echo "Package built successfully: $(TARGET)_$(VERSION)_amd64.deb"

.PHONY: all clean install source deb