#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

TARGETDIR=./debian/nmh

export DEB_CFLAGS_MAINT_APPEND="-Wall"
export CFLAGS:= -Wall $(shell  dpkg-buildflags --get CFLAGS)
export CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

build-stamp: 
	dh_testdir
	dh_autoreconf
	./configure \
	  --prefix=                              \
	  --bindir='$${prefix}/usr/bin/mh'       \
	  --sysconfdir='$${prefix}/etc/nmh'      \
	  --libdir='$${prefix}/usr/lib/mh'       \
	  --mandir='$${prefix}/usr/share/man'    \
	  --docdir='$${prefix}/usr/share/doc/nmh' \
	  --with-tls \
	  --with-mts=sendmail/pipe \
	  --with-cyrus-sasl
	$(MAKE) sendmailpath=/usr/sbin/sendmail
	touch build-stamp                    	

build: build-stamp
build-arch: build
build-indep:

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	# generated file shouldn't end up in the debian diff
	rm -f sbr/dtimep.c
	rm -f config.log
	dh_autoreconf_clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	make install prefix=$(TARGETDIR)

# run the check *after* putting everything in its place,
# so that the required components and other files are found 
# by the tests.
	[ -n "$(filter nocheck,$(DEB_BUILD_OPTIONS))" ] || $(MAKE) check

# upstream install file isn't wanted
	rm -f $(TARGETDIR)/usr/share/doc/nmh/INSTALL
# ditto for rpm build script and spec file
	rm -f $(TARGETDIR)/usr/share/doc/nmh/contrib/build_nmh $(TARGETDIR)/usr/share/doc/nmh/contrib/nmh.spec

# now install the mime-support-aware mhn.defaults
	$(INSTALL_FILE) debian/mhn.defaults $(TARGETDIR)/etc/nmh/

# install bash-completions (newer location)
	$(INSTALL_DIR) $(TARGETDIR)/usr/share/bash-completion/completions
	$(INSTALL_FILE) debian/nmh.bash_completions $(TARGETDIR)/usr/share/bash-completion/completions/nmh

# don't want to fiddle with makefile.in too much, so i fix things here:
# install-mh manpage is in wrong section
	mv $(TARGETDIR)/usr/share/man/man1/install-mh.1mh $(TARGETDIR)/usr/share/man/man8/install-mh.8mh

binary-indep: build install
# nothing to do

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary:	binary-arch

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