#!/usr/bin/make -f

export DH_GOLANG_INSTALL_EXTRA := \
	common/buf/data \
	release/config/geoip.dat \
	release/config/geosite.dat

include /usr/share/dpkg/default.mk

# Build parameters are from upstream script:
# - release/user-package.sh
BUILDNAME = $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" +%Y%m%d-%H%M%S)
LDFLAGS = -s -w -buildid= \
	-X v2ray.com/core.codename=user \
	-X v2ray.com/core.build=$(BUILDNAME) \
	-X v2ray.com/core.version=$(shell echo $(DEB_VERSION_UPSTREAM)|sed -e 's/+[^+]*$$//')

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

# Regenerate protobuf (*.pb.go) files from *.proto
override_dh_auto_configure:
	dh_auto_configure
	find obj-$(DEB_HOST_GNU_TYPE)/src/v2ray.com/core -type f -name '*.pb.go' \
		-delete -printf 'Remove %p\n'
	cd obj-$(DEB_HOST_GNU_TYPE)/src; \
	find v2ray.com/core -name '*.proto' -printf 'Regenerate %p\n' \
		-exec protoc -I./v2ray.com/core --go_out=plugins=grpc:. {} \;

override_dh_auto_build:
	DH_GOPKG="v2ray.com/core/main" dh_auto_build -- -ldflags "$(LDFLAGS)"
	cd obj-$(DEB_HOST_GNU_TYPE); mv bin/main bin/v2ray
	DH_GOPKG="v2ray.com/core/infra/control/main" dh_auto_build -- -ldflags "$(LDFLAGS)" -tags confonly
	cd obj-$(DEB_HOST_GNU_TYPE); mv bin/main bin/v2ctl

override_dh_install:
	dh_install -X.pem

ifeq ($(filter nocheck, $(DEB_BUILD_OPTIONS)),)
override_dh_auto_test:
ifneq ($(filter $(DEB_TARGET_ARCH), mips64el mipsel riscv64),)
# Ignore failure on some slow systems
	-dh_auto_test -- -timeout 30m
else
	dh_auto_test -- -timeout 30m
endif
endif
