#!/usr/bin/make -f

export DH_VERBOSE = 1

# Get git commit date for version
GIT_DATE := $(shell date +%Y%m%d)
GIT_COMMIT := HEAD

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

# Launchpad has no network access, so use local toolchain only
export GOTOOLCHAIN := local

%:
	dh $@

override_dh_installsystemd:
	dh_installsystemd --name=dms

override_dh_auto_build:
	# Create Go cache directories (sbuild sets HOME to non-existent path)
	mkdir -p $(HOME) $(GOCACHE) $(GOMODCACHE)

	# Git source is already included in source package (cloned by ppa-build.sh)
	# Launchpad build environment has no internet access
	test -d dms-git-repo || (echo "ERROR: dms-git-repo directory not found!" && exit 1)

	# Patch go.mod for Launchpad: align go directive w/latest Go toolchain
	GO_VERSION=$$(go env GOVERSION | sed -E 's/^go([0-9]+\.[0-9]+).*/\1/'); \
	if [ -n "$$GO_VERSION" ]; then \
		sed -E -i "s/^go 1\.[0-9]+(\.[0-9]+)?/go $$GO_VERSION/" dms-git-repo/core/go.mod; \
		if [ -f dms-git-repo/core/vendor/modules.txt ]; then \
			sed -E -i "s/^(## explicit; go )1\.[0-9]+(\.[0-9]+)?$$/\1$$GO_VERSION/" dms-git-repo/core/vendor/modules.txt; \
		fi; \
	else \
		echo "Warning: Could not detect Go version, leaving go.mod go directive unchanged"; \
	fi
	sed -E -i '/^toolchain go[0-9.]+$$/d' dms-git-repo/core/go.mod

	# Build dms-cli from source
	# Detect architecture
	$(eval DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH))
	@if [ -f dms-git-repo/.dms-version ]; then \
		. dms-git-repo/.dms-version; \
		echo "Building with VERSION=$$VERSION COMMIT=$$COMMIT ARCH=$(DEB_HOST_ARCH)"; \
		cd dms-git-repo/core && $(MAKE) GOFLAGS="-mod=vendor" dist ARCH=$(DEB_HOST_ARCH) VERSION="$$VERSION" COMMIT="$$COMMIT"; \
	else \
		echo "Warning: .dms-version not found, building without version info"; \
		cd dms-git-repo/core && $(MAKE) GOFLAGS="-mod=vendor" dist ARCH=$(DEB_HOST_ARCH); \
	fi
	cp dms-git-repo/core/bin/dms-linux-$(DEB_HOST_ARCH) dms
	chmod +x dms

override_dh_auto_install:
	# Install binary
	install -Dm755 dms debian/dms-git/usr/bin/dms

	# Install QML files from git clone
	mkdir -p debian/dms-git/usr/share/quickshell/dms
	cp -r dms-git-repo/* debian/dms-git/usr/share/quickshell/dms/

	# Remove unnecessary directories
	rm -rf debian/dms-git/usr/share/quickshell/dms/core
	rm -rf debian/dms-git/usr/share/quickshell/dms/distro

	# Install systemd user service
	install -Dm644 dms-git-repo/assets/systemd/dms.service \
		debian/dms-git/usr/lib/systemd/user/dms.service

	# Install desktop file and icon
	install -Dm644 dms-git-repo/assets/dms-open.desktop \
		debian/dms-git/usr/share/applications/dms-open.desktop
	install -Dm644 dms-git-repo/assets/danklogo.svg \
		debian/dms-git/usr/share/icons/hicolor/scalable/apps/danklogo.svg

	# Create DMS Version file
	@if [ -f dms-git-repo/.dms-version ]; then \
		. dms-git-repo/.dms-version; \
		echo "$$VERSION" > debian/dms-git/usr/share/quickshell/dms/VERSION; \
	else \
		echo "dev" > debian/dms-git/usr/share/quickshell/dms/VERSION; \
	fi

override_dh_auto_clean:
	# Don't delete dms-git-repo directory - it's part of the source package (native format)
	# Clean up build artifacts
	rm -f dms
	rm -rf dms-git-repo/core/bin
	rm -rf debian/tmp-home
	dh_auto_clean
