#!/usr/bin/make -f

export DH_VERBOSE = 1

# Version info from changelog
DEB_VERSION := $(shell dpkg-parsechangelog -S Version)
UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed 's/-[^-]*$$//')
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Go needs writable directories for cache
export HOME := $(CURDIR)/debian/tmp-home
export GOCACHE := $(CURDIR)/debian/tmp-home/go-cache
export GOMODCACHE := $(CURDIR)/debian/tmp-home/go-mod

export GOTOOLCHAIN := local

%:
	dh $@

override_dh_installsystemd:
	dh_installsystemd --name=dms

override_dh_auto_build:
	# Create Go cache directories
	mkdir -p $(HOME) $(GOCACHE) $(GOMODCACHE)

	# Verify core directory exists (native package format has source at root)
	test -d core || (echo "ERROR: core directory not found!" && exit 1)

	# Bundled Go at repo root (go$$VER.linux-{amd64,arm64}.tar.gz); packaged via obs-upload.sh
	GO_TOOLCHAIN_VERSION=$$(grep -m1 '^go ' core/go.mod | awk '{print $$2}'); \
	case "$(DEB_HOST_ARCH)" in \
	amd64) GO_LINUX_ARCH=amd64 ;; \
	arm64) GO_LINUX_ARCH=arm64 ;; \
	*) echo "ERROR: Unsupported architecture: $(DEB_HOST_ARCH)" && exit 1 ;; \
	esac; \
	GO_TARBALL="$(CURDIR)/go$$GO_TOOLCHAIN_VERSION.linux-$$GO_LINUX_ARCH.tar.gz"; \
	test -f "$$GO_TARBALL" || (echo "ERROR: Missing bundled Go toolchain $$GO_TARBALL" && exit 1); \
	rm -rf "$(CURDIR)/go-bootstrap" "$(CURDIR)/.go-toolchain"; \
	mkdir -p "$(CURDIR)/go-bootstrap"; \
	tar -xzf "$$GO_TARBALL" -C "$(CURDIR)/go-bootstrap"; \
	mv "$(CURDIR)/go-bootstrap/go" "$(CURDIR)/.go-toolchain"; \
	export PATH="$(CURDIR)/.go-toolchain/bin:$$PATH"; \
	export GOROOT="$(CURDIR)/.go-toolchain"; \
	go version; \
	sed -i "s/^go [0-9]\+\.[0-9]\+\(\.[0-9]*\)\?$$/go $$GO_TOOLCHAIN_VERSION/" core/go.mod; \
	sed -i "s/^\(## explicit; go \)[0-9]\+\.[0-9]\+\(\.[0-9]*\)\?$$/\1$$GO_TOOLCHAIN_VERSION/" core/vendor/modules.txt; \
	VERSION="$(UPSTREAM_VERSION)"; \
	COMMIT=$$(echo "$(UPSTREAM_VERSION)" | grep -oP '(?<=git)[0-9]+\.[a-f0-9]+' | cut -d. -f2 | head -c8 || echo "unknown"); \
	if [ "$(DEB_HOST_ARCH)" = "amd64" ]; then \
		MAKE_ARCH=amd64; \
	elif [ "$(DEB_HOST_ARCH)" = "arm64" ]; then \
		MAKE_ARCH=arm64; \
	else \
		echo "ERROR: Unsupported architecture: $(DEB_HOST_ARCH)" && exit 1; \
	fi; \
	echo "Building with VERSION=$$VERSION COMMIT=$$COMMIT ARCH=$$MAKE_ARCH"; \
	cd core && $(MAKE) GOFLAGS="-mod=vendor" dist ARCH=$$MAKE_ARCH VERSION="$$VERSION" COMMIT="$$COMMIT"

	# Copy binary to expected location
	if [ "$(DEB_HOST_ARCH)" = "amd64" ]; then \
		cp core/bin/dms-linux-amd64 dms; \
	elif [ "$(DEB_HOST_ARCH)" = "arm64" ]; then \
		cp core/bin/dms-linux-arm64 dms; \
	fi
	chmod +x dms

override_dh_auto_install:
	install -Dm755 dms debian/dms-git/usr/bin/dms

	mkdir -p debian/dms-git/usr/share/quickshell/dms debian/dms-git/usr/lib/systemd/user
	if [ -d quickshell ]; then \
		cp -r quickshell/* debian/dms-git/usr/share/quickshell/dms/; \
		install -Dm644 assets/systemd/dms.service debian/dms-git/usr/lib/systemd/user/dms.service; \
		install -Dm644 assets/dms-open.desktop debian/dms-git/usr/share/applications/dms-open.desktop; \
		install -Dm644 assets/danklogo.svg debian/dms-git/usr/share/icons/hicolor/scalable/apps/danklogo.svg; \
	else \
		echo "ERROR: quickshell directory not found!" && \
		echo "Contents of current directory:" && ls -la && \
		exit 1; \
	fi

	rm -rf debian/dms-git/usr/share/quickshell/dms/core \
		debian/dms-git/usr/share/quickshell/dms/distro

	echo "$(UPSTREAM_VERSION)" > debian/dms-git/usr/share/quickshell/dms/VERSION

override_dh_auto_clean:
	# Clean up build artifacts
	rm -f dms
	rm -rf core/bin
	rm -rf debian/tmp-home
	rm -rf go-bootstrap .go-toolchain
	dh_auto_clean
