#!/usr/bin/make -f

export DH_VERBOSE=1

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

# Extract version from debian/changelog
DEB_VERSION := $(shell dpkg-parsechangelog -SVersion | sed 's/-[0-9]*$$//')
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
export CGO_ENABLED=1

%:
	dh $@

override_dh_auto_build:
	@if [ ! -d vendor ]; then \
		echo "vendor/ not found, attempting to extract from SOURCES..."; \
		if [ -f /usr/src/packages/SOURCES/$(SOFTWARE_NAME)-$(UPSTREAM_VERSION)-vendor.tar.gz ]; then \
			tar -xzf /usr/src/packages/SOURCES/$(SOFTWARE_NAME)-$(UPSTREAM_VERSION)-vendor.tar.gz; \
			echo "vendor/ extracted successfully"; \
		else \
			echo "ERROR: vendor tarball not found in /usr/src/packages/SOURCES/"; \
			exit 1; \
		fi; \
	fi

	# Extract frontend
	@if [ ! -d public/dist ]; then \
		echo "public/dist not found, attempting to extract frontend..."; \
		mkdir -p public/dist; \
		if [ -f /usr/src/packages/SOURCES/$(SOFTWARE_NAME)-frontend-$(UPSTREAM_VERSION).tar.gz ]; then \
			tar -xzf /usr/src/packages/SOURCES/$(SOFTWARE_NAME)-frontend-$(UPSTREAM_VERSION).tar.gz -C public/dist; \
			echo "frontend extracted successfully"; \
		else \
			echo "ERROR: frontend tarball not found in /usr/src/packages/SOURCES/"; \
			exit 1; \
		fi; \
	fi

	# Verify directories exist
	@test -d vendor || (echo "ERROR: vendor/ directory still not found" && exit 1)
	@test -d public/dist || (echo "ERROR: public/dist/ directory still not found" && exit 1)
	@echo "==> Verified: vendor/ and public/dist/ are present"
	@echo "==> Frontend files count: $$(find public/dist -type f | wc -l)"
	@echo "==> Frontend total size: $$(du -sh public/dist | cut -f1)"

	# 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) .

	# Verify binary was built and check size
	@test -f $(SOFTWARE_NAME) || (echo "ERROR: binary not built" && exit 1)
	@echo "==> Binary built successfully: $(SOFTWARE_NAME)"
	@ls -lh $(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
	
	# Install systemd service files
	install -Dm644 /usr/src/packages/SOURCES/$(SOFTWARE_NAME).service \
		$(CURDIR)/debian/$(SOFTWARE_NAME)/lib/systemd/system/$(SOFTWARE_NAME).service
	install -Dm644 /usr/src/packages/SOURCES/$(SOFTWARE_NAME)@.service \
		$(CURDIR)/debian/$(SOFTWARE_NAME)/lib/systemd/system/$(SOFTWARE_NAME)@.service
	
	# Install sysusers configuration
	install -Dm644 /usr/src/packages/SOURCES/$(SOFTWARE_NAME).sysusers \
		$(CURDIR)/debian/$(SOFTWARE_NAME)/usr/lib/sysusers.d/$(SOFTWARE_NAME).conf

override_dh_auto_test:

override_dh_strip:
