#!/usr/bin/make -f

export PYBUILD_NAME=onnx-tf
export PYBUILD_BEFORE_TEST=mkdir -p {build_dir}/.onnx/models/ && for model in /usr/share/onnx-tf-test-models/*; do ln -s $$model {build_dir}/.onnx/models/; done && cp {dir}/test/frontend/test_model.yaml {build_dir}/test/frontend/
export PYBUILD_AFTER_TEST=rm -rf {build_dir}/.onnx/models/
# UnitTest don't work with pytest
export PYBUILD_TEST_PYTEST=0
export ONNX_MODELS=/usr/share/onnx-tf-test-models/

# TensorFlow below 1.12 does not support Python 3.7
TF_VERSION = $(shell python3 -c 'import tensorflow; print(tensorflow.VERSION)')
ifneq ($(shell dpkg --compare-versions $(TF_VERSION) ge 1.12.0 && echo yes),yes)
  export PYBUILD_DISABLE_python3.7=test
endif

# 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 --count 1 --show-field Version)
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)
SUBSTVARS_BINARY_VERSION := -Vbinary:Version="$(PKG_VERSION_TAG)"
SUBSTVARS_SOURCE_VERSION := -Vsource:Version="$(PKG_VERSION_TAG)"


generate-test-models-pkg:
	# Run debian/rules generate-test-models-pkg to generate a new package containing test models
	cd debian/onnx-tensorflow-test-models \
	  && fakeroot debian/rules clean \
	  && fakeroot debian/rules binary \
	  && fakeroot debian/rules clean \
	  && cd $(CURDIR) \
	  && mv debian/onnx-tf-test-models_$(PKG_VERSION)_all.deb ../ \
	  && rm -f debian/onnx-tensorflow_0.0_* \
	  && rm -f debian/onnx-tensorflow_0.0.*

%:
	dh $@ --with python2,python3 --buildsystem=pybuild


override_dh_auto_install:
	dh_auto_install
	rm -rf $(CURDIR)/debian/python-onnx-tf/usr/lib/python*/dist-packages/third_party/
	rm -rf $(CURDIR)/debian/python3-onnx-tf/usr/lib/python*/dist-packages/third_party/


override_dh_auto_clean:
	dh_auto_clean
	rm -rf .cache


override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)" $(SUBSTVARS_BINARY_VERSION) $(SUBSTVARS_SOURCE_VERSION)
