#! /usr/bin/make -f

BACKEND_NAME := imagescan
export BACKEND_NAME

# FIXME: run --with autoreconf so we get the latest config.{sub,guess}
#        see https://wiki.debian.org/Autoreconf, needs a build-depends
#        on dh-autoreconf (available in Ubuntu 12.04, should be fine).
#        Only do this on distributions that meet version requirements
#        in configure for the tools that autoreconf runs.
%:
	dh $@ --verbose \
	    --parallel

override_dh_clean:
	dh_clean
	sed -i 's/imagescan-3\.\([^ \t]*\)/utsushi-0.\1  /' NEWS

os_id = $(shell test -e /etc/os-release && . /etc/os-release && echo $$ID)
os_ver = $(shell test -e /etc/os-release && . /etc/os-release && echo $$VERSION_ID)
cxx11flag =
ifeq ($(strip ${os_id}),ubuntu)
ifeq ($(strip ${os_ver}),16.04)
cxx11flag = CXXFLAGS="-std=c++11"
endif
endif
ifeq ($(strip ${os_id}),linuxmint)
ifeq ($(strip ${os_ver}),18)
cxx11flag = CXXFLAGS="-std=c++11"
endif
endif
override_dh_auto_configure:
	env
	sed -i 's/^utsushi-0\.\([^ \t]*\)  /imagescan-3.\1/' NEWS
	dh_auto_configure -- \
	    --with-jpeg \
	    --with-tiff \
	    --with-gtkmm \
	    --with-sane \
	    --with-magick \
	    --with-magick-pp \
	    ${cxx11flag}
	sed -i '/BACKEND_NAME = /s/=/?=/' sane/Makefile
	sed -i '/BACKEND_NAME = /s/=/?=/' sane/tests/Makefile

# FIXME: address the compiler warning in the test suite and remove
#        this override
override_dh_auto_test:

destdir         = debian/tmp
dest_prefix     = ${destdir}/usr
dest_bindir     = ${dest_prefix}/bin
dest_datadir    = ${dest_prefix}/share
dest_pkgdatadir = ${dest_datadir}/utsushi
dest_confdir    = ${destdir}/etc
dest_pkgconfdir = ${dest_confdir}/imagescan
dest_udevdir    = ${dest_confdir}/udev/rules.d
override_dh_auto_install:
	dh_auto_install
	mv ${dest_bindir}/utsushi ${dest_bindir}/imagescan
	install -m 0644 doc/icon32.xpm ${dest_pkgdatadir}/
	install -m 0644 doc/icon16.xpm ${dest_pkgdatadir}/
	test -d ${dest_pkgconfdir} || mkdir -p ${dest_pkgconfdir}
	install -m 0644 debian/imagescan.conf ${dest_pkgconfdir}
	mv ${dest_confdir}/utsushi/combo.conf ${dest_pkgconfdir}
	cat ${dest_udevdir}/*.rules > debian/imagescan.udev
	rm ${dest_udevdir}/*.rules
	test -d ${destdir}/usr/share/applications \
	    || mkdir -p ${destdir}/usr/share/applications
	install -m 0644 debian/imagescan.desktop \
	    ${destdir}/usr/share/applications/
	find ${dest_prefix}/lib* \
	    \( -name 'lib*.a' -o -name 'lib*.so' -o -name 'libsane-*.la' \) \
	    -delete

override_dh_install:
	dh_install --autodest \
	    --fail-missing \
	    --exclude=/include/

override_dh_installdocs:
	dh_installdocs --all \
	    NEWS \
	    README \
	    AUTHORS \
	    COPYING

override_dh_installudev:
	dh_installudev --priority=60

override_dh_strip:
	dh_strip --dbg-package=imagescan-dbg

# FIXME: get rid of the useless dependencies
override_dh_makeshlibs:
	dh_makeshlibs \
	    --exclude=libboost_ \
	    --exclude=libsane- \
	    --exclude=libcnx- \
	    --exclude=libdrv- \
	    --exclude=libflt-

# With older versions of debhelper the dh_shlibdeps utility needs to
# be invoked with a suitable option to find internal libraries.
# TODO: remove when no longer needed
DEB_BUILD_MULTIARCH ?= $(shell dpkg-architecture -qDEB_BUILD_MULTIARCH)
dh_min = 9.20120909
dh_cur = $(shell dpkg-query -W -f='$${Version}' debhelper)
dh_shlibdeps_opts = $(shell dpkg --compare-versions ${dh_min} le ${dh_cur} \
    || echo -l$(PWD)/debian/imagescan/usr/lib/${DEB_BUILD_MULTIARCH}/utsushi)
ifneq ($(strip ${dh_shlibdeps_opts}),)
override_dh_shlibdeps:
	dh_shlibdeps ${dh_shlibdeps_opts}
endif
