#!/usr/bin/make -f

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

CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

CFLAGS += -Wall -g -D_FORTIFY_SOURCE=2
LDFLAGS += -Wl,--as-needed -Wl,-z,now

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

PLUGINSDIR = $(CURDIR)/debian/claws-mail/usr/lib/claws-mail/plugins

fix-config:
	for cfg in sub guess ; \
	do mv config.$$cfg u.config.$$cfg ; \
	   ln -s /usr/share/misc/config.$$cfg ; \
	done
	touch fix-config

clean-fix-config:
	for cfg in sub guess ; \
	do rm -f config.$$cfg ; \
	   [ ! -f u.config.$$cfg ] || mv u.config.$$cfg config.$$cfg ; \
	done
	rm -f fix-config

config.status: fix-config configure
	dh_testdir
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--enable-gpgme \
		--enable-ipv6 \
		--enable-aspell \
		--enable-jpilot \
		--enable-ldap \
		--enable-spamassassin-plugin=yes \
		--disable-dillo-viewer-plugin \
		--with-manualdir=\$${prefix}/share/doc/claws-mail \
		--with-config-dir=.claws-mail

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp: config.status
	dh_testdir
	$(MAKE) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
	touch build-stamp

clean: clean-fix-config
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	$(MAKE) -C debian/manpages clean
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep 
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/claws-mail
	$(MAKE) -C debian/manpages
	# libclaws-mail-dev
	mkdir -m 755 -p $(CURDIR)/debian/libclaws-mail-dev/usr/lib/pkgconfig
	mv $(CURDIR)/debian/claws-mail/usr/include \
	    $(CURDIR)/debian/libclaws-mail-dev/usr/include
	mv $(CURDIR)/debian/claws-mail/usr/lib/pkgconfig/claws-mail.pc \
	   $(CURDIR)/debian/libclaws-mail-dev/usr/lib/pkgconfig
	# claws-mail-i18n
	mkdir -m 755 -p $(CURDIR)/debian/claws-mail-i18n/usr/share/locale
	mv $(CURDIR)/debian/claws-mail/usr/share/locale/* \
	   $(CURDIR)/debian/claws-mail-i18n/usr/share/locale
	rmdir $(CURDIR)/debian/claws-mail/usr/share/locale
	# claws-mail-doc 
	for TR in "en" "fr" "pl" "es" "de"; \
	do mv $(CURDIR)/debian/claws-mail/usr/share/doc/claws-mail/$$TR \
	      $(CURDIR)/debian/claws-mail-doc/usr/share/doc/claws-mail/; \
	done 
	# claws-mail-spamassassin
	mkdir -m 755 -p \
	     $(CURDIR)/debian/claws-mail-spamassassin/usr/lib/claws-mail/plugins
	mv $(PLUGINSDIR)/spamassassin.so \
	   $(CURDIR)/debian/claws-mail-spamassassin/usr/lib/claws-mail/plugins
	# claws-mail-trayicon
	mkdir -m 755 -p \
	      $(CURDIR)/debian/claws-mail-trayicon/usr/lib/claws-mail/plugins
	mv $(PLUGINSDIR)/trayicon.so \
	   $(CURDIR)/debian/claws-mail-trayicon/usr/lib/claws-mail/plugins
	# claws-mail-pgpmime
	mkdir -m 755 -p \
	      $(CURDIR)/debian/claws-mail-pgpmime/usr/lib/claws-mail/plugins
	mv $(PLUGINSDIR)/pgpmime.so \
	   $(PLUGINSDIR)/pgpcore.so \
	   $(PLUGINSDIR)/pgpmime.deps \
	   $(CURDIR)/debian/claws-mail-pgpmime/usr/lib/claws-mail/plugins
	# claws-mail-pgpinline
	mkdir -m 755 -p \
	      $(CURDIR)/debian/claws-mail-pgpinline/usr/lib/claws-mail/plugins
	mv $(PLUGINSDIR)/pgpinline.so \
	   $(PLUGINSDIR)/pgpinline.deps \
	   $(CURDIR)/debian/claws-mail-pgpinline/usr/lib/claws-mail/plugins
	# claws-mail-bogofilter
	mkdir -m 755 -p \
	      $(CURDIR)/debian/claws-mail-bogofilter/usr/lib/claws-mail/plugins
	mv $(PLUGINSDIR)/bogofilter.so \
	   $(CURDIR)/debian/claws-mail-bogofilter/usr/lib/claws-mail/plugins
	# claws-mail-smime-plugin
	mkdir -m 755 -p \
	      $(CURDIR)/debian/claws-mail-smime-plugin/usr/lib/claws-mail/plugins
	mv $(PLUGINSDIR)/smime.so \
	   $(CURDIR)/debian/claws-mail-smime-plugin/usr/lib/claws-mail/plugins
	# claws-mail-tools
	mkdir -m 755 -p \
	      $(CURDIR)/debian/claws-mail-tools/usr/share/doc/claws-mail/tools
	cp -r $(CURDIR)/tools/* \
	      $(CURDIR)/debian/claws-mail-tools/usr/share/doc/claws-mail/tools
	# claws-mail
	rm -rf $(CURDIR)/debian/claws-mail/usr/share/claws-mail
	rm -rf $(CURDIR)/debian/claws-mail/usr/share/doc/claws-mail/manual/
	rm -rf $(CURDIR)/debian/claws-mail/usr/lib
	rm -f $(CURDIR)/debian/claws-mail/usr/bin/sylpheed-claws
	# application icons 
	mkdir -m 755 -p \
	   $(CURDIR)/debian/claws-mail/usr/share/pixmaps/
	cp $(CURDIR)/debian/claws-mail-32x32.xpm \
	   $(CURDIR)/debian/claws-mail/usr/share/pixmaps/
	cp $(CURDIR)/claws-mail*.png \
	   $(CURDIR)/debian/claws-mail/usr/share/pixmaps/

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i -k ChangeLog
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_installmenu -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a -k ChangeLog
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_installmenu -a
	dh_link -a
	dh_strip --dbg-package=claws-mail-dbg
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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

