#!/usr/bin/make -f

export DH_VERBOSE=1

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

# We want default Java for this version and nothing else
export JAVA_HOME=/usr/lib/jvm/default-java

# Mimic dh_numpy behavior to generate numpy dependencies
ifeq ($(shell test -f /usr/share/numpy3/versions && echo yes),yes)
    NUMPY3_ABI := $(shell grep '^abi' /usr/share/numpy3/versions | cut -d' ' -f2)
    NUMPY3_API_MIN_VERSION := $(shell grep '^api-min-version' /usr/share/numpy3/versions | cut -d' ' -f2)
else
    NUMPY3_ABI := $(shell grep '^abi' /usr/share/numpy/versions | cut -d' ' -f2)
    NUMPY3_API_MIN_VERSION := $(shell grep '^api-min-version' /usr/share/numpy/versions | cut -d' ' -f2)
endif

# Compute TensorFlow Breaks dependencies
TB_VERSION := $(shell dpkg-parsechangelog --count 1  --show-field Version | rev | cut -d'-' -f2- | rev)
TF_MIN_VERSION = $(shell python3 -c 'split=[int(x) for x in "$(TB_VERSION)".split(".")]; print("%d.%d.%d~" % (split[0], split[1], 0))')
TF_MAX_VERSION = $(shell python3 -c 'split=[int(x) for x in "$(TB_VERSION)".split(".")]; print("%d.%d.%d~" % (split[0], split[1]+1, 0))')
SUBSTVARS_PY3BREAKS := -Vpython3custom:Breaks="python3-tensorflow (>= $(TF_MAX_VERSION)), python3-tensorflow (<< $(TF_MIN_VERSION))"

# See tensorboard/pip_package/setup.py
# Wheel is not needed, see debian/patches/Generate_classic_files_instead_of_WHL
SUBSTVARS_PY3DEPS := -Vpython3custom:Depends="python3-numpy (>= $(NUMPY3_API_MIN_VERSION)), python3-numpy-abi$(NUMPY3_ABI),
SUBSTVARS_PY3DEPS += python3-absl (>= 0.4~),
SUBSTVARS_PY3DEPS += python3-six (>= 1.10.0~),
SUBSTVARS_PY3DEPS += python3-markdown (>= 2.6.8~),
SUBSTVARS_PY3DEPS += python3-protobuf (>= 3.6.0~),
SUBSTVARS_PY3DEPS += python3-werkzeug (>= 0.11.15~),
SUBSTVARS_PY3DEPS += python3-grpcio (>= 1.6.3~)"

# 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)
PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_VERSION).$(DIST_RELEASE)

# Bazel need a home OUTSIDE source folder
export BAZEL_HOME=/tmp/bazel_home_tensorboard_$(PKG_VERSION)

%:
	dh $@ --with python3


override_dh_auto_build:
	HOME="$(BAZEL_HOME)/cache" \
	  bazel build tensorboard:tensorboard \
	    --sandbox_debug \
	    --python_path /usr/bin/python3 \
	    --verbose_failures
	HOME="$(BAZEL_HOME)/cache" \
	  bazel build //tensorboard/pip_package:build_pip_package \
	    --sandbox_debug \
	    --python_path /usr/bin/python3 \
	    --verbose_failures

override_dh_auto_install:
	# See debian/patches/Generate_classic_files_instead_of_WHL
	HOME="$(BAZEL_HOME)/cache" \
	PYTHON_BIN_PATH="/usr/bin/python3" \
	  $(CURDIR)/bazel-bin/tensorboard/pip_package/build_pip_package \
	  $(CURDIR)/debian/python3-tensorboard
	# Remove useless "external" folder
	rm -rf $(CURDIR)/debian/python3-tensorboard/usr/lib/python3/dist-packages/external/
	# Rename binaries for py2/py3 version (py3 will get unversionned link)
	mv $(CURDIR)/debian/python3-tensorboard/usr/bin/tensorboard $(CURDIR)/debian/python3-tensorboard/usr/bin/tensorboard-py3
	ln -sf /usr/bin/tensorboard-py3 $(CURDIR)/debian/python3-tensorboard/usr/bin/tensorboard

# Python deps added by hands (see top of file)
override_dh_python3:
	dh_python3 --no-guessing-deps

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# Most of the test are going to fail, because tensorboard depends on tensorflow for test which depends on tensorboard for installation
	# What a mess
	-HOME="$(BAZEL_HOME)/cache" \
	  bazel test //tensorboard/... \
	    --sandbox_debug \
	    --python_path /usr/bin/python3 \
	    --test_verbose_timeout_warnings
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -f bazel-*
	rm -rf $(BAZEL_HOME)/
	rm -rf $(CURDIR)/bin-hack/

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