#!/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 tensorflow_estimator/tools/pip_package/setup.py
# Nothing to see anymore in setup.py since 1.14.0
# See tensorflow_estimator/contrib/estimator/BUILD for expected packages and extract corresponding version from TensorFlow ones
SUBSTVARS_PY3DEPS := -Vpython3custom:Depends="python3-numpy (>= $(NUMPY3_API_MIN_VERSION)), python3-numpy-abi$(NUMPY3_ABI),
SUBSTVARS_PY3DEPS += python3-absl (>= 0.7.0~),
SUBSTVARS_PY3DEPS += python3-six (>= 1.10.0~)"


# 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_tensorflow-estimator_$(PKG_VERSION)

# Reduce number of parallel unittests
UNITTEST_PARALLEL := $(shell python3 -c "import math; print(math.ceil(`nproc` / 2))")


%:
	dh $@ --with python3

override_dh_auto_build:
	# Found --python_path information here:
	# https://github.com/tensorflow/estimator/blob/master/WORKSPACE
	HOME="$(BAZEL_HOME)/cache" \
	  bazel build //tensorflow_estimator/tools/pip_package:build_pip_package \
	    --sandbox_debug \
	    --python_path /usr/bin/python3

override_dh_auto_install:
	HOME="$(BAZEL_HOME)/cache" \
	PYTHON_BIN_PATH="/usr/bin/python3" \
	  bazel-bin/tensorflow_estimator/tools/pip_package/build_pip_package \
	    "$(CURDIR)/debian/python3-tensorflow-estimator"

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

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_PROFILES)),)
	HOME="$(BAZEL_HOME)/cache" \
	  bazel test //tensorflow_estimator/... \
	    --sandbox_debug \
	    --python_path /usr/bin/python3 \
	    --test_verbose_timeout_warnings \
	    --jobs $(UNITTEST_PARALLEL)
endif

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

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