#!/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
# Bazel need a home OUTSIDE source folder
export BAZEL_HOME=/tmp/tensorflow_estimator

# Fails in cowbuilder too because this variable
# is not removed inside the chroot
unexport JAVA_HOME

# 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)
    NUMPY2_ABI := $(shell grep '^abi' /usr/share/numpy/versions | cut -d' ' -f2)
    NUMPY2_API_MIN_VERSION := $(shell grep '^api-min-version' /usr/share/numpy/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)
    NUMPY2_ABI := $(shell grep '^abi' /usr/share/numpy/versions | cut -d' ' -f2)
    NUMPY2_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_PY2BREAKS := -Vpython2custom:Breaks="python-tensorflow (>= $(TF_MAX_VERSION)), python-tensorflow (<< $(TF_MIN_VERSION))"
SUBSTVARS_PY3BREAKS := -Vpython3custom:Breaks="python3-tensorflow (>= $(TF_MAX_VERSION)), python3-tensorflow (<< $(TF_MIN_VERSION))"

# See tensorflow_estimator/tools/pip_package/setup.py
# Wheel is not needed, see debian/patches/Generate_classic_files_instead_of_WHL
SUBSTVARS_PY2DEPS := -Vpython2custom:Depends="python-numpy (>= $(NUMPY2_API_MIN_VERSION)), python-numpy-abi$(NUMPY2_ABI),
SUBSTVARS_PY2DEPS += python-absl (>= 0.1.6~),
SUBSTVARS_PY2DEPS += python-six (>= 1.10.0~)"

SUBSTVARS_PY3DEPS := -Vpython3custom:Depends="python3-numpy (>= $(NUMPY3_API_MIN_VERSION)), python3-numpy-abi$(NUMPY3_ABI),
SUBSTVARS_PY3DEPS += python3-absl (>= 0.1.6~),
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_RELEASE).$(DIST_VERSION)

%:
	dh $@ --with python2,python3

override_dh_auto_build:
	HOME="$(BAZEL_HOME)/cache" bazel build //tensorflow_estimator/tools/pip_package:build_pip_package

override_dh_auto_install:
	HOME="$(BAZEL_HOME)/cache" bazel-bin/tensorflow_estimator/tools/pip_package/build_pip_package "$(CURDIR)/debian"

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

override_dh_auto_test:
	HOME="$(BAZEL_HOME)/cache" bazel test //tensorflow_estimator/...

override_dh_auto_clean:
	dh_auto_clean
	rm -f bazel-*

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