#!/usr/bin/make -f
# GIMP1.2's debian/rules file. Uses debhelper by Joey Hess.
# Based on sample debhelper debian/rules file.
# Written 2 Jun 1998 by Ben Gertzfield <che@debian.org>
# Hacked 29 May 2000 by Ian McKellar <yakk@gimp.org>

## Read major and minor version numbers from the changelog :-)
version=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' | sed -e 's/-.*//')

ifeq ($(DEBCVS),1)
	major=1.2
	minor=$(shell echo ${version} | sed -e 's/^[0-9]*\.//')
else
	major=$(shell echo ${version} | sed -e 's/\.[0-9]*$$//')
	minor=$(shell echo ${version} | sed -e 's/^[0-9]*\.[0-9]*\.//')
endif

next_version=${major}.$(shell expr ${minor} + 1)

build: build-stamp
build-stamp:
	dh_testdir

	# We may need these in the future because of the /etc patch
	#aclocal
	#automake
	#autoconf

	# Add here commands to compile the pacckage.
	./configure --sysconfdir=/etc --prefix=/usr --enable-perl --mandir=/usr/share/man

	# 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

	$(MAKE)

# 	Commented out because you have to be in X to build the docs, yuck
#	cd docs && $(MAKE)
#	cd ..
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp debian/shlibs.local debian/libgimp$(major).shlibs
	# Add here commands to clean up after the build process.
	-$(MAKE) clean
	-$(MAKE) distclean
	find . -name '*.o' -o -name '_libs' -o -name '*.lo' -o -name '*.a' -o -name '*.P' -o -name 'Makefile' | xargs rm -rf
	rm -f app/gimp
	rm -f plug-ins/perl/po/*.gmo
	dh_clean

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

# Build architecture-dependent files here.
binary-arch: build install gimp$(major)-nonfree gimp$(major)-perl gimp$(major) libgimp$(major)-dev libgimp$(major)

install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs usr/share/pixmaps
	##$(MAKE) prefix=$(shell pwd)/debian/tmp/usr PREFIX=$(shell pwd)/debian/tmp/usr INSTALLDIRS=perl INSTALLMAN1DIR=$(shell pwd)/debian/tmp/usr/share/man/man1 INSTALLMAN3DIR=$(shell pwd)/debian/tmp/usr/share/man/man3 DESTDIR=$(shell pwd)/debian/tmp/ install
	$(MAKE) prefix=$(shell pwd)/debian/tmp/usr mandir=$(shell pwd)/debian/tmp/usr/share/man PREFIX=$(shell pwd)/debian/tmp/usr gimpsysconfdir=$(shell pwd)/debian/tmp/etc/gimp/$(major) INSTALLDIRS=perl INSTALLMAN1DIR=$(shell pwd)/debian/tmp/usr/share/man/man1 INSTALLMAN3DIR=$(shell pwd)/debian/tmp/usr/share/man/man3 INSTALLMAN5DIR=$(shell pwd)/debian/tmp/usr/share/man/man5 install
	-find debian/tmp/usr -name perllocal.pod -exec rm -f {} \;
	find debian/tmp -type f -name .packlist | xargs rm -f
	-find debian/tmp/usr/lib/perl5 -type d -empty | xargs rmdir -p
	-mv debian/tmp/usr/man/man1/* debian/tmp/usr/share/man/man1/
	-mv debian/tmp/usr/man/man5/* debian/tmp/usr/share/man/man5/
	rm -f debian/shlibs.local debian/libgimp$(major).shlibs

	# These are the safe way, but the wrong way, to make shlib deps
	#echo "libgimp-$(major) $(minor) libgimp$(major) (>= $(version))" > debian/shlibs.local
	#echo "libgck-$(major) $(minor) libgimp$(major) (>= $(version))" >> debian/shlibs.local
	#echo "libgimpui-$(major) $(minor) libgimp$(major) (>= $(version))" >> debian/shlibs.local

	# This way is correct, but only seems to work on woody
	# (we don't need this now that gimp's at 1.2, but 1.3 will need it)
	#echo "libgimp-$(major) $(minor) libgimp$(major) (>= $(version)), libgimp$(major) (<< $(next_version))" > debian/shlibs.local
	#echo "libgck-$(major) $(minor) libgimp$(major) (>= $(version)), libgimp$(major) (<< $(next_version))" >> debian/shlibs.local
	#echo "libgimpui-$(major) $(minor) libgimp$(major) (>= $(version)), libgimp$(major) (<< $(next_version))" >> debian/shlibs.local

	echo "libgimp-$(major) $(minor) libgimp$(major) (>= $(version))" > debian/shlibs.local
	echo "libgck-$(major) $(minor) libgimp$(major) (>= $(version))" >> debian/shlibs.local
	echo "libgimpui-$(major) $(minor) libgimp$(major) (>= $(version))" >> debian/shlibs.local

	cp debian/shlibs.local debian/libgimp$(major).shlibs

# you are very naughty, helix peoples, this is already in the shlibs for aalib
# I can guess you're just hacking in slink support, so I forgive you
#	echo "libaa 1 aalib1 (>= 1.2)" >> debian/shlibs.local
	cp pixmaps/wilber.xpm debian/tmp/usr/share/pixmaps

	touch install-stamp

libgimp$(major): build install
	dh_testdir -plibgimp$(major) 
	dh_testroot -plibgimp$(major)
	dh_installdirs -plibgimp$(major)
	rm -rf debian/tmp/usr/bin debian/tmp/usr/include \
	  debian/tmp/usr/share debian/tmp/usr/lib/*.la debian/tmp/usr/man debian/tmp/usr/lib/gimp debian/tmp/usr/lib/perl5 debian/tmp/etc
	dh_installdocs -plibgimp$(major)
	dh_installchangelogs -plibgimp$(major)
	dh_strip -plibgimp$(major)
	dh_compress -plibgimp$(major)
	dh_fixperms -plibgimp$(major)
	dh_installdeb -plibgimp$(major)
	dh_shlibdeps -plibgimp$(major)
	dh_gencontrol -plibgimp$(major)
	dh_md5sums -plibgimp$(major)
	dh_builddeb -plibgimp$(major)

gimp$(major): build install
	dh_testdir -pgimp$(major) 
	dh_testroot -pgimp$(major)
	dh_clean -k -pgimp$(major) 
	dh_installdirs -pgimp$(major)
	-dh_movefiles -pgimp$(major)
	-rm -f install-stamp
	dh_installdocs -pgimp$(major) debian/README.debian docs/*.txt \
	  README docs/Wilber.xcf.gz
	dh_installchangelogs -pgimp$(major) ChangeLog
	dh_strip -pgimp$(major)
	dh_compress -pgimp$(major)
	dh_fixperms -pgimp$(major)
	dh_installmenu -pgimp$(major)
	dh_installdeb -pgimp$(major)
	LD_LIBRARY_PATH=$(shell pwd)/debian/tmp/usr/lib dh_shlibdeps -pgimp$(major)
	dh_gencontrol -pgimp$(major)
	dh_md5sums -pgimp$(major)
	dh_builddeb -pgimp$(major)

gimp$(major)-perl: build install
	dh_testdir -pgimp$(major)-perl
	dh_testroot -pgimp$(major)-perl
	dh_clean -pgimp$(major)-perl -k
	dh_installdirs -pgimp$(major)-perl usr/lib/perl5 /usr/lib/gimp/$(major)/plug-ins
	dh_movefiles -pgimp$(major)-perl
	for i in `file debian/tmp/usr/lib/gimp/$(major)/plug-ins/* | grep perl | cut -d : -f 1`; \
	do \
		mv $$i debian/gimp$(major)-perl/usr/lib/gimp/$(major)/plug-ins ; \
	done

	-rm -f install-stamp
	dh_perl -pgimp$(major)-perl usr/lib/perl5 usr/lib/gimp
	dh_installdocs -pgimp$(major)-perl  
	dh_undocumented -pgimp$(major)-perl
	dh_installchangelogs -pgimp$(major)-perl
	dh_strip -pgimp$(major)-perl
	dh_compress -pgimp$(major)-perl
	dh_fixperms -pgimp$(major)-perl
	dh_installdeb -pgimp$(major)-perl
	LD_LIBRARY_PATH=$(shell pwd)/debian/tmp/usr/lib dh_shlibdeps -pgimp$(major)-perl
	dh_gencontrol -pgimp$(major)-perl
	dh_md5sums -pgimp$(major)-perl
	dh_builddeb -pgimp$(major)-perl

gimp$(major)-nonfree: build install
	dh_testdir -pgimp$(major)-nonfree
	dh_testroot -pgimp$(major)-nonfree
	dh_clean -k -pgimp$(major)-nonfree
	dh_movefiles -pgimp$(major)-nonfree
	-rm -f install-stamp
	dh_installdirs -pgimp$(major)-nonfree
	dh_installdocs -pgimp$(major)-nonfree
	dh_installchangelogs -pgimp$(major)-nonfree
	dh_strip -pgimp$(major)-nonfree
	dh_compress -pgimp$(major)-nonfree
	dh_fixperms -pgimp$(major)-nonfree
	dh_installdeb -pgimp$(major)-nonfree
	LD_LIBRARY_PATH=$(shell pwd)/debian/tmp/usr/lib dh_shlibdeps -pgimp$(major)-nonfree
	dh_gencontrol -pgimp$(major)-nonfree
	dh_md5sums -pgimp$(major)-nonfree
	dh_builddeb -pgimp$(major)-nonfree

libgimp$(major)-dev: build install
	dh_testdir -plibgimp$(major)-dev
	dh_testroot -plibgimp$(major)-dev
	dh_clean -plibgimp$(major)-dev -k
	dh_installdirs -plibgimp$(major)-dev
	dh_movefiles -plibgimp$(major)-dev
	-rm -f install-stamp
	dh_installdocs -plibgimp$(major)-dev  
	dh_link -plibgimp$(major)-dev usr/share/man/man1/gimptool.1 usr/share/man/man1/gimp-config.1
	dh_installchangelogs -plibgimp$(major)-dev
	dh_strip -plibgimp$(major)-dev
	dh_compress -plibgimp$(major)-dev
	dh_fixperms -plibgimp$(major)-dev
	dh_installdeb -plibgimp$(major)-dev
	dh_shlibdeps -plibgimp$(major)-dev
	dh_gencontrol -plibgimp$(major)-dev
	dh_md5sums -plibgimp$(major)-dev
	dh_builddeb -plibgimp$(major)-dev

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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