#!/usr/bin/make -f

export DH_VERBOSE=1

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

DEB_VERSION := $(shell dpkg-parsechangelog -SVersion | sed 's/-[^-]*$$//')
UPSTREAM_VERSION := $(DEB_VERSION)
SOFTWARE_NAME := openlist

# 架构映射：Debian arch -> Go arch
ifeq ($(DEB_HOST_ARCH),amd64)
    GO_ARCH := amd64
else ifeq ($(DEB_HOST_ARCH),arm64)
    GO_ARCH := arm64
else ifeq ($(DEB_HOST_ARCH),armhf)
    GO_ARCH := arm
else ifeq ($(DEB_HOST_ARCH),armel)
    GO_ARCH := arm
else ifeq ($(DEB_HOST_ARCH),i386)
    GO_ARCH := 386
else ifeq ($(DEB_HOST_ARCH),ppc64el)
    GO_ARCH := ppc64le
else ifeq ($(DEB_HOST_ARCH),s390x)
    GO_ARCH := s390x
else ifeq ($(DEB_HOST_ARCH),riscv64)
    GO_ARCH := riscv64
else ifeq ($(DEB_HOST_ARCH),loong64)
    GO_ARCH := loong64
else ifeq ($(DEB_HOST_ARCH),mips64el)
    GO_ARCH := mips64le
else ifeq ($(DEB_HOST_ARCH),mipsel)
    GO_ARCH := mipsle
else
    GO_ARCH := $(DEB_HOST_ARCH)
endif

export GOTOOLCHAIN=local
export GOPROXY=off
export GOSUMDB=off
export GOMODCACHE=$(CURDIR)/.gomodcache
# Launchpad sbuild sets HOME to unwritable /sbuild-nonexistent; keep the
# build cache inside the build tree.
export GOCACHE=$(CURDIR)/.gocache
export CGO_ENABLED=1

%:
	dh $@

override_dh_auto_build:
	# vendor/ is unpacked by dpkg-source from the orig-vendor component.
	@test -d vendor -a -f vendor/modules.txt || { \
		echo "ERROR: vendor/ missing; orig-vendor component did not unpack" >&2; \
		exit 1; \
	}
	# Prebuilt frontend bundle: dpkg-source unpacks the orig-frontend
	# component into frontend/ (fetched from the OpenList-Frontend release
	# matching this tag at vendoring time; source URL recorded in the release
	# logs). Copy it into public/dist (go:embed).
	@test -d frontend && [ -n "$$(find frontend -type f | head -1)" ] || { \
		echo "ERROR: frontend/ empty; orig-frontend component did not unpack" >&2; \
		exit 1; \
	}
	mkdir -p public/dist
	cp -a frontend/. public/dist/

	# Build with metadata
	go build \
		-v -trimpath -buildmode=pie \
		-mod=vendor -modcacherw -buildvcs=false \
		-tags "jsoniter" \
		-ldflags "-X 'github.com/OpenListTeam/OpenList/v4/internal/conf.BuiltAt=$$(date +'%F %T %z')' \
		         -X 'github.com/OpenListTeam/OpenList/v4/internal/conf.GitAuthor=The OpenList Projects Contributors <noreply@openlist.team>' \
		         -X 'github.com/OpenListTeam/OpenList/v4/internal/conf.GitCommit=unknown' \
		         -X 'github.com/OpenListTeam/OpenList/v4/internal/conf.Version=$(UPSTREAM_VERSION)' \
		         -X 'github.com/OpenListTeam/OpenList/v4/internal/conf.WebVersion=$(UPSTREAM_VERSION)' \
		         -s -w -buildid=" \
		-o $(SOFTWARE_NAME) .

	@test -f $(SOFTWARE_NAME) || (echo "ERROR: binary not built" && exit 1)
	@echo "==> Binary built successfully: $(SOFTWARE_NAME)"

override_dh_auto_install:
	install -Dm755 $(SOFTWARE_NAME) $(CURDIR)/debian/$(SOFTWARE_NAME)/usr/bin/$(SOFTWARE_NAME)
	install -Dm644 LICENSE $(CURDIR)/debian/$(SOFTWARE_NAME)/usr/share/doc/$(SOFTWARE_NAME)/copyright

	# Systemd service + template unit are auto-installed by dh_installinit
	# from debian/openlist.service / debian/openlist@.service.

	# Sysusers configuration
	install -Dm644 debian/$(SOFTWARE_NAME).sysusers \
		$(CURDIR)/debian/$(SOFTWARE_NAME)/usr/lib/sysusers.d/$(SOFTWARE_NAME).conf

override_dh_auto_test:

override_dh_strip:
