#!/usr/bin/make -f
#------------------------------------------------------------------------------
# vim: syntax=make
#
# $Sendmail: rules,v 8.14.1  2007-05-27 18:30:00 cowboy Exp $
#------------------------------------------------------------------------------

# CDBS crud
#include /usr/share/cdbs/1/class/makefile.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk
# Overridden below, but needed now
#DEB_TAR_SRCDIR := .
#include /usr/share/cdbs/1/rules/tarball.mk

# Determine architecture
DEB_HOST_GNU_CPU ?= $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)

# Make it even harder to exploit sendmail.
# Well, almost impossible now 8-)
# * Compile all with -fPIC (works for pic or pie objects)
# * Link with either -pie or -shared
dpkg_buildflags = DEB_BUILD_MAINT_OPTIONS="hardening=+all,-pie" dpkg-buildflags
CFLAGS   := $(shell $(dpkg_buildflags) --get CFLAGS)
CFLAGS   += -fPIC
ifneq (,$(filter -fstack-protector,$(CFLAGS)))
	CFLAGS += -fstack-protector-all
	endif
CXXFLAGS := $(shell $(dpkg_buildflags) --get CXXFLAGS)
CPPFLAGS := $(shell $(dpkg_buildflags) --get CPPFLAGS)
LDFLAGS  := $(shell $(dpkg_buildflags) --get LDFLAGS)
LDFLAGS  += -Wl,-z,noexecstack -Wl,--warn-shared-textrel
LDFLAGS  += -fpie
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

# Obtain package/version information from the path (debian/changelog is walkabout)
ifneq (y, $(shell test -f debian/changelog && echo 'y'))
	DIRNAME = $(shell basename $(CURDIR))
	DEB_SOURCE_PACKAGE = $(shell expr "$(DIRNAME)" : '\(.*\)-')
	DEB_VERSION = $(shell expr "$(DIRNAME)" : '.*-\(.*\)')
	DEB_NOEPOCH_VERSION = $(shell echo $(DEB_VERSION) | cut -d: -f2-)
	DEB_UPSTREAM_VERSION = $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')
	endif

# strip of a optionally added data (ie: upstream+cvs-debian)
DEB_UPSTREAM_TARBALL_VERSION := $(shell echo $(DEB_UPSTREAM_VERSION) | sed 's/+[^-]*//')
DEB_TAR_SRCDIR := $(DEB_SOURCE_PACKAGE)-$(DEB_UPSTREAM_TARBALL_VERSION)
DEB_SRCDIR := build-tree/$(DEB_TAR_SRCDIR)
DEB_BUILDDIR := $(DEB_SRCDIR)
DEB_DESTDIR = $(CURDIR)/${PKG_TMP}
DEB_DBG_PACKAGES = libmilter$(sm_libmilter_version)-dbg
DEB_DBG_PACKAGE_libmilter$(sm_libmilter_version) = libmilter$(sm_libmilter_version)-dbg
DEB_DH_INSTALL_CRON_ARGS = --name=$(DEB_SOURCE_PACKAGE)
DEB_DH_INSTALLINIT_ARGS  = --name=$(DEB_SOURCE_PACKAGE)
DEB_UPDATE_RCD_PARAMS    = --update-rcd-params="defaults 21 19"
# autoconf/make are mine, not upstream (yet) :(
DEB_CONFIGURE_LIBEXECDIR ="\$${prefix}/lib/sm.bin"
DEB_AC_AUX_DIR = $(CURDIR)/debian/build
DEB_CONFIGURE_SCRIPT = $(CURDIR)/debian/configure
DEB_CONFIGURE_INVOKE = cd $(CURDIR)/debian \
	&& $(DEB_CONFIGURE_SCRIPT_ENV) $(DEB_CONFIGURE_SCRIPT) $(DEB_CONFIGURE_NORMAL_ARGS)
DEB_MAKE_INVOKE =
DEB_MAKE_BUILD_TARGET =
DEB_MAKE_INSTALL_TARGET =
DEB_INSTALL_CHANGELOGS_ALL = $(DEB_SRCDIR)/RELEASE_NOTES
DEB_DH_BUGFILES_ARGS_DEFAULT = -A

version_string := ${DEB_VERSION}
version_full   := $(shell expr "${version_string}" : '\([^-]*\)')
version_debian := $(shell expr "${version_string}" : '.*\(-.*\)$$')
version		   := ${version_full}
version_major  := $(shell expr "${version_full}" : '\([0-9]*\.[0-9]*\)')
version_minor  := $(shell expr "${version_full}" : '[0-9]*\.[0-9]*\.\(.*\)')
ifneq (, $(findstring +,${version_minor}))
	beta_version := $(shell expr "${version_full}" : '.*[+~]\([0-9.].*\)')
	beta_major := $(shell expr "${beta_version}" : '\([0-9]*\.[0-9]*\)')
	beta_minor := $(shell expr "${beta_version}" : '[0-9]*\.[0-9]*\.\(.*\)')
	ifneq (, $(beta_version))
		version        := ${beta_version}
		version_major  := ${beta_major}
		version_minor  := ${beta_minor}
		endif
	endif

# Some variables that should be removed in favour of DEB_* above
SRC_DIR    := $(CURDIR)/debian
PKG_DIR    := debian/${DEB_SOURCE_PACKAGE}-bin
PKG_DOC     = debian/${DEB_SOURCE_PACKAGE}-doc${docdir}/${DEB_SOURCE_PACKAGE}-doc
SM_SUBDIRS := editmap libmilter mail.local mailstats \
		makemap praliases rmail sendmail smrsh vacation
PKG_TMP	 = debian/tmp

# We want delayed interpretation of the options !
ifeq (y, $(shell test -x /usr/bin/debuild && echo 'y'))
	DEB_BUILD = \
		DEB_BUILD_OPTIONS="${DEB_BUILD_OPTIONS}" \
		debuild
else
	DEB_BUILD = \
		DEB_BUILD_OPTIONS="${DEB_BUILD_OPTIONS}" \
		dpkg-buildpackage
	endif

# Now that all that is out of the way, read the desired configuration
# settings
CONF_NAME = debian/configure.options
$(eval CONFIG_OPTS := $(shell grep -v "^\#" ${CONF_NAME}))
DEB_CONFIGURE_EXTRA_FLAGS = $(CONFIG_OPTS)

# Include Debian Autoconf settings
ifeq (y, $(shell test -e debian/build/autoconf.mk && echo 'y'))
	include debian/build/autoconf.mk
else
	ifeq (y, $(shell test -e build/autoconf.mk && echo 'y'))
		include build/autoconf.mk
	else
		include debian/build/autoconf.mk.in
		endif
	endif

include /usr/share/cdbs/1/rules/tarball.mk
include /usr/share/cdbs/1/rules/patchsys-quilt.mk
DEB_PATCHDIRS = $(CURDIR)/debian/patches/${version_major}/${version_full}


#-------------
# Normalize CDBS rules
.PHONY: unpack
unpack:: pre-build
.PHONY: configure
configure:: common-configure-arch common-configure-indep


#-------------
# Maintainer only rule... refresh debian/configure, debian/*/Makefile.in, and debian/control
AUTOFOODUMMY	 = debian/AUTHORS debian/COPYING debian/ChangeLog debian/INSTALL debian/NEWS debian/README
AUTOMAKEARGS	?= --force
AUTOMAKEARGS	?= --force --force-missing --add-missing --copy
refresh-debian-configure:
	touch $(AUTOFOODUMMY)
	# unpack
	-debian/rules configure
	# regenerate
	cd debian && aclocal -I ./build
	cd debian && autoconf
	cd debian && automake $(AUTOMAKEARGS)
	# regenerate more, run twice in case debian/control gets updated
	$(RM) debian/stamp-autotools
	debian/rules configure || debian/rules configure
	# cleanup
	fakeroot debian/rules clean
	$(RM) debian/aclocal.m4
	for f in $(AUTOFOODUMMY) ; do if [ ! -s $$f ]; then $(RM) $$f ; fi; done


#-------------
# Maintainer only rule... must be run in external debian directory
.PHONY: refresh-faq
refresh-faq::
	@echo 'Refreshing FAQ'
	@chmod u+w faq.txt;
	(wget --passive-ftp --timestamping \
		--cache=off http://www.sendmail.org/faq/faq.txt;);
	@chmod a-w faq.txt;
	@echo 'Refreshing Misc'
	@chmod u+w cf/feature/rhsbl.m4;
	(cd cf/feature && wget --passive-ftp --timestamping \
		--cache=off http://www.megacity.org/software_downloads/rhsbl.m4;);
	@chmod u-w cf/feature/rhsbl.m4;


#------------------------------------------------------------------------------
# Build/Package check rules
#------------------------------------------------------------------------------
#-------------
.PHONY: info
info:
	#--------------------------------------------------------------------
	# The following information will be used for this run:
	#	Source	   = ${DEB_SOURCE_PACKAGE}
	#	Package    = ${DEB_SOURCE_PACKAGE} ${DEB_VERSION}
	#	             ${version_string}${sm_custom_s}${version_debian}
	#	Upstream V = ${DEB_UPSTREAM_TARBALL_VERSION}
	#	BuildTree  = ${DEB_SRCDIR}
	#	Arch	   = ${DEB_BUILD_ARCH} on ${DEB_HOST_ARCH}
	#	Options
	#	  configure   = ${CONFIG_OPTS}
	#	  config opt  = ${SM_CONF_OPT}
	#	  ENABLE_INDEP= ${ENABLE_INDEP}
	#	  ENABLE_ARCH = ${ENABLE_ARCH}
	#	  ENABLE_DEV  = ${ENABLE_DEV}
	#	  ENABLE_DOC  = ${ENABLE_DOC}
	#	  compile opt = ${CC} ${CFLAGS} ${DEFINES}
	#	DEB_BUILD_OPTIONS = ${DEB_BUILD_OPTIONS}
	#	DH_VERBOSE = ${DH_VERBOSE}
	#	DH_OPTIONS = ${DH_OPTIONS}
	#	DH_COMPAT  = ${DH_COMPAT}
	#--------------------------------------------------------------------


#-------------
.PHONY: verify-tarball
verify-tarball::
	#--------------------------------------------------------------------
	# verify_tarball:
	#
	# Verifying the md5 summs and signed files
	@dh_testdir;
	@for file in $$(find . -maxdepth 1 -name '*.md5'); do \
		echo "Checking MD5 source: $$file."; \
		cat $$file; \
		md5sum -c $$file; \
		done;
	@for file in $$(find . -maxdepth 1 -name '*.sig'); do \
		echo "Checking signature file $$file."; \
		part=$$(echo "$$file" | sed -e 's/\.sig$$//'); \
		if [ -f $$part ]; then \
			gpg --verify $$part.sig $$part || true; \
		elif [ -f $$part.gz ]; then \
			gunzip -c $$part.gz > $$part; \
			gpg --verify $$part.sig $$part || true; \
			rm $$part; \
			fi; \
		done;
	#--------------------------------------------------------------------


#------------------------------------------------------------------------------
# User callable rules
#------------------------------------------------------------------------------
#-------------
.PHONY: clean
clean::
	#--------------------------------------------------------------------
	# clean:
	#
	# Local stuff
	-if [ -f debian/Makefile ]; then \
		$(MAKE) -C debian -f Makefile clean maintainer-clean; \
		fi;
	# Debian stuff
	$(RM) -r ${_cdbs_tarball_dir};
	$(RM) sendmail*.tar.gz.cdbs-config_list
	$(RM) \
		debian/config.log \
		debian/config.status \
		debian/*.debhelper \
		debian/control.new \
		;
	for file in tmp ${DEB_PACKAGES};do \
		$(RM) -r debian/$$file; \
		done;
	#--------------------------------------------------------------------


#-------------
common-build-indep::
	#--------------------------------------------------------------------
	# build-indep:
	#
	# sendmail operations guide
	GROFF_NO_SGR=1 $(MAKE) -C ${DEB_SRCDIR}/doc/op/ op.txt
	# Debian stuff
	$(MAKE) -C debian -f Makefile build-indep
	#--------------------------------------------------------------------


#-------------
common-install-indep::
	#--------------------------------------------------------------------
	# install-indep
	#
	# Correct some paths in upstream documentation
	sed -i \
		-e "s?/usr/libexec?${libexecdir}?g" \
		-e "s?/usr/ucb/vacation?${libexecdir}/vacation.sendmail?g" \
		-e "s?/usr/adm/sm.bin?${sysconfdir}/mail/smrsh?g" \
		${DEB_SRCDIR}/smrsh/README
	# sendmail operations guide and other documentation
	${INSTALL} -d ${PKG_DOC}/op
	$(INSTALL_DATA) ${DEB_SRCDIR}/doc/op/op.ps	${PKG_DOC}/op/
	$(INSTALL_DATA) ${DEB_SRCDIR}/doc/op/op.txt	${PKG_DOC}/op/
	$(INSTALL_DATA) ${DEB_SRCDIR}/README		${PKG_DOC}/
	$(INSTALL_DATA) ${DEB_SRCDIR}/KNOWNBUGS		${PKG_DOC}/
	$(INSTALL_DATA) ${DEB_SRCDIR}/cf/README		${PKG_DOC}/cf.README
	${INSTALL} -d ${PKG_DOC}/sendmail
	$(INSTALL_DATA) ${DEB_SRCDIR}/sendmail/README		${PKG_DOC}/sendmail/
	$(INSTALL_DATA) ${DEB_SRCDIR}/sendmail/SECURITY		${PKG_DOC}/sendmail/
	$(INSTALL_DATA) ${DEB_SRCDIR}/sendmail/TRACEFLAGS	${PKG_DOC}/sendmail/
	$(INSTALL_DATA) ${DEB_SRCDIR}/sendmail/TUNING		${PKG_DOC}/sendmail/
	$(INSTALL_DATA) ${DEB_SRCDIR}/smrsh/README	${PKG_DOC}/smrsh.README
	$(INSTALL_DATA) debian/faq.txt			${PKG_DOC}/
	${INSTALL} -d ${PKG_DOC}/libmilter/html
	$(INSTALL_DATA) ${DEB_SRCDIR}/libmilter/README	${PKG_DOC}/libmilter/
	cp -a ${DEB_SRCDIR}/libmilter/docs/*		${PKG_DOC}/libmilter/html/
	# Debian stuff
	$(MAKE) -C debian/cf		-f Makefile install-indep DESTDIR="${SRC_DIR}/sendmail-cf"
	$(MAKE) -C debian/examples	-f Makefile install-indep DESTDIR="${SRC_DIR}/sendmail-base"
	$(MAKE) -C debian/local		-f Makefile install-indep DESTDIR="${SRC_DIR}/sendmail-base"
	#--------------------------------------------------------------------


#-------------
common-build-arch::
	#--------------------------------------------------------------------
	# build-arch
	#
	# Place our m4 configuration files for the sendmail build to use
	cp -f debian/build/site.config.m4 ${DEB_SRCDIR}/devtools/Site/
	# Finally, build the whole enchilada (we'll let sendmail figure
	# which, if any of the dependant libraries each component needs)
	for subdir in ${SM_SUBDIRS}; do \
		if [ -d  ${DEB_SRCDIR}/$${subdir} ]; then \
			(cd ${DEB_SRCDIR}/$${subdir} && ./Build -S;); \
			fi; \
		done; 
	# Debian stuff
	$(MAKE) -C debian -f Makefile build-arch
	# Remove our config m4 files
	$(RM) ${DEB_SRCDIR}/devtools/Site/site.config.m4
	#--------------------------------------------------------------------


#-------------
common-install-arch::
	#--------------------------------------------------------------------
	# install-arch
	#
	# Correct some paths in upstream man pages
	sed -i \
		-e "s?/usr/adm/sm.bin?${sysconfdir}/mail/smrsh?g" \
		${DEB_SRCDIR}/smrsh/smrsh.8
	sed -i \
		-e "s?/etc/mail/statistics?${localstatedir}/lib/sendmail/sendmail.st?g" \
		${DEB_SRCDIR}/sendmail/sendmail.8
	# sendmail install proper... with a little help ;-}
	$(MAKE) -C ${DEB_SRCDIR}		install		DESTDIR="$(DEB_DESTDIR)"
	$(MAKE) -C ${DEB_SRCDIR}/libmilter	install		DESTDIR="$(DEB_DESTDIR)"
	$(MAKE) -C ${DEB_SRCDIR}/mail.local	force-install	DESTDIR="$(DEB_DESTDIR)"
	# Debian stuff
	$(MAKE) -C debian/sensible_mda		install-arch	DESTDIR="$(DEB_DESTDIR)"
	# Handle man pages for the sendmail aliases
	mv ${PKG_TMP}${mandir}/man5/aliases.5 \
		${PKG_TMP}${mandir}/man5/aliases.sendmail.5
	mv ${PKG_TMP}${mandir}/man1/mailq.1 \
		${PKG_TMP}${mandir}/man1/mailq.sendmail.1
	mv ${PKG_TMP}${mandir}/man1/newaliases.1 \
		${PKG_TMP}${mandir}/man8/newaliases.sendmail.8
	mv ${PKG_TMP}${mandir}/man8/sendmail.8 \
		${PKG_TMP}${mandir}/man8/sendmail.sendmail.8
	# Special handling of vacation - Debian has a seperate package
	# with a different version (sigh) keep our copy just in case...
	mv ${PKG_TMP}${libexecdir}/vacation \
		${PKG_TMP}${libexecdir}/vacation.sendmail
	mv ${PKG_TMP}${mandir}/man1/vacation.1 \
		${PKG_TMP}${mandir}/man1/vacation.sendmail.1
	#--------------------------------------------------------------------


#-------------
# CDBS host dh_fixperms fixup
binary-predeb/sendmail-bin::
	#--------------------------------------------------------------------
	# Proper ownership/modes
	chown root:mail ${PKG_DIR}${libexecdir}/sendmail;
	chmod 02755 ${PKG_DIR}${libexecdir}/sendmail;
	chown root:mail ${PKG_DIR}${libexecdir}/mailstats;
	chmod 02755 ${PKG_DIR}${libexecdir}/mailstats;
	#--------------------------------------------------------------------

binary-predeb/sensible-mda::
	#--------------------------------------------------------------------
	# Proper ownership/modes
	chmod 04755 debian/sensible-mda/usr/sbin/sensible-mda;
	#--------------------------------------------------------------------


#-------------
# after the directories are created but before dh_compress is run.
common-binary-post-install-arch::
	#--------------------------------------------------------------------
	# Obtain build dependancy information
	dh_buildinfo
	#--------------------------------------------------------------------

binary-makedeb-IMPL/sendmail:: verify-tarball


