#!/usr/bin/make -f

export DH_VERBOSE=1

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

# Extract version from debian/changelog and convert to upstream format
# Debian version: 1.13.0.beta.1.reF1nd-1 -> Upstream: 1.13.0-beta.1-reF1nd
DEB_VERSION := $(shell dpkg-parsechangelog -SVersion | sed 's/-[0-9]*$$//')
# Convert dots back to hyphens for alpha/beta/rc/reF1nd parts: 1.13.0.beta.1.reF1nd -> 1.13.0-beta.1-reF1nd
UPSTREAM_VERSION := $(shell echo '$(DEB_VERSION)' | sed -E 's/\.(alpha|beta|rc)\./-\1./g; s/\.reF1nd/-reF1nd/')
SOFTWARE_NAME := sing-box

# 架构映射：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

ifneq ($(DEB_HOST_ARCH),riscv64)
    export CGO_LDFLAGS=-fuse-ld=mold
endif

%:
	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
	
	@test -d vendor || (echo "ERROR: vendor/ directory still not found" && exit 1)

	_base_tags="with_gvisor,with_quic,with_wireguard,with_utls,with_tailscale,with_clash_api,with_acme,with_dhcp"; \
	if [ "$(DEB_HOST_ARCH)" != "riscv64" ]; then \
	    _tags="$$_base_tags,with_naive_outbound"; \
	else \
	    _tags="$$_base_tags"; \
	fi; \
	go build \
	    -v -trimpath -buildmode=pie \
	    -mod=vendor -modcacherw -buildvcs=false \
	    -tags "$$_tags" \
	    -ldflags "-X github.com/sagernet/sing-box/constant.Version=$(UPSTREAM_VERSION) -s -w -buildid= -checklinkname=0" \
	    ./cmd/sing-box
	
	echo "u sing-box - \"Sing-box Service\" - -" > release/config/sing-box.sysusers
	
	mkdir -p completions
	./sing-box completion bash > completions/bash
	./sing-box completion fish > completions/fish
	./sing-box completion zsh > completions/zsh
	
	printf '// Allow sing-box to set domain and default-route\npolkit.addRule(function(action, subject) {\n    if ((action.id == "org.freedesktop.resolve1.set-domains" ||\n         action.id == "org.freedesktop.resolve1.set-default-route" ||\n         action.id == "org.freedesktop.resolve1.set-dns-servers") &&\n         subject.user == "sing-box") {\n        return polkit.Result.YES;\n    }\n});' > release/config/sing-box.rules

override_dh_auto_install:
	install -Dm755 sing-box $(CURDIR)/debian/sing-box-ref1nd/usr/bin/sing-box
	install -Dm644 release/config/config.json $(CURDIR)/debian/sing-box-ref1nd/etc/sing-box/config.json
	install -Dm644 release/config/sing-box.service $(CURDIR)/debian/sing-box-ref1nd/lib/systemd/system/sing-box.service
	install -Dm644 release/config/sing-box@.service $(CURDIR)/debian/sing-box-ref1nd/lib/systemd/system/sing-box@.service
	install -Dm644 release/config/sing-box.sysusers $(CURDIR)/debian/sing-box-ref1nd/usr/lib/sysusers.d/sing-box.conf
	install -Dm644 release/config/sing-box.rules $(CURDIR)/debian/sing-box-ref1nd/usr/share/polkit-1/rules.d/sing-box.rules
	install -dm755 $(CURDIR)/debian/sing-box-ref1nd/usr/share/sing-box
	install -Dm644 LICENSE $(CURDIR)/debian/sing-box-ref1nd/usr/share/doc/sing-box-ref1nd/copyright
	
	install -Dm644 completions/bash $(CURDIR)/debian/sing-box-ref1nd-bash-completion/usr/share/bash-completion/completions/sing-box
	install -Dm644 completions/zsh $(CURDIR)/debian/sing-box-ref1nd-zsh-completion/usr/share/zsh/vendor-completions/_sing-box
	install -Dm644 completions/fish $(CURDIR)/debian/sing-box-ref1nd-fish-completion/usr/share/fish/vendor_completions.d/sing-box.fish

override_dh_auto_test:

override_dh_strip:


