#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

IMAGEMAGICKVERSION=6.6.0

DESTDIR="$(CURDIR)/debian/tmp"

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += -j$(NUMJOBS)
endif

build: build-stamp
build-stamp: $(QUILT_STAMPFN)
	dh_testdir

	MagickDocumentPath="/usr/share/doc/imagemagick" ./configure \
	--prefix=/usr \
	--mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info \
	--with-modules \
	--with-gs-font-dir=/usr/share/fonts/type1/gsfonts \
	--with-magick-plus-plus \
	--with-djvu \
	--enable-shared \
	--without-dps \
	--without-fpx \
	--with-perl-options='INSTALLDIRS=vendor' \
	--x-includes=/usr/include/X11 \
	--x-libraries=/usr/lib/X11

	cd "$(CURDIR)/PerlMagick" && \
		perl Makefile.PL

	# Patch the generated libtool to avoid passing -rpath when linking,
	# and to explicitly link libraries against the libraries they
	# depend on.

	sed -i libtool \
		-e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
		-e '/^archive_cmds="/s/"$$/ \\$$deplibs"/'

	$(MAKE)

	cd "$(CURDIR)/PerlMagick" && \
		$(MAKE)

	touch build-stamp

check: check-stamp
check-stamp: build-stamp
	dh_testdir

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# Only run the testsuite to get more info in the build logs for now,
	# but don't make a failing testsuite fail the whole build. Change it
	# to a fatal error only once we've got an estimate on how harmful
	# that would be.
	@if test "$(DEB_HOST_GNU_TYPE)" = "$(DEB_BUILD_GNU_TYPE)"; then \
		unset DISPLAY; \
		LD_LIBRARY_PATH="$(CURDIR)/magick/.libs:$(CURDIR)/wand/.libs:$(CURDIR)/Magick++/lib/.libs:$$LD_LIBRARY_PATH" \
		$(MAKE) check LD_RUN_PATH= && echo "*** Testsuite succeeded ***" || echo "*** Testsuite failed ***"; \
		cat test-suite.log; \
	else \
		echo "Skipping regression tests because we appear to be cross-compiling"; \
	fi
endif

	touch check-stamp
clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Commands to clean up after the build process
	[ ! -f PerlMagick/Makefile ] || (cd PerlMagick && $(MAKE) distclean)
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f Magick++/Makefile ] || (cd Magick++ && $(MAKE) distclean)
	rm -f "$(CURDIR)/utilities/.libs/*" "$(CURDIR)/_configs.sed" \
		"$(CURDIR)/magick/magick-config.h"

	dh_clean build-stamp

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install \
		DESTDIR="$(CURDIR)/debian/tmp" \
		pkgdocdir=/usr/share/doc/imagemagick

	mkdir -p $(DESTDIR)/usr/share/pixmaps/
	install -m 644 "$(CURDIR)/debian/display.xpm" \
		$(DESTDIR)/usr/share/pixmaps/

	-find "$(CURDIR)/debian" -type f -name .packlist | xargs rm -f

	# Fix HTML location inside manpages
	sed -i 's/doc\/ImageMagick-$(IMAGEMAGICKVERSION)/doc\/imagemagick/' \
		$(DESTDIR)/usr/share/man/man*/*
	sed -i 's/doc\/ImageMagick\\-$(IMAGEMAGICKVERSION)/doc\/imagemagick/' \
		$(DESTDIR)/usr/share/man/man*/*

	# Use x-terminal emulator for editing (Bug #132947)
	sed -i 's/xterm/\/etc\/alternatives\/x-terminal-emulator/' \
		$(DESTDIR)/usr/lib/ImageMagick-$(IMAGEMAGICKVERSION)/config/delegates.xml

	# Remove empty directories in debian/tmp
	-find $(DESTDIR) -type d -empty | xargs rmdir -p

	# Remove RPATH from Magick.so
	chrpath -d $(DESTDIR)/usr/lib/perl5/auto/Image/Magick/Magick.so

	# empties dependency_libs from .la files
	# http://lists.debian.org/debian-devel/2009/08/msg00783.html
	find $(DESTDIR)/usr/lib -name '*.la' -exec \
		sed -i "s,^dependency_libs=.*,dependency_libs=''," {} \;

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build check install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_install
	# Remove extra coders
	while read FILE; do \
		rm -f debian/libmagickcore3/$$FILE; \
	done < debian/libmagickcore3-extra.install
	dh_installmenu
	dh_installman
	dh_installmime
	dh_installinfo
	dh_installchangelogs
	dh_link
	dh_strip --dbg-package=imagemagick-dbg
	dh_compress
	dh_fixperms
	dh_perl
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
