#!/usr/bin/make -f

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

export UPSTREAM_VERSION := $(shell echo "$(DEB_VERSION)" | cut -d '+' -f 1)
export VIEWER_TARGET := libOrthancWSI.so
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 $@

ORTHANC_FRAMEWORK_LIBRARIES="boost_filesystem boost_iostreams boost_locale boost_regex boost_thread jsoncpp pugixml uuid  dcmdata ofstd jpeg png curl boost_program_options dcmjpeg dcmjpls dcmimage dl crypto"

CMAKE_APPLICATIONS_EXTRA_FLAGS += \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DORTHANC_FRAMEWORK_SOURCE=system \
        -DORTHANC_FRAMEWORK_USE_SHARED:BOOL=OFF \
        -DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES=${ORTHANC_FRAMEWORK_LIBRARIES} \
	-DCMAKE_BUILD_TYPE=None   # The build type must be set to None, see #711515

CMAKE_VIEWER_EXTRA_FLAGS += \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DORTHANC_FRAMEWORK_SOURCE=system \
        -DORTHANC_FRAMEWORK_USE_SHARED:BOOL=OFF \
        -DORTHANC_FRAMEWORK_ADDITIONAL_LIBRARIES=${ORTHANC_FRAMEWORK_LIBRARIES} \
	-DUSE_SYSTEM_OPENLAYERS:BOOL=ON \
	-DOPENLAYERS_CSS=$(CURDIR)/BuildViewer/ol.min.css \
	-DOPENLAYERS_JS=$(CURDIR)/BuildViewer/ol.min.js \
	-DCMAKE_BUILD_TYPE=None   # The build type must be set to None, see #711515

# Automated generation of the "Built-Using" attribute in "d/control"
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=989126
# Adapted from:
# https://wiki.debian.org/SIMDEverywhere#Approach (Point 6)
#
# In addition, automated generation of the "Depends" on "dcmtk-data"
# to access the DICOM dictionaries during runtime:
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1076541
override_dh_gencontrol:
	dh_gencontrol -- \
		-Vorthancframework:Built-Using="$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W liborthancframework-dev)" \
		-Vdcmtk-data:Depends="$(shell dpkg-query -f '$${binary:Package} (>= $${source:Upstream-Version}), ' -W dcmtk-data)"

override_dh_auto_configure:
        # Configure the command-line tools
	mkdir BuildApplications
	dh_auto_configure --builddirectory=BuildApplications \
			  --sourcedirectory=Applications \
			  -- $(CMAKE_APPLICATIONS_EXTRA_FLAGS)

        # Minify the official OpenLayers 3.19.0 distribution (Debian
        # packages only contains OpenLayers 2 for the time being,
        # which is incompatible with OpenLayers 3)
	mkdir -p BuildViewer
	yui-compressor debian/JS/openlayers-3.19.0/ol-debug.js > BuildViewer/ol.min.js
	yui-compressor debian/JS/openlayers-3.19.0/ol.css > BuildViewer/ol.min.css

        # Configure the Web viewer plugin
	dh_auto_configure --builddirectory=BuildViewer \
			  --sourcedirectory=ViewerPlugin \
			  -- $(CMAKE_VIEWER_EXTRA_FLAGS)

override_dh_clean:
	dh_clean BuildApplications/
	dh_clean BuildViewer/

override_dh_auto_build:
        # Build the command-line tools
	dh_auto_build --builddirectory=BuildApplications \
		      --sourcedirectory=Applications

        # Build the Web viewer plugin
	dh_auto_build --builddirectory=BuildViewer \
		      --sourcedirectory=ViewerPlugin

override_dh_auto_install:
        # Install the command-line tools
	dh_auto_install --builddirectory=BuildApplications

        # Install the plugin
	dh_install BuildViewer/${VIEWER_TARGET}.${UPSTREAM_VERSION} usr/lib/orthanc

override_dh_link:
	dh_link usr/lib/orthanc/${VIEWER_TARGET}.${UPSTREAM_VERSION} \
		usr/share/orthanc/plugins/${VIEWER_TARGET}

override_dh_installchangelogs:
	dh_installchangelogs -k NEWS
