#!/usr/bin/make -f
#
# Copyright (C) 1998-2000 Roberto Lumbreras <rover@debian.org>
# Copying: GPL

SHELL	=	/bin/bash

PACKAGE	=	$(shell perl -e 'print <> =~ /^(.*) \(.*\)/' debian/changelog)
PKG_VER	=	$(shell perl -e 'print <> =~ /\((.*)\)/' debian/changelog)
PKG_UPVER=	$(shell perl -e 'print <> =~ /\((.*)-[^-]*\)/' debian/changelog)

CFLAGS	=	-Wall -g
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

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

##BINS	= $(shell grep :/: BIN | grep -v 'rc:$$' | awk -F: '{ print $$6; }')

BINS	=	ezmlm-archive ezmlm-issubn ezmlm-make \
		ezmlm-manage ezmlm-send ezmlm-request ezmlm-reject \
		ezmlm-return ezmlm-warn ezmlm-weed ezmlm-list ezmlm-clean \
		ezmlm-cron ezmlm-limit ezmlm-store ezmlm-split ezmlm-moderate \
		ezmlm-sub ezmlm-unsub ezmlm-idx ezmlm-gate \
		ezmlm-tstdig ezmlm-get ezmlm-cgi

SHBINS	=	ezmlm-accept ezmlm-glconf ezmlm-mktab ezmlm-check

MAN1	= $(shell grep /man1/ MAN | awk -F: '{ print $$6; }')
MAN5	= $(shell grep /man5/ MAN | awk -F: '{ print $$6; }')

DOCS	=	BLURB DOWNGRADE.idx FAQ.idx INSTALL INSTALL.idx README \
		README.idx THANKS TODO UPGRADE.idx debian/QUICKSTART

RCS	= $(shell ls -1 ezmlmrc.* | grep -v '\.5$$') ezmlmglrc ezmlmsubrc

build:	build-stamp

build-stamp:
	echo $(CC) $(CFLAGS) > conf-cc.
	tail +2 conf-cc >> conf-cc.; mv conf-cc. conf-cc
	echo $(CC) $(LDFLAGS) > conf-ld.
	tail +2 conf-ld >> conf-ld.; mv conf-ld. conf-ld
	$(MAKE) mysql
	$(MAKE)
	touch build-stamp

binary:	binary-indep binary-arch # binary-src

binary-indep:

binary-arch:    checkroot build
	$(RM) -r debian/tmp
	$(INSTALL_DIR) debian/tmp/{DEBIAN,etc,etc/ezmlm,usr/{bin,share/{man/man{1,5},doc/$(PACKAGE)-idx/examples}}}
	$(INSTALL_PROGRAM) $(BINS) debian/tmp/usr/bin/
	$(INSTALL_SCRIPT) $(SHBINS) debian/tmp/usr/bin/
	$(INSTALL_FILE) $(MAN1) debian/tmp/usr/share/man/man1/
	$(INSTALL_FILE) $(MAN5) debian/tmp/usr/share/man/man5/
	$(INSTALL_FILE) $(DOCS) debian/tmp/usr/share/doc/ezmlm-idx/
	$(INSTALL_FILE) ezmlmrc debian/tmp/etc/ezmlm/ezmlmrc
	$(INSTALL_FILE) $(RCS) debian/tmp/usr/share/doc/ezmlm-idx/examples/
	$(INSTALL_FILE) CHANGES debian/tmp/usr/share/doc/ezmlm-idx/changelog
	$(INSTALL_FILE) CHANGES.idx debian/tmp/usr/share/doc/ezmlm-idx/changelog.idx
	$(INSTALL_FILE) debian/changelog debian/tmp/usr/share/doc/ezmlm-idx/changelog.Debian
	-gzip -9fr debian/tmp/usr/share
	$(INSTALL_FILE) debian/copyright debian/tmp/usr/share/doc/ezmlm-idx/
	sed -e "s/#PACKAGE#/$(PACKAGE)-idx/g" debian/run.postinst > debian/tmp/DEBIAN/postinst
	$(INSTALL_FILE) debian/conffiles debian/tmp/DEBIAN/conffiles
	chmod 0755 debian/tmp/DEBIAN/postinst

	dpkg-shlibdeps $(BINS)
	dpkg-gencontrol -isp -pezmlm-idx -cdebian/control.real
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

binary-src:	checkroot debian/control ../$(PACKAGE)_$(PKG_UPVER).orig.tar.gz ../$(PACKAGE)_$(PKG_VER).dsc ../$(PACKAGE)_$(PKG_VER).diff.gz
	$(RM) -r debian/tmp-src debian/files
	$(INSTALL_DIR) debian/tmp-src/{DEBIAN,usr/{bin,{src,share/doc}/$(PACKAGE)-src}}
	$(INSTALL_SCRIPT) debian/build-PACKAGE debian/tmp-src/usr/bin/build-$(PACKAGE)
	$(INSTALL_FILE) ../$(PACKAGE)_$(PKG_UPVER).orig.tar.gz \
		../$(PACKAGE)_$(PKG_VER).dsc \
		../$(PACKAGE)_$(PKG_VER).diff.gz \
		debian/tmp-src/usr/src/$(PACKAGE)-src/
	sed -e "s/#PACKAGE#/$(PACKAGE)-src/g" debian/src.postinst > debian/tmp-src/DEBIAN/postinst
	chmod 0755 debian/tmp-src/DEBIAN/postinst
	sed -e "s/#PACKAGE#/$(PACKAGE)/g" debian/README-src > debian/tmp-src/usr/share/doc/$(PACKAGE)-src/README.Debian
	chmod 0644 debian/tmp-src/usr/share/doc/$(PACKAGE)-src/README.Debian
	$(INSTALL_FILE) debian/changelog debian/tmp-src/usr/share/doc/$(PACKAGE)-src/changelog.Debian
	$(INSTALL_FILE) debian/README-src debian/tmp-src/usr/share/doc/$(PACKAGE)-src/README.Debian
	-gzip -9fr debian/tmp-src/usr/share
	$(INSTALL_FILE) debian/copyright debian/tmp-src/usr/share/doc/$(PACKAGE)-src/

	dpkg-gencontrol -isp -p$(PACKAGE)-src -Pdebian/tmp-src
	chown -R root.root debian/tmp-src
	chmod -R go=rX debian/tmp-src
	dpkg --build debian/tmp-src ..

debian/control:	debian/control.real
	test -f debian/control.real && sed -e "/^Package: $(PACKAGE)-src$$/b" -e "/^Package: /,/^$$/d" debian/control.real > debian/control

../$(PACKAGE)_$(PKG_UPVER).orig.tar.gz:
	@{ echo "Error: $@ missing"; exit 1; }

../$(PACKAGE)_$(PKG_VER).dsc ../$(PACKAGE)_$(PKG_VER).diff.gz: debian/rules
	debian/rules clean
	S=$$(basename $$(pwd)); \
	cd ..; dpkg-source -b $$S ## $(PACKAGE)-$(PKG_UPVER)

checkdir:
	@test -f debian/rules

checkroot:	checkdir
	@test 0 = `id -u` || { echo "Error: not super-user"; exit 1; }

clean:	checkdir debian/control
	$(RM) build-stamp debian/files debian/substvars
	$(MAKE) clean
	$(RM) ezmlm-mktab checktag.c issub.c logmsg.c subscribe.c opensql.c \
		putsubs.c tagmsg.c searchlog.c conf-sqlld conf-sqlcc
	$(RM) *~ core
	$(RM) -r debian/tmp debian/tmp-src
	echo "cc -O2" > conf-cc.
	tail +2 conf-cc >> conf-cc.; mv conf-cc. conf-cc
	echo "cc -s" > conf-ld.
	tail +2 conf-ld >> conf-ld.; mv conf-ld. conf-ld

.PHONY: build binary binary-indep binary-arch binary-src checkdir checkroot clean
