#!/usr/bin/make -f
#export DH_VERBOSE = 1

# Hardening Caffe according to https://wiki.debian.org/Hardening
export DEB_BUILD_MAINT_OPTIONS   = hardening=+all
export DEB_CFLAGS_MAINT_APPEND   = -Wall #-pedantic
export DEB_CXXFLAGS_MAINT_APPEND = -Wall #-pedantic
export DEB_LDFLAGS_MAINT_APPEND  = -Wl,--as-needed

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
RULES         := debian/rules
ORIG_SOURCE   := "https://github.com/BVLC/caffe/archive/rc4.tar.gz"
CAFFE_SOVER   := 1.0.0-rc4
BUILDDIR      := "caffe_cpu_build"

TEMPLATES     := $(wildcard debian/*.in)
AUTOGEN       := $(patsubst %.in,%,$(TEMPLATES))

## CMake Configuration Template.
## * PLEASE sync this common template with caffe-contrib source.
CMAKE_CONFIGURE_TEMPLATE = \
	-DALLOW_LMDB_NOLOCK=OFF \
	-DBLAS="Generic" \
	-DBUILD_SHARED_LIBS=ON \
	-DBUILD_docs=OFF \
	-DBUILD_matlab=OFF \
	-DBUILD_python=ON \
	-DBUILD_python_layer=ON \
	-Dpython_version="3" \
	-DCMAKE_BUILD_TYPE="Release" \
	-DCMAKE_C_FLAGS="$(CFLAGS) $(CPPFLAGS)" \
	-DCMAKE_CXX_FLAGS="$(CXXFLAGS) $(CPPFLAGS)" \
	-DCMAKE_SKIP_RPATH=TRUE \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DUSE_CUDNN=OFF \
	-DUSE_LEVELDB=ON \
	-DUSE_LMDB=ON \
	-DUSE_OPENCV=ON \
	-DCPU_ONLY=$(FILL_THIS_BLANK) \
	-DCMAKE_INSTALL_PREFIX="/usr"
# These flags are for cuda version but please keep them
#	-DCUDA_ARCH_NAME="All"
#	-DCMAKE_CXX_COMPILER=g++-5
#	-DCMAKE_C_COMPILER=gcc-5

CONFFLAG_CPU = \
	$(CMAKE_CONFIGURE_TEMPLATE) \
	-DCPU_ONLY=ON

%:: %.in
	perl -p \
	-e 's{#CAFFE_CONFIGURE#}{caffe_cpu}g;' \
	-e 's{#CAFFE_SOVER#}{$(CAFFE_SOVER)}g;' \
	-e 's{#DEB_HOST_MULTIARCH#}{$(DEB_HOST_MULTIARCH)}g;' \
	< $< > $@

%:
	dh $@ \
		--parallel \
		--buildsystem=cmake \
		--with python3,bash-completion

override_dh_auto_configure: $(AUTOGEN)
	dh_auto_configure --builddirectory=$(BUILDDIR) \
		-- $(CONFFLAG_CPU)

override_dh_auto_clean:
	dh_auto_clean --builddirectory=$(BUILDDIR)
	-$(RM) -rf python/caffe/proto/ doxygen
	-$(RM) python/caffe/_caffe.so
	-find . -type d -name '__pycache__' -exec rm -rf '{}' +

override_dh_auto_build-indep:
	doxygen .Doxyfile # we don't use BUILD_DOC provided by upstream cmake
	sed -i -e 's/]{hyperref}/,draft]{hyperref}/g' doxygen/latex/refman.tex # patch the tex source to avoid a pdflatex compilation error
	$(MAKE) -C doxygen/latex/
	sed -i -e 's/cdn.mathjax.org/127.0.0.1/g' docs/_layouts/default.html # lintian privacy
	sed -i -e 's/code.jquery.com/127.0.0.1/g' examples/web_demo/templates/index.html # lintian privacy
	sed -i -e 's/netdna.bootstrapcdn.com/127.0.0.1/g' examples/web_demo/templates/index.html # lintian privacy
	find doxygen/html -name 'jquery.js' -delete # lintian embedded-javascript
	find docs -size 0 -delete # lintian zero-byte-file
	find docs -name '.gitignore' -delete # lintian vcs-control-file
	gzip -d examples/finetune_flickr_style/flickr_style.csv.gz # lintian gzip timestamp
	gzip -n ./examples/finetune_flickr_style/flickr_style.csv # ditto

override_dh_auto_build-arch:
	dh_auto_build --builddirectory=$(BUILDDIR) \
		-- caffe pycaffe test.testbin all

# In the test phase, we need to set LD_LIBRARY_PATH properly
# for those test ELFs linked with libcaffe.so.X
override_dh_auto_test-arch:
	dh_auto_test --builddirectory=$(BUILDDIR) \
		-- runtest pytest LD_LIBRARY_PATH=$(shell pwd)/$(BUILDDIR)/lib/ 
override_dh_auto_test-indep:

override_dh_python3:
	dh_python3 --requires=python/requirements.txt
	dh_numpy3

override_dh_install:
	dh_install --list-missing

override_dh_auto_install:
	dh_auto_install --builddirectory=$(BUILDDIR) -- install

override_dh_makeshlibs:
	dh_makeshlibs --package=python3-caffe-cpu -X/usr/lib/python
	dh_makeshlibs --remaining-packages

override_dh_fixperms:
	dh_fixperms
	chmod -x debian/tmp/usr/lib/python3/dist-packages/caffe/proto/caffe_pb2.py
	chmod -x debian/tmp/usr/lib/python3/dist-packages/caffe/proto/__init__.py

get-orig-source:
	wget -c $(ORIG_SOURCE) -O caffe_1.0.0~rc4.orig.tar.gz
