#!/usr/bin/make -f
#
# Debian build makefile
#


DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

ifndef PERL
    PERL = /usr/bin/perl
endif

DEST=$(CURDIR)/debian/swish-e
DOCDIR=$(DEST)/usr/share/doc/swish-e

clean:
	dh_testdir
	dh_testroot
	dh_clean build.stamp
	-( cd perl; $(MAKE) realclean)
	-[ ! -f Makefile ] || $(MAKE) distclean
	rm -f perl/t/index.swish-e* perl/Makefile.old
	rm -f doc/swish-e.1 doc/swish-search.1 *.cgi.7
	rm -f src/acconfig.h doc/bin/toc_file config.log
	rm -f html/search.cgi debian/files
	rm -f SWISH-Stemmer-0.05.tar.gz

build-indep:
	# nothing

build-arch: build

build: build.stamp
build.stamp:
	dh_testdir
	dh_update_autotools_config
	env CC=gcc CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" ./configure --mandir=\$${prefix}/share/man --with-libxml2 --prefix=/usr
	$(MAKE)
	#$(MAKE) docs
	#(cd doc;$(MAKE) man)
	(chmod 755 swish-config;cd perl; env PATH=..:$$PATH SWISHSKIPTEST=1 SWISHIGNOREVER=1 $(PERL) Makefile.PL INSTALLDIRS=vendor LDDLFLAGS="$(LDFLAGS) -shared" < /dev/null \
		&& $(MAKE) OPTIMIZE="$(CFLAGS) -I../src/" CC=gcc LD=gcc LD_RUN_PATH=)
	> $@

binary: binary-arch binary-indep

binary-arch: build
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs 
	uudecode debian/SWISH-Stemmer-0.05.tar.gz.uu
	# bin
	make install DESTDIR=$(DEST)
	sed -i "/dependency_libs/ s/'.*'/''/" `find . -name '*.la'`
	# perl
	(cd perl;$(MAKE) install DESTDIR=$(DEST))
	# docs
	(cd example;pod2man -s 7 swish.cgi >../swish.cgi.7 ;pod2man -s 7 search.cgi >../search.cgi.7)
	dh_installman
	dh_installdocs README* 
	dh_installexamples example/* conf filter-bin prog-bin SWISH-Stemmer-0.05.tar.gz
	# swish-search symlinks
	(cd $(DEST)/usr/bin; ln -s swish-e swish-search)
	(cd $(DEST)/usr/share/man/man1; ln -s swish-e.1 swish-search.1)	
	# let's make lintian happy
	mv $(DEST)/usr/bin/swish-filter-test $(DOCDIR)/examples/
	find $(DOCDIR)/examples -name '*' -exec chmod 644 {} \;
	find $(DOCDIR)/examples -name '*.in' -exec rm {} \;
	find $(DEST)/usr/lib/swish-e/perl -name '*.pm' -exec chmod 644 {} \;
	cp -f debian/swish_filter.pl $(DOCDIR)/examples/filter-bin/
	chmod 755 $(DOCDIR)/examples/filter-bin/*.pl
	chmod 755 $(DOCDIR)/examples/filter-bin/*.sh
	rm -f $(DOCDIR)/README.cvs $(DOCDIR)/INSTALL
	rm -f $(DOCDIR)/examples/prog-bin/*.in $(DOCDIR)/examples/prog-bin/Make*
	rm -f $(DOCDIR)/examples/*.in $(DOCDIR)/examples/Make*
	rm -f $(DOCDIR)/filter-bin/Make* $(DOCDIR)/conf/Make*
	#rm -rf $(DESTDIR)/usr/share/swish-e
	#
	dh_movefiles --sourcedir=debian/swish-e
	dh_installchangelogs
	dh_strip
	dh_compress -Xexamples
	dh_installdebconf
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_perl
	dh_shlibdeps -l$(DEST)/usr/lib/
	dh_gencontrol -a
	dh_md5sums
	dh_builddeb

binary-indep: build

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