#!/usr/bin/make -f

export DH_VERBOSE = 1

DEB_VERSION := $(shell dpkg-parsechangelog -S Version)
UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed 's/^[0-9]*://; s/db[0-9]*$$//')
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

# 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

# OBS builds are offline: dependencies are vendored, the toolchain is bundled
export GOTOOLCHAIN := local
export GOFLAGS := -mod=vendor

%:
	dh $@

# Bundled Go is only on PATH inside override_dh_auto_build; dh_auto_test
# would run `make test` with no go binary (OBS offline). Skip here.
override_dh_auto_test:
	@echo "Skipping dh_auto_test (bundled Go not on PATH; tests run in CI)"

override_dh_auto_build:
	mkdir -p $(HOME) $(GOCACHE) $(GOMODCACHE)

	test -d core || (echo "ERROR: core directory not found!" && exit 1)
	test -d core/vendor || (echo "ERROR: vendored Go dependencies missing!" && exit 1)
	test -e quickshell/DCommon/Widgets/DIcon.qml || (echo "ERROR: DCommon missing!" && exit 1)

	# Bundled Go at source root (go$$VER.linux-{amd64,arm64}.tar.gz); staged by obs-upload.sh
	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=$$(ls "$(CURDIR)"/go[0-9]*.linux-$$GO_LINUX_ARCH.tar.gz 2>/dev/null | head -n1); \
	test -f "$$GO_TARBALL" || (echo "ERROR: Missing bundled Go toolchain for $(DEB_HOST_ARCH)" && 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; \
	GO_TOOLCHAIN_VERSION=$$(go env GOVERSION | sed "s/^go//"); \
	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; \
	COMMIT=$$(echo "$(UPSTREAM_VERSION)" | grep -oP '(?<=git)[0-9]+\.[a-f0-9]+' | cut -d. -f2 | head -c8 || echo "unknown"); \
	$(MAKE) -C core build VERSION="$(UPSTREAM_VERSION)" COMMIT="$$COMMIT"

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

	install -d debian/dms-greeter-git/usr/share/bash-completion/completions
	install -d debian/dms-greeter-git/usr/share/zsh/vendor-completions
	install -d debian/dms-greeter-git/usr/share/fish/vendor_completions.d
	core/bin/dms-greeter completion bash > debian/dms-greeter-git/usr/share/bash-completion/completions/dms-greeter
	core/bin/dms-greeter completion zsh > debian/dms-greeter-git/usr/share/zsh/vendor-completions/_dms-greeter
	core/bin/dms-greeter completion fish > debian/dms-greeter-git/usr/share/fish/vendor_completions.d/dms-greeter.fish

	install -Dpm0644 assets/systemd/sysusers-dms-greeter.conf \
		debian/dms-greeter-git/usr/lib/sysusers.d/dms-greeter.conf
	install -Dpm0644 assets/systemd/tmpfiles-dms-greeter.conf \
		debian/dms-greeter-git/usr/lib/tmpfiles.d/dms-greeter.conf

	install -Dm644 README.md debian/dms-greeter-git/usr/share/doc/dms-greeter/README.md
	install -Dm644 LICENSE debian/dms-greeter-git/usr/share/doc/dms-greeter/LICENSE
	install -d debian/dms-greeter-git/usr/share/doc/dms-greeter/examples
	install -m644 assets/examples/* debian/dms-greeter-git/usr/share/doc/dms-greeter/examples/

override_dh_auto_clean:
	rm -rf core/bin core/internal/shellembed/dist
	rm -rf debian/tmp-home go-bootstrap .go-toolchain
	dh_auto_clean
