#!/usr/bin/make -f

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

export JAVASCRIPT_LIBS := Build/javascript-libs
export UPSTREAM_VERSION := $(shell echo "$(DEB_VERSION)" | cut -d '-' -f 1)
export TARGET := libOrthancWebViewer.so

export DEB_BUILD_MAINT_OPTIONS := hardening=+all

%:
	dh $@ --builddirectory=Build

CMAKE_EXTRA_FLAGS += \
	-DCMAKE_SKIP_RPATH:BOOL=ON \
	-DSTATIC_BUILD:BOOL=OFF \
	-DSTANDALONE_BUILD:BOOL=ON \
        -DUSE_GTEST_DEBIAN_SOURCE_PACKAGE:BOOL=ON \
	-DCMAKE_BUILD_TYPE=""   # The build type must be left empty, see #711515

override_dh_auto_configure:
        # Place the third-party JavaScript libraries at the location
        # expected by CMake
	mkdir -p ${JAVASCRIPT_LIBS}/fonts
	mkdir -p ${JAVASCRIPT_LIBS}/images

        # Use the jquery library from Debian
	cp /usr/share/javascript/jquery/jquery.min.js ${JAVASCRIPT_LIBS}/jquery.js

        # The jsPanel-2.3.3 toolbox used in the Orthanc Web viewer is
        # not compatible with the version of the "jquery-ui" package
        # shipped with Debian. We need to force the version of jquery-ui.
	yui-compressor debian/JS/jquery-ui-1.11.3/jquery-ui.js > \
		${JAVASCRIPT_LIBS}/jquery-ui.min.js
	yui-compressor debian/JS/jquery-ui-1.11.3/jquery-ui.css > \
		${JAVASCRIPT_LIBS}/jquery-ui.min.css
	yui-compressor debian/JS/jquery-ui-1.11.3/jquery-ui.theme.css > \
		${JAVASCRIPT_LIBS}/jquery-ui.theme.min.css
	cp debian/JS/jquery-ui-1.11.3/images/* ${JAVASCRIPT_LIBS}/images

        # Note: We don't minify the Cornerstone library, as the
        # version of yui-compressor shipped by Debian Sid crashes on it
	cp debian/JS/cornerstone-0.11.0/cornerstone.js \
		${JAVASCRIPT_LIBS}/cornerstone.min.js
	cp debian/JS/cornerstone-0.11.0/cornerstone.css \
		${JAVASCRIPT_LIBS}/cornerstone.css

	yui-compressor debian/JS/jsPanel-2.3.3/jquery.jspanel.js > \
		${JAVASCRIPT_LIBS}/jquery.jspanel.min.js
	yui-compressor debian/JS/jsPanel-2.3.3/jquery.jspanel.css > \
		${JAVASCRIPT_LIBS}/jquery.jspanel.min.css
	yui-compressor debian/JS/jsPanel-2.3.3/jquery.ui.touch-punch.js > \
		${JAVASCRIPT_LIBS}/jquery.ui.touch-punch.min.js
	yui-compressor debian/JS/jsPanel-2.3.3/mobile-detect.js > \
		${JAVASCRIPT_LIBS}/mobile-detect.min.js
	cp debian/JS/jsPanel-2.3.3/images/* ${JAVASCRIPT_LIBS}/images/
	cp debian/JS/jsPanel-2.3.3/fonts/* ${JAVASCRIPT_LIBS}/fonts/

	yui-compressor debian/JS/js-url-1.8.6/url.js > \
		${JAVASCRIPT_LIBS}/url.min.js

	yui-compressor debian/JS/pako-0.2.5/pako_inflate.js > \
		${JAVASCRIPT_LIBS}/pako_inflate.min.js

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

override_dh_auto_test:
	( cd Build; ./UnitTests )

override_dh_auto_install:
	cp Build/${TARGET} Build/${TARGET}.${UPSTREAM_VERSION}
	dh_install Build/${TARGET}.${UPSTREAM_VERSION} usr/lib

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

override_dh_installchangelogs:
	dh_installchangelogs -k NEWS

get-orig-source:
	uscan --verbose --force-download --repack --compression xz
