# Makefile for cyrus imap server and associated programs
#
# Copyright (c) 1994-2008 Carnegie Mellon University.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#
# 3. The name "Carnegie Mellon University" must not be used to
#    endorse or promote products derived from this software without
#    prior written permission. For permission or any legal
#    details, please contact
#      Carnegie Mellon University
#      Center for Technology Transfer and Enterprise Creation
#      4615 Forbes Avenue
#      Suite 302
#      Pittsburgh, PA  15213
#      (412) 268-7393, fax: (412) 268-7395
#      innovation@andrew.cmu.edu
#
# 4. Redistributions of any form whatsoever must retain the following
#    acknowledgment:
#    "This product includes software developed by Computing Services
#     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
#
# CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
# OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

DEFINES=-DSETPROCTITLE

# \Seen state database.  Either 'db' (berkeley db) or 
# 'local' (legacy flat file).
SEEN=seen_db.o

srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@

CC = @CC@
INSTALL = @INSTALL@
RANLIB = @RANLIB@
AWK = @AWK@

CYRUS_USER=@cyrus_user@
CYRUS_GROUP=@cyrus_group@

DEFS = @DEFS@ @LOCALDEFS@
CPPFLAGS = -I.. -I$(srcdir)/../lib @COM_ERR_CPPFLAGS@ @SIEVE_CPPFLAGS@ @CPPFLAGS@ @SASLFLAGS@
IMAP_LIBS = @IMAP_LIBS@ @LIB_RT@
SIEVE_OBJS = @SIEVE_OBJS@
SIEVE_LIBS = @SIEVE_LIBS@
IMAP_COM_ERR_LIBS = @IMAP_COM_ERR_LIBS@
LIB_WRAP = @LIB_WRAP@
LIBS = $(IMAP_LIBS) $(IMAP_COM_ERR_LIBS)
DEPLIBS = ../lib/libcyrus.a ../lib/libcyrus_min.a @DEPLIBS@

CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@ @COM_ERR_LDFLAGS@ 

SHELL = /bin/sh
MAKEDEPEND = @MAKEDEPEND@

COMPILE_ET=@COMPILE_ET@

#
# Some notes on purify --
#     you probably want to run the make as the cyrus user as 
#     purify sets the cache directory based on the user. So,
#     if you don't, purify can't find the instrumented libraries
#     and so you don't get any useful information.
#     It may also help to run purify by hand to instrument any of
#     the dynamic libraries that may crop up during run time.
# 
PURIFY=/usr/local/bin/purify
PUREOPT= -best-effort -logfile=/tmp/pure/%v.%p.log -always_use_cache_dir -cachedir=/usr/tmp/$(USER)
QUANTIFY=/usr/local/bin/quantify
QUANTOPT=-windows=no -filename-prefix=/tmp/quant/%v.%p -write-summary-file= -logfile=/tmp/quant/%v.%p.log

prefix = @prefix@
exec_prefix = @exec_prefix@
cyrus_prefix = @cyrus_prefix@
service_path = @service_path@

LOBJS= append.o mailbox.o mboxlist.o mupdate-client.o mboxname.o message.o \
	global.o imap_err.o mupdate_err.o proc.o setproctitle.o \
	convert_code.o duplicate.o saslclient.o saslserver.o ../lib/signals.o \
	annotate.o search_engines.o squat.o squat_internal.o mbdump.o \
	imapparse.o telemetry.o user.o notify.o idle.o quota_db.o \
	sync_log.o $(SEEN) mboxkey.o backend.o tls.o message_guid.o \
	statuscache_db.o userdeny_db.o sequence.o upgrade_index.o \
	dlist.o version.o

IMAPDOBJS=pushstats.o imapd.o proxy.o imap_proxy.o index.o

LMTPOBJS=lmtpstats.o lmtpengine.o spool.o

# Your typical objects for the command line utilities
CLIOBJS=cli_fatal.o mutex_fake.o

SERVICE=../master/service.o
SERVICETHREAD=../master/service-thread.o

PROGS = imapd lmtpd pop3d \
	fud smmapd reconstruct quota mbpath ipurge cyr_dbtool cyr_synclog \
	cyrdump chk_cyrus cvt_cyrusdb deliver ctl_mboxlist \
	ctl_deliver ctl_cyrusdb squatter mbexamine cyr_expire arbitron \
	unexpunge cyr_df cyr_sequence cyr_userseen @IMAP_PROGS@

BUILTSOURCES = imap_err.c imap_err.h pushstats.c pushstats.h \
	lmtpstats.c lmtpstats.h mupdate_err.c mupdate_err.h \
	nntp_err.c nntp_err.h

all: $(BUILTSOURCES) $(PROGS) $(SUIDPROGS)

pure: imapd.pure lmtpd.pure mupdate.pure


install:
	$(srcdir)/../install-sh -d ${DESTDIR}$(service_path)
	for file in $(PROGS); \
	do \
		$(INSTALL) -m 755 $$file $(DESTDIR)$(service_path) || exit 1; \
	done
	ln -f $(DESTDIR)$(service_path)/pop3d $(DESTDIR)$(service_path)/pop3proxyd
	ln -f $(DESTDIR)$(service_path)/imapd $(DESTDIR)$(service_path)/proxyd
	ln -f $(DESTDIR)$(service_path)/lmtpd $(DESTDIR)$(service_path)/lmtpproxyd

.c.o:
	$(CC) -c $(CPPFLAGS) $(DEFS) $(CFLAGS) \
	$<

### libimap

libimap.a: $(LOBJS)
	rm -f libimap.a
	ar cr libimap.a $(LOBJS)
	$(RANLIB) libimap.a

### Built Source Files

version.o: ../xversion.h

pushstats.c: pushstats.snmp $(srcdir)/../snmp/snmpgen
	$(srcdir)/../snmp/snmpgen $(srcdir)/pushstats.snmp

pushstats.h: pushstats.c

lmtpstats.c: lmtpstats.snmp $(srcdir)/../snmp/snmpgen
	$(srcdir)/../snmp/snmpgen $(srcdir)/lmtpstats.snmp

lmtpstats.h: lmtpstats.c

imap_err.c: imap_err.et
	$(COMPILE_ET) $(srcdir)/imap_err.et

imap_err.h: imap_err.c

nntp_err.c: nntp_err.et
	$(COMPILE_ET) $(srcdir)/nntp_err.et

nntp_err.h: nntp_err.c

mupdate_err.c: mupdate_err.et
	$(COMPILE_ET) $(srcdir)/mupdate_err.et

mupdate_err.h: mupdate_err.c

### Services
idled: idled.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o idled \
	 idled.o mutex_fake.o libimap.a $(DEPLIBS) $(LIBS)

lmtpd: lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) mutex_fake.o \
	 libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
	$(CC) $(LDFLAGS) -o lmtpd \
	 $(SERVICE) lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) \
	 mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)

lmtpd.pure: lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) \
	 mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(SERVICE)
	$(PURIFY) $(PUREOPT) $(CC) $(LDFLAGS) -o lmtpd.pure \
	 $(SERVICE) lmtpd.o proxy.o $(LMTPOBJS) $(SIEVE_OBJS) \
	 mutex_fake.o libimap.a $(SIEVE_LIBS) $(DEPLIBS) $(LIBS) $(LIB_WRAP)

imapd: $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
	$(CC) $(LDFLAGS) -o imapd \
	 $(SERVICE) $(IMAPDOBJS) mutex_fake.o \
	libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)

imapd.pure: $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
	$(PURIFY) $(PUREOPT) $(CC) $(LDFLAGS) -o imapd.pure \
	 $(SERVICE) $(IMAPDOBJS) mutex_fake.o libimap.a \
	$(DEPLIBS) $(LIBS) $(LIB_WRAP)

imapd.quant: $(IMAPDOBJS) mutex_fake.o libimap.a $(DEPLIBS) $(SERVICE)
	$(QUANTIFY) $(QUANTOPT) $(CC) $(LDFLAGS) -o imapd.quant \
	 $(SERVICE) $(IMAPDOBJS) mutex_fake.o libimap.a \
	$(DEPLIBS) $(LIBS) $(LIB_WRAP)

mupdate: mupdate.o mupdate-slave.o mupdate-client.o mutex_pthread.o tls.o \
	libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o mupdate \
	 $(SERVICETHREAD) mupdate.o mupdate-slave.o mupdate-client.o \
	 mutex_pthread.o tls.o libimap.a \
	 $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread

mupdate.pure: mupdate.o mupdate-slave.o mupdate-client.o mutex_pthread.o \
	libimap.a $(DEPLIBS)
	$(PURIFY) $(PUREOPT) $(CC) $(LDFLAGS) -o mupdate.pure \
	 $(SERVICETHREAD) mupdate.o mupdate-slave.o mupdate-client.o \
	 mutex_pthread.o libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP) -lpthread

pop3d: pop3d.o proxy.o backend.o tls.o mutex_fake.o libimap.a \
	$(DEPLIBS) $(SERVICE)
	$(CC) $(LDFLAGS) -o pop3d pop3d.o proxy.o backend.o tls.o $(SERVICE) \
	 mutex_fake.o libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)

nntpd: nntpd.o proxy.o backend.o index.o smtpclient.o spool.o tls.o \
	 mutex_fake.o nntp_err.o libimap.a $(DEPLIBS) $(SERVICE)
	$(CC) $(LDFLAGS) -o nntpd nntpd.o proxy.o backend.o index.o spool.o \
	 smtpclient.o tls.o $(SERVICE) mutex_fake.o nntp_err.o \
	 libimap.a $(DEPLIBS) $(LIBS) $(LIB_WRAP)

fud: fud.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
	$(CC) $(LDFLAGS) -o fud $(SERVICE) fud.o mutex_fake.o libimap.a \
	$(DEPLIBS) $(LIBS) $(LIB_WRAP)

smmapd: smmapd.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
	$(CC) $(LDFLAGS) -o smmapd $(SERVICE) smmapd.o mutex_fake.o libimap.a \
	$(DEPLIBS) $(LIBS) $(LIB_WRAP)

sync_server: sync_server.o sync_support.o \
	imapparse.o tls.o libimap.a mutex_fake.o $(DEPLIBS) $(SERVICE)
	$(CC) $(LDFLAGS) -o \
	sync_server sync_server.o sync_support.o \
	imapparse.o tls.o $(SERVICE) libimap.a mutex_fake.o \
	$(DEPLIBS) $(LIBS) $(LIB_WRAP)

### Command Line Utilities
arbitron: arbitron.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o arbitron arbitron.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

cyr_userseen: cyr_userseen.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o cyr_userseen cyr_userseen.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

cyr_sequence: cyr_sequence.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o cyr_sequence cyr_sequence.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

cyr_dbtool: cyr_dbtool.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o cyr_dbtool cyr_dbtool.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

cyr_synclog: cyr_synclog.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o cyr_synclog cyr_synclog.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

cvt_cyrusdb: cvt_cyrusdb.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o cvt_cyrusdb cvt_cyrusdb.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

chk_cyrus: chk_cyrus.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o chk_cyrus chk_cyrus.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

deliver: deliver.o $(LMTPOBJS) proxy.o mutex_fake.o libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o deliver deliver.o $(LMTPOBJS) proxy.o \
	mutex_fake.o libimap.a $(DEPLIBS) $(LIBS)

ctl_deliver: ctl_deliver.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	 $@ ctl_deliver.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

ctl_mboxlist: ctl_mboxlist.o mupdate-client.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o $@ ctl_mboxlist.o mupdate-client.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

ctl_cyrusdb: ctl_cyrusdb.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	 $@ ctl_cyrusdb.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

cyr_expire: cyr_expire.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o $@ cyr_expire.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

fetchnews: fetchnews.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	 $@ fetchnews.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

squatter: squatter.o index.o squat_build.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o squatter squatter.o index.o squat_build.o \
	$(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

mbpath: mbpath.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o mbpath mbpath.o $(CLIOBJS) libimap.a \
	$(DEPLIBS) $(LIBS)

ipurge: ipurge.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o ipurge ipurge.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

cyr_virusscan: cyr_virusscan.o index.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o cyr_virusscan cyr_virusscan.o index.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS) -lclamav

cyrdump: cyrdump.o index.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o cyrdump cyrdump.o index.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

cyr_df: cyr_df.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	 cyr_df cyr_df.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

mbexamine: mbexamine.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	 mbexamine mbexamine.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

reconstruct: reconstruct.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	 reconstruct reconstruct.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

quota: quota.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o quota quota.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

tls_prune: tls_prune.o tls.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	 $@ tls_prune.o tls.o $(CLIOBJS) libimap.a $(DEPLIBS) $(LIBS)

unexpunge: unexpunge.o $(CLIOBJS) libimap.a $(DEPLIBS)
	$(CC) $(LDFLAGS) -o $@ unexpunge.o $(CLIOBJS) \
	libimap.a $(DEPLIBS) $(LIBS)

make_md5: make_md5.o libimap.a mutex_fake.o $(DEPLIBS)
	$(CC) $(LDFLAGS) -o make_md5 make_md5.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)

make_sha1: make_sha1.o libimap.a mutex_fake.o $(DEPLIBS)
	$(CC) $(LDFLAGS) -o make_sha1 make_sha1.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)

sync_client: sync_client.o sync_support.o \
	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	sync_client sync_client.o sync_support.o \
	backend.o tls.o imapparse.o libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)

sync_reset: sync_reset.o sync_support.o \
	libimap.a mutex_fake.o $(DEPLIBS)
	$(CC) $(LDFLAGS) -o \
	sync_reset sync_reset.o sync_support.o \
	libimap.a mutex_fake.o $(DEPLIBS) $(LIBS)

### Other Misc Targets

clean:
	rm -f *.o *.a Makefile.bak makedepend.log \
	$(BUILTSOURCES) $(PROGS) $(SUIDPROGS) cyr_virusscan

distclean: clean
	rm -f Makefile

depend: imap_err.h
	${MAKEDEPEND} $(CPPFLAGS) $(DEFS) $(CFLAGS) *.c $(srcdir)/*.c 1>makedepend.log 2>&1

# DO NOT DELETE THIS LINE -- make depend depends on it.
