#!/usr/bin/make -f

CFLAGS = -Wall -g

NS := claws-mail
CLAWS_VERSION = $(shell /bin/bash $(CURDIR)/debian/get-claws-ver.sh $(CURDIR))
CLAWS_LAST = $(shell /bin/bash $(CURDIR)/debian/get-claws-last.sh $(CURDIR))

IDIR    := $(CURDIR)/debian
PDIR    := $(CURDIR)/debian/manpages
GENDOCS := $(CURDIR)/debian/gendocs
GENXAMP := $(CURDIR)/debian/genexamples

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

configure-stamp:
	dh_testdir
	cp $(IDIR)/configure $(IDIR)/Makefile $(CURDIR)
	chmod +x $(CURDIR)/configure
	$(CURDIR)/configure --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info
	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	$(MAKE)
	docbook-to-man $(PDIR)/$(NS)-extra-plugins.sgml \
		> $(PDIR)/$(NS)-extra-plugins.1
	# _______________________________________(vcalendar-plugin)____
	docbook-to-man $(PDIR)/$(NS)-vcalendar-plugin.sgml \
		> $(PDIR)/$(NS)-vcalendar-plugin.1
	# ____________________________________________(perl-filter)____
	pod2man $(CURDIR)/perl_plugin-*/cm_perl.pod \
	        > $(PDIR)/$(NS)-perl-filter.1
	pod2man $(PDIR)/matcherrc2perlfilter.pl.pod \
	        > $(PDIR)/matcherrc2perlfilter.1
	# ___________________________________________(feeds-reader)____
	docbook-to-man $(PDIR)/$(NS)-feeds-reader.sgml \
		> $(PDIR)/$(NS)-feeds-reader.1
	# ________________________________________(mailmbox-plugin)____
	docbook-to-man $(PDIR)/$(NS)-mailmbox-plugin.sgml \
		> $(PDIR)/$(NS)-mailmbox-plugin.1
	# __________________________________________(acpi-notifier)____
	docbook-to-man $(PDIR)/$(NS)-acpi-notifier.sgml \
		> $(PDIR)/$(NS)-acpi-notifier.1
	# _________________________________________(attach-remover)____
	docbook-to-man $(PDIR)/$(NS)-attach-remover.sgml \
		> $(PDIR)/$(NS)-attach-remover.1
	# _______________________________________(fetchinfo-plugin)____
	docbook-to-man $(PDIR)/$(NS)-fetchinfo-plugin.sgml \
		> $(PDIR)/$(NS)-fetchinfo-plugin.1
	# ___________________________________________(html2-viewer)____
	docbook-to-man $(PDIR)/$(NS)-html2-viewer.sgml \
		> $(PDIR)/$(NS)-html2-viewer.1
	# _________________________________________(newmail-plugin)____
	docbook-to-man $(PDIR)/$(NS)-newmail-plugin.sgml \
		> $(PDIR)/$(NS)-newmail-plugin.1
	# _________________________________________(multi-notifier)____
	docbook-to-man $(PDIR)/$(NS)-multi-notifier.sgml \
		> $(PDIR)/$(NS)-multi-notifier.1
	# __________________________________________(attach-warner)____
	docbook-to-man $(PDIR)/$(NS)-attach-warner.sgml \
	        > $(PDIR)/$(NS)-attach-warner.1
	# ____________________________________________(spam-report)____
	docbook-to-man $(PDIR)/$(NS)-spam-report.sgml \
		> $(PDIR)/$(NS)-spam-report.1
	# ____________________________________________(tnef-parser)____
	docbook-to-man $(PDIR)/$(NS)-tnef-parser.sgml \
	        > $(PDIR)/$(NS)-tnef-parser.1
	# ________________________________________(archiver-plugin)____
	docbook-to-man $(PDIR)/$(NS)-archiver-plugin.sgml \
	        > $(PDIR)/$(NS)-archiver-plugin.1
	# ________________________________________(bsfilter-plugin)____
	docbook-to-man $(PDIR)/$(NS)-bsfilter-plugin.sgml \
		> $(PDIR)/$(NS)-bsfilter-plugin.1
	# ___________________________________________(fancy-plugin)____
	docbook-to-man $(PDIR)/$(NS)-fancy-plugin.sgml \
		> $(PDIR)/$(NS)-fancy-plugin.1
	# __________________________________________(python-plugin)____
	docbook-to-man $(PDIR)/$(NS)-python-plugin.sgml \
		> $(PDIR)/$(NS)-python-plugin.1
	# _____________________________________(geolocation-plugin)____
	docbook-to-man $(PDIR)/$(NS)-geolocation-plugin.sgml \
		> $(PDIR)/$(NS)-geolocation-plugin.1
	# _________________________________________(address-keeper)____
	docbook-to-man $(PDIR)/$(NS)-address-keeper.sgml \
		> $(PDIR)/$(NS)-address-keeper.1
	# ___________________________________________(clamd-plugin)____
	docbook-to-man $(PDIR)/$(NS)-clamd-plugin.sgml \
		> $(PDIR)/$(NS)-clamd-plugin.1
	touch build-stamp

clean: clean-docs clean-examples
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	[ ! -f Makefile ] || $(MAKE) clean
	-$(CURDIR)/configure --unconfigure
	rm -f $(CURDIR)/configure $(CURDIR)/Makefile
	# clean installdirs
	rm -rf $(IDIR)/vcalendar-* \
	       $(IDIR)/perl_plugin-* \
	       $(IDIR)/rssyl-* \
	       $(IDIR)/mailmbox-* \
	       $(IDIR)/gtkhtml2_viewer-* \
	       $(IDIR)/acpi_notifier-* \
	       $(IDIR)/att_remover-* \
	       $(IDIR)/fetchinfo-plugin-* \
	       $(IDIR)/newmail-* \
	       $(IDIR)/notification_plugin-* \
	       $(IDIR)/attachwarner-* \
	       $(IDIR)/spam_report-* \
	       $(IDIR)/tnef_parse-* \
	       $(IDIR)/archive-* \
	       $(IDIR)/bsfilter_plugin-* \
	       $(IDIR)/fancy-* \
	       $(IDIR)/python_plugin-* \
	       $(IDIR)/geolocation_plugin-* \
	       $(IDIR)/address_keeper-* \
	       $(IDIR)/clamd-*
	# clean generated manpages
	rm -f $(PDIR)/$(NS)-extra-plugins.1 \
	      $(PDIR)/$(NS)-vcalendar-plugin.1 \
	      $(PDIR)/$(NS)-perl-filter.1 \
	      $(PDIR)/matcherrc2perlfilter.1 \
	      $(PDIR)/$(NS)-feeds-reader.1 \
	      $(PDIR)/$(NS)-mailmbox-plugin.1 \
	      $(PDIR)/$(NS)-acpi-notifier.1 \
	      $(PDIR)/$(NS)-attach-remover.1 \
	      $(PDIR)/$(NS)-fetchinfo-plugin.1 \
	      $(PDIR)/$(NS)-html2-viewer.1 \
	      $(PDIR)/$(NS)-newmail-plugin.1 \
	      $(PDIR)/$(NS)-multi-notifier.1 \
	      $(PDIR)/$(NS)-attach-warner.1 \
	      $(PDIR)/$(NS)-spam-report.1 \
	      $(PDIR)/$(NS)-tnef-parser.1 \
	      $(PDIR)/$(NS)-archiver-plugin.1 \
	      $(PDIR)/$(NS)-bsfilter-plugin.1 \
	      $(PDIR)/$(NS)-fancy-plugin.1 \
	      $(PDIR)/$(NS)-python-plugin.1 \
	      $(PDIR)/$(NS)-geolocation-plugin.1 \
	      $(PDIR)/$(NS)-address-keeper.1 \
	      $(PDIR)/$(NS)-clamd-plugin.1
	dh_clean 

install:
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) install DESTDIR=$(IDIR)
	# _______________________________________[vcalendar-plugin]____
	mv $(IDIR)/vcalendar-*/* \
	   $(IDIR)/$(NS)-vcalendar-plugin
	# remove libical rests...
	rm -rf $(IDIR)/$(NS)-vcalendar-plugin/usr/include
	# ...and unwanted files
	rm -f $(IDIR)/$(NS)-vcalendar-plugin/usr/lib/$(NS)/plugins/*.*a
	# ____________________________________________[perl-filter]____
	mv $(IDIR)/perl_plugin-*/* \
	   $(IDIR)/$(NS)-perl-filter
	mkdir -p -m 755 $(IDIR)/$(NS)-perl-filter/usr/bin
	install -m 755 $(CURDIR)/perl_plugin-*/tools/matcherrc2perlfilter.pl \
	   $(IDIR)/$(NS)-perl-filter/usr/bin/matcherrc2perlfilter
	rm -f $(IDIR)/$(NS)-perl-filter/usr/lib/$(NS)/plugins/*.*a
	# ___________________________________________[feeds-reader]____
	mv $(IDIR)/rssyl-*/* \
	   $(IDIR)/$(NS)-feeds-reader
	rm -f $(IDIR)/$(NS)-feeds-reader/usr/lib/$(NS)/plugins/*.*a
	# ________________________________________[mailmbox-plugin]____
	mv $(IDIR)/mailmbox-*/* \
	   $(IDIR)/$(NS)-mailmbox-plugin
	rm -f $(IDIR)/$(NS)-mailmbox-plugin/usr/lib/$(NS)/plugins/*.*a
	# ___________________________________________[html2-viewer]____
	mv $(IDIR)/gtkhtml2_viewer-*/* \
	   $(IDIR)/$(NS)-html2-viewer
	rm -f $(IDIR)/$(NS)-html2-viewer/usr/lib/$(NS)/plugins/*.*a
	# __________________________________________[acpi-notifier]____
	mv $(IDIR)/acpi_notifier-*/* \
	   $(IDIR)/$(NS)-acpi-notifier
	rm -f $(IDIR)/$(NS)-acpi-notifier/usr/lib/$(NS)/plugins/*.*a
	# _________________________________________[attach-remover]____
	mv $(IDIR)/att_remover-*/* \
	   $(IDIR)/$(NS)-attach-remover
	rm -f $(IDIR)/$(NS)-attach-remover/usr/lib/$(NS)/plugins/*.*a
	# _______________________________________[fetchinfo-plugin]____
	mv $(IDIR)/fetchinfo-plugin-*/* \
	   $(IDIR)/$(NS)-fetchinfo-plugin
	rm -f $(IDIR)/$(NS)-fetchinfo-plugin/usr/lib/$(NS)/plugins/*.*a
	# _________________________________________[newmail-plugin]____
	mv $(IDIR)/newmail-*/* \
	   $(IDIR)/$(NS)-newmail-plugin
	rm -f $(IDIR)/$(NS)-newmail-plugin/usr/lib/$(NS)/plugins/*.*a
	# _________________________________________[multi-notifier]____
	mv $(IDIR)/notification_plugin-*/* \
	   $(IDIR)/$(NS)-multi-notifier
	rm -f $(IDIR)/$(NS)-multi-notifier/usr/lib/$(NS)/plugins/*.*a
	# __________________________________________[attach-warner]____
	mv $(IDIR)/attachwarner-*/* \
	   $(IDIR)/$(NS)-attach-warner
	rm -f $(IDIR)/$(NS)-attach-warner/usr/lib/$(NS)/plugins/*.*a
	# ____________________________________________[spam-report]____
	mv $(IDIR)/spam_report-*/* \
	   $(IDIR)/$(NS)-spam-report
	rm -f $(IDIR)/$(NS)-spam-report/usr/lib/$(NS)/plugins/*.*a
	# ____________________________________________[tnef-parser]____
	mv $(IDIR)/tnef_parse-*/* \
	   $(IDIR)/$(NS)-tnef-parser
	rm -f $(IDIR)/$(NS)-tnef-parser/usr/lib/$(NS)/plugins/*.*a
	# ________________________________________[archiver-plugin]____
	mv $(IDIR)/archive-*/* \
	   $(IDIR)/$(NS)-archiver-plugin
	rm -f $(IDIR)/$(NS)-archiver-plugin/usr/lib/$(NS)/plugins/*.*a
	# ________________________________________[bsfilter-plugin]____
	mv $(IDIR)/bsfilter_plugin-*/* \
	   $(IDIR)/$(NS)-bsfilter-plugin
	rm -f $(IDIR)/$(NS)-bsfilter-plugin/usr/lib/$(NS)/plugins/*.*a
	# ___________________________________________[fancy-plugin]____
	mv $(IDIR)/fancy-*/* \
	   $(IDIR)/$(NS)-fancy-plugin
	rm -f $(IDIR)/$(NS)-fancy-plugin/usr/lib/$(NS)/plugins/*.*a
	# __________________________________________[python-plugin]____
	mv $(IDIR)/python_plugin-*/* \
	   $(IDIR)/$(NS)-python-plugin
	rm -f $(IDIR)/$(NS)-python-plugin/usr/lib/$(NS)/plugins/*.*a
	# _____________________________________[geolocation-plugin]____
	mv $(IDIR)/geolocation_plugin-*/* \
	   $(IDIR)/$(NS)-geolocation-plugin
	rm -f $(IDIR)/$(NS)-geolocation-plugin/usr/lib/$(NS)/plugins/*.*a
	# _________________________________________[address-keeper]____
	mv $(IDIR)/address_keeper-*/* \
	   $(IDIR)/$(NS)-address-keeper
	rm -f $(IDIR)/$(NS)-address-keeper/usr/lib/$(NS)/plugins/*.*a
	# ___________________________________________[clamd-plugin]____
	mv $(IDIR)/clamd-*/* \
	   $(IDIR)/$(NS)-clamd-plugin
	rm -f $(IDIR)/$(NS)-clamd-plugin/usr/lib/$(NS)/plugins/*.*a

generate-examples:
	chmod +x $(GENXAMP)
	$(GENXAMP) $(NS)-python-plugin python_plugin-*

clean-examples:
	chmod +x $(GENXAMP)
	$(GENXAMP) $(NS)-python-plugin --clean

generate-docs:
	chmod +x $(GENDOCS)
	$(GENDOCS) $(NS)-vcalendar-plugin vcalendar-*
	$(GENDOCS) $(NS)-perl-filter      perl_plugin-*
	$(GENDOCS) $(NS)-feeds-reader     rssyl-*
	$(GENDOCS) $(NS)-mailmbox-plugin  mailmbox-*
	$(GENDOCS) $(NS)-html2-viewer     gtkhtml2_viewer-*
	$(GENDOCS) $(NS)-acpi-notifier    acpi_notifier-*
	$(GENDOCS) $(NS)-attach-remover   att_remover-*
	$(GENDOCS) $(NS)-fetchinfo-plugin fetchinfo-plugin-*
	$(GENDOCS) $(NS)-newmail-plugin   newmail-*
	$(GENDOCS) $(NS)-multi-notifier   notification_plugin-*
	$(GENDOCS) $(NS)-attach-warner    attachwarner-*
	$(GENDOCS) $(NS)-spam-report      spam_report-*
	$(GENDOCS) $(NS)-tnef-parser      tnef_parse-*
	$(GENDOCS) $(NS)-archiver-plugin  archive-*
	$(GENDOCS) $(NS)-bsfilter-plugin  bsfilter_plugin-*
	$(GENDOCS) $(NS)-fancy-plugin     fancy-*
	$(GENDOCS) $(NS)-python-plugin    python_plugin-*
	$(GENDOCS) $(NS)-geolocation-plugin geolocation_plugin-*
	$(GENDOCS) $(NS)-address-keeper   address_keeper-*
	$(GENDOCS) $(NS)-clamd-plugin     clamd-*

clean-docs:
	chmod +x $(GENDOCS)
	$(GENDOCS) $(NS)-vcalendar-plugin --clean
	$(GENDOCS) $(NS)-perl-filter      --clean
	$(GENDOCS) $(NS)-feeds-reader     --clean
	$(GENDOCS) $(NS)-mailmbox-plugin  --clean
	$(GENDOCS) $(NS)-html2-viewer     --clean
	$(GENDOCS) $(NS)-acpi-notifier    --clean
	$(GENDOCS) $(NS)-attach-remover   --clean
	$(GENDOCS) $(NS)-fetchinfo-plugin --clean
	$(GENDOCS) $(NS)-newmail-plugin   --clean
	$(GENDOCS) $(NS)-multi-notifier   --clean 
	$(GENDOCS) $(NS)-attach-warner    --clean
	$(GENDOCS) $(NS)-spam-report      --clean
	$(GENDOCS) $(NS)-tnef-parser      --clean
	$(GENDOCS) $(NS)-archiver-plugin  --clean
	$(GENDOCS) $(NS)-bsfilter-plugin  --clean
	$(GENDOCS) $(NS)-fancy-plugin     --clean
	$(GENDOCS) $(NS)-python-plugin    --clean
	$(GENDOCS) $(NS)-geolocation-plugin --clean
	$(GENDOCS) $(NS)-address-keeper   --clean
	$(GENDOCS) $(NS)-clamd-plugin     --clean

binary-indep: generate-docs generate-examples

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installchangelogs \
		-p$(NS)-acpi-notifier     acpi_notifier-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-attach-remover    att_remover-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-feeds-reader      rssyl-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-fetchinfo-plugin  fetchinfo-plugin-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-html2-viewer      gtkhtml2_viewer-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-mailmbox-plugin   mailmbox-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-multi-notifier    notification_plugin-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-newmail-plugin    newmail-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-perl-filter       perl_plugin-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-attach-warner     attachwarner-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-spam-report       spam_report-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-tnef-parser       tnef_parse-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-archiver-plugin   archive-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-bsfilter-plugin   bsfilter_plugin-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-fancy-plugin      fancy-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-python-plugin     python_plugin-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-geolocation-plugin geolocation_plugin-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-address-keeper    address_keeper-*/ChangeLog
	dh_installchangelogs \
		-p$(NS)-clamd-plugin      clamd-*/ChangeLog
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip --dbg-package=claws-mail-extra-plugins-dbg
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -VClaws-Version=$(CLAWS_VERSION) \
		-VClaws-Last=$(CLAWS_LAST)
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install generate-docs clean-docs generate-examples clean-examples

