#!/usr/bin/make -f

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | grep -E '^Version:' | cut -d' ' -f2)
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
UPST_VERSION := $(shell echo $(PKG_VERSION) | cut -d '-' -f 1)
BUILD_ARCH   := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_VERSION).$(DIST_RELEASE)

# Bazel wants to create stuff in $HOME
# Workaround so it can run in pbuilder
ifeq ($(HOME),/nonexistent)
export HOME=$(CURDIR)
endif

# Force compilation with default java JDK
export JAVA_HOME=/usr/lib/jvm/default-java

export VERBOSE=yes

%:
	dh $@

override_dh_auto_build:
	# I need debug symbols in the crappy CPP launcher
	sed -i 's!"\(-Wno-sign-compare\)"!"\1 $(CXXFLAGS)"!' "$(CURDIR)/src/main/cpp/BUILD"
	./compile.sh compile

#override_dh_auto_test:
#	./compile.sh determinism
#	./compile.sh tests

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)"

override_dh_auto_clean:
	sed -i 's!"\(.*\) $(CXXFLAGS)"!"\1"!' "$(CURDIR)/src/main/cpp/BUILD"
	rm -f bazel-bazel-$(UPST_VERSION) \
	      bazel-bin \
	      bazel-genfiles \
	      bazel-out \
	      bazel-testlogs \
	      output/bazel

# https://github.com/bazelbuild/bazel/issues/600
override_dh_strip:
