#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk   # To access the "DEB_VERSION" variable

export DESTDIR := $(CURDIR)/debian/tmp
export DOC_DIR := $(DESTDIR)/usr/share/doc/orthanc
export PLUGINS_DIR := $(DESTDIR)/usr/share/orthanc/plugins

export UPSTREAM_VERSION := $(shell echo "$(DEB_VERSION)" | cut -d '+' -f 1)
export BUILDARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Disable assert() checking from upstream project, for best performance
# https://lists.debian.org/debian-med/2018/04/msg00132.html
export DEB_CFLAGS_MAINT_APPEND=-DNDEBUG
export DEB_CXXFLAGS_MAINT_APPEND=-DNDEBUG

%:
	dh $@ --builddirectory=Build

CMAKE_EXTRA_FLAGS += \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DSTATIC_BUILD:BOOL=OFF \
	-DSTANDALONE_BUILD:BOOL=ON \
	-DENABLE_CIVETWEB:BOOL=OFF \
	-DUSE_SYSTEM_MONGOOSE:BOOL=OFF \
	-DUSE_GOOGLE_TEST_DEBIAN_PACKAGE:BOOL=ON \
	-DDCMTK_LIBRARIES:STRING=dcmjpls \
	-DUNIT_TESTS_WITH_HTTP_CONNEXIONS:BOOL=OFF \
	-DCMAKE_BUILD_TYPE=None  # The build type must be set to None, see #711515

override_dh_auto_configure:
        # Put 3rd party packages where the cmake build system expects them
	mkdir -p ThirdPartyDownloads
	( cd ThirdPartyDownloads; cp ../debian/ThirdPartyDownloads/* . )

        # Place back minified JavaScript libraries that were stripped from upstream
	yui-compressor debian/JS/jquery-1.7.2.js \
	    > OrthancExplorer/libs/jquery.min.js
	yui-compressor debian/JS/jquery.mobile-1.1.0/jquery.mobile-1.1.0.js \
	    > OrthancExplorer/libs/jquery.mobile.min.js
	yui-compressor debian/JS/jquery.mobile-1.1.0/jquery.mobile-1.1.0.css \
	    > OrthancExplorer/libs/jquery.mobile.min.css
	cat debian/JS/DateJs/src/globalization/en-US.js \
	    debian/JS/DateJs/src/core-debug.js \
	    debian/JS/DateJs/src/sugarpak-debug.js \
	    debian/JS/DateJs/src/parser-debug.js \
	    | yui-compressor --type js \
	    > OrthancExplorer/libs/date.js
	cp debian/JS/jquery.mobile-1.1.0/images/* OrthancExplorer/libs/images

        # Launch the original Orthanc CMake script
	dh_auto_configure -- $(CMAKE_EXTRA_FLAGS)

override_dh_auto_build:
	dh_auto_build

        # Fix the lintian warning: "orthanc-doc: embedded-javascript-library
        # usr/share/doc/orthanc/OrthancPlugin/jquery.js please use libjs-jquery"
	rm Build/OrthancPluginDocumentation/doc/jquery.js
	ln -s /usr/share/javascript/jquery/jquery.min.js Build/OrthancPluginDocumentation/doc/jquery.js

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
        # Generate the en_US.UTF-8 locale before running tests
        # (required for case-insensitive comparison of strings with
        # accents). Full explanation:
        # https://www.mattfischer.com/blog/archives/105
	mkdir -p $(DESTDIR)/locale/
	localedef -f UTF-8 -i en_US $(DESTDIR)/locale/en_US.UTF-8/

ifeq ($(BUILDARCH),i386)
        # Disable unit test "ImageProcessing.Convolution" that only
        # fails on Debian automated build machines (the test passes
        # locally on Docker image "i386/debian:sid")
	( cd Build; LOCPATH=$(DESTDIR)/locale/ ./UnitTests --gtest_filter=-ImageProcessing.Convolution )
else
	( cd Build; LOCPATH=$(DESTDIR)/locale/ ./UnitTests )
endif
endif

override_dh_clean:
	rm -rf ThirdPartyDownloads
	dh_clean

override_dh_auto_install-indep:
        # Move the index of the "orthanc-doc" package from Debian
	mkdir -p $(DOC_DIR)
	cp debian/docs/index.html $(DOC_DIR)

        # Populate the content of the "orthanc-doc" package
	cp -r Resources/Samples $(DOC_DIR)/Samples
	cp -r Plugins/Samples $(DOC_DIR)/OrthancPluginSamples

	dh_auto_install -i

override_dh_link-arch:
        # Create a link to the shared library of the plugins
	dh_link usr/lib/orthanc/libServeFolders.so.$(UPSTREAM_VERSION) \
	        usr/share/orthanc/plugins/libServeFolders.so
	dh_link usr/lib/orthanc/libModalityWorklists.so.$(UPSTREAM_VERSION) \
	        usr/share/orthanc/plugins/libModalityWorklists.so

override_dh_installchangelogs:
	dh_installchangelogs -k NEWS

override_dh_compress:
        # Do not compress the samples
	dh_compress -XOrthancPluginSamples -XSamples
