#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

ifeq ($(shell test -d /usr/lib/go-1.23/bin && echo yes),yes)
    export PATH := /usr/lib/go-1.23/bin:$(PATH)
else ifeq ($(shell test -d /usr/lib/go-1.20/bin && echo yes),yes)
    export PATH := /usr/lib/go-1.20/bin:$(PATH)
endif
export DH_GOLANG_INSTALL_EXTRA := \
	component/ebpf/redir/bpf_bpfeb.o \
	component/ebpf/redir/bpf_bpfel.o \
	component/ebpf/tc/bpf_bpfeb.o \
	component/ebpf/tc/bpf_bpfel.o \
	vendor/golang.org/x/net/publicsuffix/data/text \
	vendor/golang.org/x/net/publicsuffix/data/nodes \
	vendor/golang.org/x/net/publicsuffix/data/children

BUILDTIME=$(shell date -u -d "@$(SOURCE_DATE_EPOCH)" +%Y%m%d-%H%M%S)
LDFLAGS = -s -w -buildid= \
	-X "github.com/metacubex/mihomo/constant.Version=v$(DEB_VERSION_UPSTREAM)" \
	-X "github.com/metacubex/mihomo/constant.BuildTime=$(BUILDTIME)"

%:
	dh $@ --buildsystem=golang --with=golang

override_dh_auto_configure:
	if [ -f /usr/src/packages/SOURCES/vendor.tar.zst ]; then \
		tar -xf /usr/src/packages/SOURCES/vendor.tar.zst; \
	fi
	dh_auto_configure -O--buildsystem=golang

override_dh_auto_build:
ifneq ($(filter $(DEB_TARGET_ARCH), amd64),)
	GO111MODULE=on GOAMD64=v2 dh_auto_build -O--buildsystem=golang -- -tags with_gvisor -trimpath -ldflags "$(LDFLAGS)"
else
	GO111MODULE=on dh_auto_build -O--buildsystem=golang -- -tags with_gvisor -trimpath -ldflags "$(LDFLAGS)"
endif
	cd obj-$(DEB_TARGET_GNU_TYPE); mv bin/mihomo bin/clash-meta

override_dh_auto_install:
	dh_auto_install -O--buildsystem=golang -- --no-source

override_dh_golang:
	echo "misc:Static-Built-Using=$(shell dpkg-query -f='$${source:Package} (= $${source:Version})\n' -W $(shell dpkg-query --search $(shell which go) | cut -d: -f1))" >> debian/clash-meta.substvars
	echo "misc:Built-Using=$(shell dpkg-query -f='$${source:Package} (= $${source:Version})\n' -W $(shell dpkg-query --search $(shell which go) | cut -d: -f1))" >> debian/clash-meta.substvars

override_dh_auto_test:
