#!/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 FRAMEWORK_VERSION := 1
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 $@

CMAKE_EXTRA_FLAGS += \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DSTATIC_BUILD:BOOL=OFF \
	-DSTANDALONE_BUILD:BOOL=ON \
	-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

CMAKE_EXTRA_FLAGS_SERVER += \
	-DBUILD_CONNECTIVITY_CHECKS:BOOL=OFF

# "ORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES" must be set to link the
# unit tests
CMAKE_EXTRA_FLAGS_STATIC_FRAMEWORK += \
	-DORTHANC_INSTALL_PREFIX:PATH=. \
	-DBUILD_SHARED_LIBRARY=OFF \
	"-DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES=uuid curl civetweb lua5.3 boost_iostreams boost_locale ofstd png jpeg z crypto"

CMAKE_EXTRA_FLAGS_SHARED_FRAMEWORK += \
	-DORTHANC_INSTALL_PREFIX:PATH=. \
	-DBUILD_SHARED_LIBRARY=ON \
	-DORTHANC_FRAMEWORK_SOVERSION=1

override_dh_auto_configure:
        # Place back minified JavaScript libraries that were stripped from upstream
	yui-compressor debian/JS/jquery-1.7.2.js \
	    > OrthancServer/OrthancExplorer/libs/jquery.min.js
	yui-compressor debian/JS/jquery.mobile-1.1.0/jquery.mobile-1.1.0.js \
	    > OrthancServer/OrthancExplorer/libs/jquery.mobile.min.js
	yui-compressor debian/JS/jquery.mobile-1.1.0/jquery.mobile-1.1.0.css \
	    > OrthancServer/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 \
	    > OrthancServer/OrthancExplorer/libs/date.js
	cp debian/JS/jquery.mobile-1.1.0/images/* OrthancServer/OrthancExplorer/libs/images

        # Launch the original Orthanc CMake scripts
	dh_auto_configure --sourcedir=./OrthancFramework/SharedLibrary/ --builddirectory=BuildStaticFramework \
                          -- $(CMAKE_EXTRA_FLAGS) $(CMAKE_EXTRA_FLAGS_STATIC_FRAMEWORK)
	dh_auto_configure --sourcedir=./OrthancFramework/SharedLibrary/ --builddirectory=BuildSharedFramework \
                          -- $(CMAKE_EXTRA_FLAGS) $(CMAKE_EXTRA_FLAGS_SHARED_FRAMEWORK)
	dh_auto_configure --sourcedir=./OrthancServer/ --builddirectory=BuildServer \
                          -- $(CMAKE_EXTRA_FLAGS) $(CMAKE_EXTRA_FLAGS_SERVER)

override_dh_auto_build:
	dh_auto_build --builddirectory=BuildStaticFramework
	dh_auto_build --builddirectory=BuildSharedFramework
	dh_auto_build --builddirectory=BuildServer

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

override_dh_auto_clean:
	# see override_dh_auto_configure:
	dh_clean OrthancServer/OrthancExplorer/libs/jquery.min.js
	dh_clean OrthancServer/OrthancExplorer/libs/jquery.mobile.min.js
	dh_clean OrthancServer/OrthancExplorer/libs/jquery.mobile.min.css
	dh_clean OrthancServer/OrthancExplorer/libs/date.js
	dh_clean -Xnotes.txt OrthancServer/OrthancExplorer/libs/images/*.png
	dh_clean -Xnotes.txt OrthancServer/OrthancExplorer/libs/images/*.gif
	# see override_dh_auto_build:
	dh_clean BuildStaticFramework/
	dh_clean BuildSharedFramework/
	dh_clean BuildServer/

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/

	( cd BuildStaticFramework && LOCPATH=$(DESTDIR)/locale/ ./UnitTests-prefix/src/UnitTests-build/UnitTests )
	( cd BuildSharedFramework && LOCPATH=$(DESTDIR)/locale/ LD_LIBRARY_PATH=. ./UnitTests-prefix/src/UnitTests-build/UnitTests )
	( cd BuildServer && LOCPATH=$(DESTDIR)/locale/ ./UnitTests --gtest_filter=-Version.CivetwebCompression )
endif

override_dh_auto_install:
        # 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 OrthancServer/Resources/Samples $(DOC_DIR)/Samples
	cp -r OrthancServer/Plugins/Samples $(DOC_DIR)/OrthancPluginSamples

        # Run the default installation steps
	dh_auto_install --builddirectory=BuildStaticFramework
	dh_auto_install --builddirectory=BuildSharedFramework
	dh_auto_install --builddirectory=BuildServer

        # Move the plugins from "/usr/share/orthanc/plugins" to
        # "/usr/lib/orthanc", to solve Lintian warning
        # "link-to-shared-library-in-wrong-package"
	mkdir -p $(DESTDIR)/usr/lib/orthanc
	mv $(DESTDIR)/usr/share/orthanc/plugins/libServeFolders.so.$(UPSTREAM_VERSION) \
	   $(DESTDIR)/usr/lib/orthanc
	mv $(DESTDIR)/usr/share/orthanc/plugins/libModalityWorklists.so.$(UPSTREAM_VERSION) \
	   $(DESTDIR)/usr/lib/orthanc

        # Update the symbolic links accordingly
	( cd $(DESTDIR)/usr/share/orthanc/plugins/ && \
	  rm ./libServeFolders.so ./libModalityWorklists.so && \
	  ln -s ../../../lib/orthanc/libServeFolders.so.$(UPSTREAM_VERSION) libServeFolders.so && \
	  ln -s ../../../lib/orthanc/libModalityWorklists.so.$(UPSTREAM_VERSION) libModalityWorklists.so )

override_dh_installchangelogs:
	dh_installchangelogs -k NEWS

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