#!/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

# This is the debhelper compatability version o t2use.
export DH_COMPAT=3

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.2.4

ifndef PERL
PERL = /usr/bin/perl
endif



build: build-stamp
build-stamp:
	dh_testdir

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

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

	sed < libtool > libtool-2 \
	-e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
	-e '/^archive_cmds="/s/"$$/ \\$$deplibs"/' 
	mv libtool-2 libtool
	chmod 755 libtool

	# Add here commands to compile the package.

	$(MAKE) 
	cd $(CURDIR)/PerlMagick && perl Makefile.PL
	cd $(CURDIR)/PerlMagick && make \
		LD_RUN_PATH= \
		EXTRALIBS='-L../magick/.libs -L/usr/lib -lMagick -L/usr/X11R6/lib -L/usr/lib -L/usr/lib -llcms -ltiff -lfreetype -ljpeg -lpng -lXext -lSM -lICE -lX11 -lbz2 -lxml2 -lz' \
		LDLOADLIBS='-L../magick/.libs -L/usr/lib -lMagick -L/usr/X11R6/lib -L/usr/lib -L/usr/lib -llcms -ltiff -lfreetype -ljpeg -lpng -lXext -lSM -lICE -lX11 -lbz2 -lxml2 -lz -lpthread -lm -lpthread'

	# Remove duplicate lib entries that ended up in --ldflags by mistake.	
	sed -ie '/--ldflags/,+1s/[[:space:]]-l[^[:space:]]\+//g' \
		"$(CURDIR)/magick/Magick-config"

	touch build-stamp

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

	# 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 ***"; \
	else \
		echo "Skipping regression tests because we appear to be cross-compiling"; \
	fi

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

	# Add here commands to clean up after the build process.
	-cd PerlMagick && $(MAKE) distclean
	-$(MAKE) distclean
	-cd Magick++ && $(MAKE) distclean
	-rm $(CURDIR)/utilities/.libs/*

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/imagemagick.
	$(MAKE) install \
		DESTDIR=$(CURDIR)/debian/imagemagick  \
		pkgdocdir=/usr/share/doc/imagemagick 
#		pkgdatadir=/usr/share/doc/imagemagick 

	cd $(CURDIR)/PerlMagick && \
	$(MAKE) install_vendor  \
		PREFIX=$(CURDIR)/debian/imagemagick/usr \
		SITEPREFIX=$(CURDIR)/debian/imagemagick/usr \
		INSTALLMAN3DIR=$(CURDIR)/debian/imagemagick/usr/share/man/man3 \
		INSTALLSITEMAN3DIR=$(CURDIR)/debian/imagemagick/usr/share/man/man3 
	cp `pwd`/magick/*.h $(CURDIR)/debian/imagemagick/usr/include/magick
	-cp `pwd`/magick/Magick-config.1 $(CURDIR)/debian/imagemagick/usr/share/man/man1/
	-find $(CURDIR)/debian -type f -name .packlist | xargs rm -f

	# Duplicate. License file is already included in the doc dir.
	-rm "$(CURDIR)"/debian/imagemagick/usr/share/ImageMagick-$(IMAGEMAGICKVERSION)/LICENSE

# Use x-terninal emulator for editing (Bug #132947)	
#
	rm $(CURDIR)/debian/imagemagick/usr/lib/ImageMagick-$(IMAGEMAGICKVERSION)/config/delegates.xml
	sed -e s!xterm!/etc/alternatives/x-terminal-emulator!  \
		$(CURDIR)/config/delegates.xml > \
		$(CURDIR)/debian/imagemagick/usr/lib/ImageMagick-$(IMAGEMAGICKVERSION)/config/delegates.xml
	dh_movefiles --sourcedir=debian/imagemagick
	-rm $(CURDIR)/debian/imagemagick/usr/lib/libltdl*
# add write permission for dh_strip
	chmod +w $(CURDIR)/debian/perlmagick/usr/lib/perl5/auto/Image/Magick/Magick.so
#	
# Remove empty directories in debian/imagemagick and debian/libmagick9
# 	
	-find $(CURDIR)/debian/imagemagick -type d -empty | xargs rmdir -p
	-find $(CURDIR)/debian/libmagick9 -type d -empty | xargs rmdir -p
#
# 	-while find $(CURDIR)/debian/imagemagick -empty |xargs rmdir ; do \
# 	 find $(CURDIR)/debian/imagemagick -empty | xargs rmdir; \
# 	done
# 	-while find $(CURDIR)/debian/libmagick9 -empty | xargs rmdir ; do \
# 	 find $(CURDIR)/debian/libmagick9 -empty | xargs rmdir; \
# 	done

clean-tarball: 
	# Nuke logo binaries
	rm -f images/configure.jpg images/logo.jpg images/logo.eps images/examples.jpg \
		www/Magick++/ImageMagick.png
	# Nuke the LogoImage define
	perl -pi -e 'BEGIN{undef $$/;} s/LogoImage\[\]\s+=\s+\{[^\}]+\}/LogoImage[] = {}/s' \
		coders/magick.c


# 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_installmenu
	dh_installmime -pimagemagick
	dh_installinfo
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_perl
	dh_shlibdeps -L libmagick9 -ldebian/libmagick9/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

