#
# makefile - make and install for abmail
#
# Copyright (C) 2006-2008 Taiji Yamada <taiji@aihara.co.jp>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
#
# build:
#	$ make CFLAGS='-DMYNETWORK=\"133.20.0.0\" -DMYNETMASK=\"16\" -DMYDOMAIN=\"dendai.ac.jp\"'	# with host configration
#	$ make	# Mac OS X
#	$ make LDLIBS='-lpthread -lrt'	# Linux
#	$ make CC='cc -DDISABLE_SHM' LDLIBS='-lpthread'	# NetBSD without shm
#	$ make CC='gcc -DDISABLE_MMAN -DDISABLE_PTHREAD -DUSE_DRACD_DB -I../db-3.3.11/build_unix' LDLIBS='-L../db-3.3.11/build_unix -ldb -lnsl -lgen'	# Solaris & dracd.db without mman & pthread
#	$ make CC='gcc -D_REENTRANT -DUSE_DRACD_DB -I../db-3.3.11/build_unix' LDLIBS='-L../db-3.3.11/build_unix -ldb -lpthread -lsocket -lnsl -lrt'	# Solaris & dracd.db
#	$ make CC='gcc -D_REENTRANT' LDLIBS='-lpthread -lsocket -lnsl -lrt'	# Solaris
#
PREFIX=/opt/local
CPPFLAGS=-Wall -O3 -I$(PREFIX)/include -DPREFIX=\"$(PREFIX)\"
LDFLAGS=-L$(PREFIX)/lib -lpcre

BINS=\
abmail	\

all: $(BINS)

clean: clean.test
	rm -f $(BINS)
	cd tools; make clean

LSTS=\
ams_reliable.lst	\
ams_unreliable.lst	\
rfs_reliable.lst	\
ras_reliable.lst	\
rss_reliable.lst	\
rhs_unreliable.lst	\
rns_reliable.lst	\
rns_unreliable.lst	\

SCRIPTS=\
abmail-echo	\
abmail-cat	\
abmail-mv	\
abmail-rescue	\
abmailecho	\
abmailcat	\
abmailmv	\
abmailrescue	\
abmails-echo	\
abmails-cat	\
abmails-rescue	\
abmails-echos	\
abmails-cats	\
abmails-rescues	\

install:
	cp $(BINS) $(PREFIX)/bin
	test -d $(PREFIX)/etc/abmail || mkdir $(PREFIX)/etc/abmail
	cp $(LSTS) $(PREFIX)/etc/abmail
	for script in $(SCRIPTS); do\
	  symlink=`perl -e 'print readlink("$$ARGV[0]")."\n";' "$$script"`;\
	  case "$$symlink" in "")\
	  sed 's|\$${ABMAIL_ROOT:-./}||g;'"s|/opt/local|$(PREFIX)|g" $$script > $(PREFIX)/bin/$$script;\
	  chmod +x $(PREFIX)/bin/$$script;\
	  ;;*) (cd $(PREFIX)/bin && rm -f $$script && ln -s $$symlink $$script);; esac;\
	done

#ABMAILFLAGS=-b -g -d
ABMAILFLAGS=-b -g -d --sm-client
test: all
	find examples/nospam -name '[0-9]*' -exec ./abmail-echo $(ABMAILFLAGS) -v- -vvv -vvvv {} \; -print > examples_nospam.log 2>&1
	-diff examples/nospam.log examples_nospam.log
	find examples/spam -name '[0-9]*' -exec ./abmailecho $(ABMAILFLAGS) -v -vvv -vvvv {} \; -print > examples_spam.log 2>&1
	-diff examples/spam.log examples_spam.log

clean.test:
	rm -f examples_*.log
