#
# COPYRIGHT INFORMATION - DO NOT REMOVE
# "Portions Copyright (c) 2000-2001 LinuxMagic Inc. All Rights Reserved.
#
# This file contains Original Code and/or Modifications of Original Code as
# defined in and that are subject to the Free Source Code License Version
# 1.0 (the 'License'). You may not use this file except in compliance with
# the License. Please obtain a copy of the License at:
#
# http://www.linuxmagic.com/opensource/licensing/FSCL.txt
#
# and read it before using this file.
#
# The Original Code and all software distributed under the License are
# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, AND LINUXMAGIC HEREBY DISCLAIMS ALL SUCH WARRANTIES,
# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
# the License for the specific language governing rights and limitations
# under the License."
#
# Please read the terms of this license carefully. By using or downloading
# this software or file, you are accepting and agreeing to the terms of this
# license with LinuxMagic Inc. If you are agreeing to this license on behalf
# of a company, you represent that you are authorized to bind the company to
# such a license. If you do not meet this criterion or you do not agree to
# any of the terms of this license, do NOT download, distribute, use or alter
# this software or file in any way.
#
# Authors: Burton Samograd (burton@wizard.ca)
#		   Josh Wilsdon (josh@wizard.ca)
#
# CVS Id: $Id: Makefile,v 1.81 2003/10/29 20:24:28 josh Exp $
#
# DO NOT MODIFY WITHOUT CONSULTING THE LICENSE
#

# Configuration
#
# Comment this out if you would like to use the program without database 
# support.  This is reccomended if you are using the open source version.
#USE_LM_DATABASE=1
USE_LM_DBFILE=1
#USE_LOCAL=1
RELEASE=1
OPENSOURCE=1
#USE_EFENCE=1

SMTP=magic-smtpd
LOCAL=magic-local

LMLIBDIR=./lmlibs
include $(LMLIBDIR)/Makefile.defaults
LMLIB=$(LMLIBDIR)/liblm.a
LMLIBFILES=$(shell find $(LMLIBDIR) -name 'Makefile*' -o -name '*.[ch]' | grep -v liblm.a | xargs)

SMTP_SRCS=\
magic-smtpd.o config_common.o config_qmail.o config_spam_rule.o \
check_user.o auth.o auth_chkp.o b64.o spam_check.o smtp_check.o \
smtp.o cdb.o strlist.o utils.o common/spam_log.o \
ip2country.o getmx.o common/load_rules.o common/clean_rules.o \
common/stopwatch.o common/read_rules.o common/load_controls.o

LOCAL_SRCS=\
magic-local.o config_common.o magic-local-rules.o magic-local-checks.o \
common/stopwatch.o utils.o magic-local-util.o common/spam_log.o \
ip2country.o header_parse.o strlist.o common/load_rules.o \
common/read_rules.o config_qmail.o config_spam_rule.o common/load_controls.o

SMTP_OBJS=$(SMTP_SRCS:.c=.o)
LOCAL_OBJS=$(LOCAL_SRCS:.c=.o)
LIBS += -llm -lresolv -lcrypt
INCLUDES=-I$(LMLIBDIR)

CFLAGS += $(INCLUDES)
LDFLAGS += -L$(LMLIBDIR) -L/usr/local/postgres/lib
CLEANFILES += config_spam_rule.h dbscripts/init_spamdir.sh

ifdef USE_EFENCE
LIBS += -lefence
endif

ifdef USE_LM_DATABASE
CFLAGS += -DUSE_LM_DATABASE
SMTP_OBJS += auth_db.o check_user_db.o postgresdb.o write_spam_log.o config_spam_rule_db.o
LOCAL_OBJS += postgresdb.o write_spam_log.o config_spam_rule_db.o
LIBS += -lpq
endif

ifdef USE_LM_DBFILE
CFLAGS += -DUSE_LM_DBFILE
SMTP_OBJS += mod_dbfile.o libgetbdb.o
LOCAL_OBJS += mod_dbfile.o libgetbdb.o
LIBS += -ldb
endif

ifdef RELEASE
CFLAGS += -DRELEASE_VERSION
endif

ifdef OPENSOURCE
CFLAGS += -DOPENSOURCE
endif


ifdef USE_LOCAL
all: $(LMLIB) $(SMTP) $(LOCAL)
else
all: $(LMLIB) $(SMTP)
endif

tests: test_header_parse

tags: notags
	@(ctags `find ./ -type f -name "*.[ch]" | xargs`)

notags:
	@(rm -vf tags)

test_header_parse: test_header_parse.c header_parse.o
	$(CC) -Wall -W -o test_header_parse test_header_parse.c header_parse.o

checkheaders:
	@if ! [ -d /usr/include/postgresql ] && ! [ -d /usr/local/include/postgresql ]; then printf "You need to have the postgres library installed to compile this program with database support.\nI couldn't find the headers in either /usr/include/postgresql or /usr/local/include/postgresql so I'm assuming it isn't installed.\nEither comment out USE_LM_DATABASE at the top of this Makefile or install the postgres library.\n"; exit 1; fi

$(SMTP): lmlibs $(SMTP_OBJS)
	$(LD) $(LDFLAGS) -o $(SMTP) $(SMTP_OBJS) $(LIBS)

$(LOCAL): lmlibs $(LOCAL_OBJS)
	$(LD) $(LDFLAGS) -o $(LOCAL) $(LOCAL_OBJS) $(LIBS)

.PHONY:lmlibs
lmlibs: $(LMLIB)
$(LMLIB): $(LMLIBFILES)
	$(MAKE) -C $(LMLIBDIR)

.PHONY: clean
clean: cleanfiles
	$(RM) $(RMFLAGS) $(SMTP) $(LOCAL) test_header_parse
	@(cd common; $(MAKE) clean)

reallyclean: clean
	$(MAKE) -C $(LMLIBDIR) clean

# Rules for automatically generated scripts and source files 
# from spam_rules.in
config_spam_rule.h: spam_rules.in config_spam_rule_h.sed
	sed -f config_spam_rule_h.sed spam_rules.in > config_spam_rule.h.tmp
	cat config_spam_rule.h.tmp | sed -e 's/-%-Q/-%-q/' > config_spam_rule.h
	rm config_spam_rule.h.tmp
dbscripts/init_spamdir.sh: spam_rules.in dbscripts/init_spamdir_sh.sed
	sed -f dbscripts/init_spamdir_sh.sed spam_rules.in > dbscripts/init_spamdir.sh
	chmod +x dbscripts/init_spamdir.sh

# 
# Generated dependencies using 'find -name '*.[c]' |  xargs gcc -MM -Ilmlibs/ -I.'
# in the main source directory and then pasted.
#
postgresdb.o: ./postgresdb.c postgresdb.h magic-smtpd.h config_msd.h utils.h
cdb.o: ./cdb.c cdb.h
check_user.o: ./check_user.c check_user.h config_msd.h smtp.h cdb.h
auth.o: ./auth.c auth.h config_msd.h
smtp.o: ./smtp.c smtp.h
magic-smtpd.o: ./magic-smtpd.c magic-smtpd.h config_msd.h \
 config_qmail.h strlist.h config_spam_rule.h smtp.h cdb.h check_user.h \
 spam_check.h auth.h utils.h common/spam_rules.inc \
 common/control_files.inc

check_user_db.o: ./check_user_db.c config_msd.h check_user.h postgresdb.h
headertest.o: ./lmlibs/examples/headertest.c
lmalloc-test.o: ./lmlibs/examples/lmalloc-test.c
lmconfig-test.o: ./lmlibs/examples/lmconfig-test.c
lmdebug-test.o: ./lmlibs/examples/lmdebug-test.c
lmlogger-test.o: ./lmlibs/examples/lmlogger-test.c
lmstring-test.o: ./lmlibs/examples/lmstring-test.c
lmalloc.o: ./lmlibs/lmalloc/lmalloc.c
lmconfig.o: ./lmlibs/lmconfig/lmconfig.c
lmconfig_control.o: ./lmlibs/lmconfig/lmconfig_control.c
lmconfig_env.o: ./lmlibs/lmconfig/lmconfig_env.c
lmconfig_flatfile.o: ./lmlibs/lmconfig/lmconfig_flatfile.c
lmconfig_database.o: ./lmlibs/lmconfig/lmconfig_database.c
lmdebug.o: ./lmlibs/lmdebug/lmdebug.c
lmlogger.o: ./lmlibs/lmlogger/lmlogger.c
lmstring.o: ./lmlibs/lmstring/lmstring.c
b64.o: ./b64.c
config_qmail.o: ./config_qmail.c config_common.h config_msd.h \
 config_qmail.h strlist.h
config_spam_rule.o: ./config_spam_rule.c config_msd.h config_common.h \
 config_spam_rule.h strlist.h
config_spam_rule_db.o: ./config_spam_rule_db.c config_msd.h config_common.h \
 config_spam_rule.h strlist.h
strlist.o: ./strlist.c strlist.h
auth_chkp.o: ./auth_chkp.c config_msd.h
auth_db.o: ./auth_db.c magic-smtpd.h config_msd.h config_qmail.h \
 strlist.h smtp.h auth.h postgresdb.h b64.h
spam_check.o: ./spam_check.c magic-smtpd.h config_msd.h config_qmail.h \
 strlist.h spam_check.h config_spam_rule.h smtp_check.h utils.h
utils.o: ./utils.c
config_common.o: ./config_common.c
smtp_check.o: ./smtp_check.c smtp_check.h config_msd.h
magic-local.o: ./magic-local.c
ip2country.o: ./ip2country.c
header_parse.o: ./header_parse.c
getmx.o: ./getmx.c
mod_dbfile.o: ./mod_dbfile.c ./config_spam_rule.h
libgetbdb.o: ./libgetbdb.c
write_spam_log.o: ./write_spam_log.c
common/spam_log.o: common/spam_log.c
	@(cd common; $(MAKE) spam_log.o)
common/spam_rules.inc: common/spam_rules
	@(cd common; $(MAKE) spam_rules.inc)
common/control_files.inc: common/control_files
	@(cd common; $(MAKE) control_files.inc) 
