#!/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 | egrep '^Version:' | cut -f 2 -d ' ')
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)

# We need to make sure bazel find the right java path
# Otherwise the resulting binary will be asking for oracle java 8
PATH_W_JVM := /usr/lib/jvm/java-8-openjdk-$(BUILD_ARCH)/bin:$(PATH)
export VERBOSE=yes
export PATH=$(PATH_W_JVM)
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-$(BUILD_ARCH)

%:
	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)+$(DIST_NAME)-$(DIST_RELEASE)-$(DIST_VERSION)"

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:
