# SA-Exim can be built standalone as a loadable module with this Makefile
# or you can copy sa-exim.c over exim's local_scan.c file if you want to
# statically build it into exim
#

VERSION=$(shell cat version)

# The idea is that you don't have to edit these values, you can override
# them on the command line:
# make SACONF=/etc/exim/sa-exim.conf LDFLAGS="-shared -fPIC" CC=cc
CC=gcc
CFLAGS=-O2 -Wall
LDFLAGS=-shared
SACONF=/etc/exim4/sa-exim.conf
SPAMC=/usr/bin/spamc


# I place the directory in exim/debian/local_scan. Adjust the path as needed
# Actually, we will also look for the versions supplied with this source
# if we can't find the exim source
EXIM_SRC= ../../src
EXIM_SRC_LOCAL = ./eximinc
SUFF=-$(VERSION)

SAFLAGS=-DSPAMASSASSIN_CONF=\"$(SACONF)\" -DSPAMC_LOCATION=\"$(SPAMC)\"
BUILDCFLAGS=-I$(EXIM_SRC) -I$(EXIM_SRC_LOCAL) -DDLOPEN_LOCAL_SCAN $(SAFLAGS) $(CFLAGS)

SONAME=$(subst .so,$(SUFF).so,sa-exim.so)

DOCS=sa.html CHANGELOG ACKNOWLEDGEMENTS
OBJECTS=$(SONAME) accept.so sa-exim_short.conf $(DOCS)
OTHERTARGETS=sa-exim.h

all: $(OBJECTS)

docs: $(DOCS)
	

$(SONAME) : sa-exim.c
	@echo "Building $@"
	$(CC) $(BUILDCFLAGS) $(LDFLAGS) -o $@ $<
	chmod a+rx $(SONAME)

accept.so: accept.c
	@echo "Building $@"
	$(CC) $(BUILDCFLAGS) $(LDFLAGS) -o $@ $<
	chmod a+rx $@

ACKNOWLEDGEMENTS: Acknowledgements.html
	@echo "Generating $@"
	@lynx -dump -nolist $< > $@

CHANGELOG: Changelog.html
	@echo "Generating $@"
	@lynx -dump -nolist $< > $@

sa.html: Changelog.html Acknowledgements.html sa.html.template
	@echo "Generating $@"
	@bash -c 'sed "/<Changelog>/,$$ d" < sa.html.template; cat Changelog.html; sed "1,/<\/Changelog>/ d; /<Acknowledgements>/,$$ d" < sa.html.template; cat Acknowledgements.html; sed "1,/<\/Acknowledgements>/ d" < sa.html.template' > sa.html

sa-exim_short.conf: sa-exim.conf
	@cat sa-exim.conf | sed "/# --- snip ---/,$$ d" > sa-exim_short.conf
	@cat sa-exim.conf | grep -v "^#" | tr '\012' '' | sed "s/*//g" | tr '' '\012' >> sa-exim_short.conf

sa-exim.c: sa-exim.h
	
sa-exim.h: version
	echo "char *version=\"`cat version` (built `date`)\";" > sa-exim.h

clean:	
	@-rm $(OBJECTS) $(DOCS) $(OTHERTARGETS) sa-exim_diff.gz build-stamp configure-stamp 2>/dev/null


# lightly tested, use at your own risk :)
deb:	../sa-exim_$(VERSION).orig.tar.gz debian/rules
	@make clean
	@dpkg-buildpackage -uc -us -sd -rfakeroot

../sa-exim_$(VERSION).orig.tar.gz: * */*
	@make clean
	@if [ -d debian ]; then echo "Can't rebuild $@ with debian tree unpacked, please remove it"; exit 1; fi
	@( cd ..; tar chvzf sa-exim_$(VERSION).orig.tar.gz sa-exim-$(VERSION) )

debian/rules:
	@wget http://marc.merlins.org/linux/exim/files/debian/sa-exim_diff.gz
	@zcat sa-exim_diff.gz | patch -s -p1
	@/bin/rm sa-exim_diff.gz
	@chmod 755 debian/rules

