#!/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

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

# 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))"

# 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:
	bazel build tensorboard:tensorboard
	bazel build //tensorboard/pip_package:build_pip_package

override_dh_auto_install:
	# See debian/patches/Generate_classic_files_instead_of_WHL
	$(CURDIR)/bazel-bin/tensorboard/pip_package/build_pip_package $(CURDIR)/debian
	# Remove useless "external" folder
	rm -rf $(CURDIR)/debian/python3-tensorboard/usr/lib/python3/dist-packages/external/
	rm -rf $(CURDIR)/debian/python-tensorboard/usr/lib/python2.?/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
	mv $(CURDIR)/debian/python-tensorboard/usr/bin/tensorboard $(CURDIR)/debian/python-tensorboard/usr/bin/tensorboard-py2
	ln -sf /usr/bin/tensorboard-py3 $(CURDIR)/debian/python3-tensorboard/usr/bin/tensorboard

override_dh_python3:
	# See tensorboard/pip_package/setup.py
	# Wheel is not needed, see debian/patches/Generate_classic_files_instead_of_WHL
	dh_python3 --depends='numpy >= 1.11.0' \
		   --depends='six >= 1.10.0' \
		   --depends='protobuf >= 3.3.0' \
		   --depends='werkzeug >= 0.11.10' \
		   --depends='markdown >= 2.6.8' \
		   --accept-upstream-versions

override_dh_python2:
	# See tensorboard/pip_package/setup.py
	# Wheel is not needed, see debian/patches/Generate_classic_files_instead_of_WHL
	dh_python2 --depends='numpy >= 1.11.0' \
		   --depends='six >= 1.10.0' \
		   --depends='protobuf >= 3.3.0' \
		   --depends='werkzeug >= 0.11.10' \
		   --depends='markdown >= 2.6.8' \
		   --depends='concurrent.futures >= 3' \
		   --accept-upstream-versions

override_dh_auto_clean:
	dh_auto_clean
	rm -f bazel-*

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