# Main makefile for Exim and friends. This is a generic file which first
# arranges to build a make file in the build directory if it is not present.

REGEXP     = ../regexp
SCRIPTS    = ../scripts
SAMPLELINK = version.c
LOCAL	   = ../Local
EDITME	   = $(LOCAL)/Makefile
EXIMON_EDITME = $(LOCAL)/eximon.conf

# Initial target checks for the existence of a site configuration file,
# created by editing EDITME, but first it arranges to touch the site
# configuration if it exists and any of the optional configuration files,
# depending on the os or the architecture, have been altered. It then checks
# for the existence of one symbolic link to be on the safe side, and builds the
# links if necessary. Next, it arranges to create a local makefile in the
# current directory if it does not exist. If building this is actually
# required, it goes no further, as a safety precaution to prevent nasty
# recursion.

all:         checklocalmake $(EDITME) $(SAMPLELINK) makefile os.h os.c config.h allexim

checklocalmake:
	     if ../scripts/newer $(EDITME)-$(OSTYPE) $(EDITME) || \
		../scripts/newer $(EDITME)-$(ARCHTYPE) $(EDITME) || \
		../scripts/newer $(EDITME)-$(OSTYPE)-$(ARCHTYPE) $(EDITME); \
	     then \
	       if [ -f $(EDITME) ]; then touch $(EDITME); fi \
	     fi
	     if ../scripts/newer $(EXIMON_EDITME)-$(OSTYPE) $(EXIMON_EDITME) || \
		../scripts/newer $(EXIMON_EDITME)-$(ARCHTYPE) $(EXIMON_EDITME) || \
		../scripts/newer $(EXIMON_EDITME)-$(OSTYPE)-$(ARCHTYPE) $(EXIMON_EDITME); \
	     then \
	       if [ -f $(EXIMON_EDITME) ]; then touch $(EXIMON_EDITME); fi \
	     fi
	     @echo ""

$(SAMPLELINK):
	     ../MakeLinks

$(EDITME):
	     @echo ""
	     @echo "*** Please create Local/Makefile by copying src/EDITME and making"
	     @echo "*** appropriate changes for your site."
	     @echo ""
	     @test ! -d ../Local && mkdir ../Local
	     @false

$(EXIMON_EDITME):
	     @echo ""
	     @echo "*** Please create Local/eximon.conf by copying exim_monitor/EDITME and making"
	     @echo "*** appropriate changes for your site."
	     @echo ""
	     @test ! -d ../Local && mkdir ../Local
	     @false

# Build the local makefile

makefile:    ../OS/Makefile-Base ../OS/Makefile-Default ../scripts/Configure $(EDITME)
	     $(SCRIPTS)/Configure-makefile
	     @echo ""; echo ">>> Please type 'make' again (ignore the following error messages)"; echo ""
	     @false

# Build (link) the os.h file

os.h:
	     $(SCRIPTS)/Configure-os.h

# Build the os.c file

os.c:        ../src/os.c
	     $(SCRIPTS)/Configure-os.c

# Build the config.h file.

config.h:    buildconfig ../src/config.h.defaults $(EDITME)
	     $(SCRIPTS)/Configure-config.h


# This is the real default target for all the various exim binaries and
# scripts, once the configuring stuff is done.

allexim:     config.h $(EXIM_MONITOR) exicyclog exinext exiwhat libident.a \
             exim_dbmbuild exim_dumpdb \
             exim_fixdb exim_tidydb directors.a routers.a \
             transports.a exim $(EXIM_MONITOR)


# The installation commands are kept in a separate script
install:
	     $(SCRIPTS)/exim_install

# Target for special-purpose configuration header builder
buildconfig: buildconfig.c
	     $(CC) -o buildconfig buildconfig.c

# Target for setting up the file containing the compile number
cnumber.h:;  @echo "0" > cnumber.h

# Target for the exicyclog utility script
exicyclog:   config.h ../src/exicyclog.src
	     @rm -f exicyclog
	     sed -e s?CONFIGURE_FILE?$(CONFIGURE_FILE)? \
		 -e s?BIN_DIRECTORY?$(BIN_DIRECTORY)? \
		 -e s?EXICYCLOG_MAX?$(EXICYCLOG_MAX)? \
		 -e s?COMPRESS_COMMAND?$(COMPRESS_COMMAND)? \
		 -e s?COMPRESS_SUFFIX?$(COMPRESS_SUFFIX)? \
	       ../src/exicyclog.src > exicyclog-t
	     @mv exicyclog-t exicyclog
	     @chmod a+x exicyclog
	     @echo ">>> exicyclog script built"; echo ""


# Target for the exinext utility script
exinext:     config.h ../src/exinext.src
	     @rm -f exinext
	     sed -e s?CONFIGURE_FILE?$(CONFIGURE_FILE)? -e s?BIN_DIRECTORY?$(BIN_DIRECTORY)? ../src/exinext.src > exinext-t
	     @mv exinext-t exinext
	     @chmod a+x exinext
	     @echo ">>> exinext script built"; echo ""

# Target for the exiwhat utility script
exiwhat:     config.h ../src/exiwhat.src
	     @rm -f exiwhat
	     sed -e s?CONFIGURE_FILE?$(CONFIGURE_FILE)? -e s?BIN_DIRECTORY?$(BIN_DIRECTORY)? ../src/exiwhat.src > exiwhat-t
	     @mv exiwhat-t exiwhat
	     @chmod a+x exiwhat
	     @echo ">>> exiwhat script built"; echo ""

# Target for the Exim monitor start-up script
eximon:      config.h ../src/eximon.src ../OS/eximon.conf-Default ../Local/eximon.conf
	     @rm -f eximon
	     ../scripts/Configure-eximon
	     sed -e s?CONFIGURE_FILE?$(CONFIGURE_FILE)? -e s?BIN_DIRECTORY?$(BIN_DIRECTORY)? ../src/eximon.src >> eximon
	     @echo ">>> eximon script built"; echo ""


# Targets for final binaries; the main one has a build number which is
# updated each time. We don't bother with that for the auxiliaries.

OBJ_EXIM =   accept.o child.o daemon.o db.o debug.o deliver.o direct.o \
             directory.o drtables.o exim.o expand.o filter.o globals.o \
             header.o host.o log.o match.o moan.o os.o parse.o queue.o \
             readconf.o regexp.o retry.o \
             rewrite.o route.o search.o smtp_in.o spool_in.o spool_out.o \
             store.o string.o tod.o transport.o tree.o verify.o version.o

exim.only:   libident.a directors.a routers.a transports.a exim

exim:        $(OBJ_EXIM) libident/libident.a directors/directors.a \
	     routers/routers.a transports/transports.a
	     awk '{ print ($$1+1) }' cnumber.h > cnumber.temp
	     /bin/rm -f cnumber.h; mv cnumber.temp cnumber.h
	     $(CC) -c $(CFLAGS) $(INCLUDE) version.c
	     /bin/rm -f exim
	     $(CC) -o exim $(LFLAGS) $(OBJ_EXIM) \
	       libident/libident.a directors/directors.a routers/routers.a \
	       transports/transports.a \
	       $(LIBS) $(EXTRALIBS) $(DBMLIB) $(LIBRESOLV)
	     $(EXIM_CHMOD)
	     @echo " "
	     @echo ">>> exim binary built"
	     @echo " "

# The utility for interrogating/fixing the contents of an exim database

OBJ_FIXDB =  exim_fixdb.o db.o debug.o directory.o globals.o log.o \
             os.o store.o string.o tod.o

exim_fixdb:  $(OBJ_FIXDB)
	     $(CC) -o exim_fixdb $(LFLAGS) $(OBJ_FIXDB) $(EXTRALIBS) $(DBMLIB)
	     @echo " "
	     @echo ">>> exim_fixdb utility built"
	     @echo " "

# The utility for dumping the contents of an exim database

OBJ_DUMPDB = exim_dumpdb.o db.o debug.o directory.o globals.o log.o \
             os.o store.o string.o tod.o

exim_dumpdb: $(OBJ_DUMPDB)
	     $(CC) -o exim_dumpdb $(LFLAGS) $(OBJ_DUMPDB) $(EXTRALIBS) $(DBMLIB)
	     @echo " "
	     @echo ">>> exim_dumpdb utility built"
	     @echo " "

# The utility for tidying the contents of an exim database

OBJ_TIDYDB = exim_tidydb.o db.o debug.o directory.o globals.o os.o log.o \
             store.o string.o tod.o

exim_tidydb: $(OBJ_TIDYDB)
	     $(CC) -o exim_tidydb $(LFLAGS) $(OBJ_TIDYDB) $(EXTRALIBS) $(DBMLIB)
	     @echo " "
	     @echo ">>> exim_tidydb utility built"
	     @echo " "

# The utility for building dbm files

exim_dbmbuild: exim_dbmbuild.c os.o config.h
	     $(CC) -o exim_dbmbuild $(LFLAGS) exim_dbmbuild.c os.o $(EXTRALIBS) $(DBMLIB)
	     @echo " "
	     @echo ">>> exim_dbmbuild utility built"
	     @echo " "

# The X-based Exim monitor program's binary part

MONBIN =     em_StripChart.o em_globals.o em_init.o em_log.o em_main.o \
	     em_menu.o em_queue.o em_string.o em_strip.o em_text.o \
	     em_xs.o em_version.o

OBJ_MONBIN = debug.o directory.o globals.o regexp.o log.o os.o spool_in.o \
             store.o string.o tod.o tree.o $(MONBIN)

eximon.bin:  $(EXIMON_EDITME) eximon $(OBJ_MONBIN)
	     $(CC) -o em_version.o -c \
	       $(CFLAGS) $(XINCLUDE) -I. ../exim_monitor/em_version.c
	     $(CC) -o eximon.bin $(XLFLAGS) \
	       $(OBJ_MONBIN) -lXaw -lXmu -lXt -lXext -lX11 \
	       $(LIBS) $(EXTRALIBS) $(EXTRALIBS_EXIMON) -lc
	     @echo " "
	     @echo ">>> exim monitor binary built"
	     @echo " "


# Compile step for most of the exim modules. HDRS is a list of headers
# which cause everthing to be rebuilt. PHDRS is the same, for the use
# of directors, routers, and transports. I can't find a way of doing this
# in one.

HDRS  =    exim.h    functions.h    globals.h    macros.h    structs.h
PHDRS = ../exim.h ../functions.h ../globals.h ../macros.h ../structs.h

.SUFFIXES:       .o .c
.c.o:;           $(CC) -c $(CFLAGS) $(INCLUDE) -I. $*.c

# Dependencies for the "ordinary" exim modules

accept.o:        $(HDRS) accept.c      config.h
child.o:         $(HDRS) child.c       config.h
daemon.o:        $(HDRS) daemon.c      config.h
db.o:            $(HDRS) db.c dbhdr.h  config.h
debug.o:         $(HDRS) debug.c
deliver.o:       $(HDRS) deliver.c     config.h
direct.o:        $(HDRS) direct.c
directory.o:     $(HDRS) directory.c
exim.o:          $(HDRS) exim.c        config.h
exim_dumpdb.o:   $(HDRS) exim_dumpdb.c dbhdr.h
exim_fixdb.o:    $(HDRS) exim_fixdb.c  dbhdr.h
exim_tidydb.o:   $(HDRS) exim_tidydb.c dbhdr.h
expand.o:        $(HDRS) expand.c
filter.o:        $(HDRS) filter.c
globals.o:       $(HDRS) globals.c     config.h
header.o:        $(HDRS) header.c
host.o:          $(HDRS) host.c        config.h
log.o:           $(HDRS) log.c         config.h
match.o:         $(HDRS) match.c
moan.o:          $(HDRS) moan.c
os.o:            $(HDRS) os.c
parse.o:         $(HDRS) parse.c
queue.o:         $(HDRS) queue.c
readconf.o:      $(HDRS) readconf.c    config.h
retry.o:         $(HDRS) retry.c
rewrite.o:       $(HDRS) rewrite.c
route.o:         $(HDRS) route.c
search.o:        $(HDRS) search.c      config.h
smtp_in.o:       $(HDRS) smtp_in.c
spool_in.o:      $(HDRS) spool_in.c
spool_out.o:     $(HDRS) spool_out.c   config.h
store.o:         $(HDRS) store.c
string.o:        $(HDRS) string.c
tod.o:           $(HDRS) tod.c
transport.o:     $(HDRS) transport.c   config.h
tree.o:          $(HDRS) tree.c
verify.o:        $(HDRS) verify.c
version.o:       $(HDRS) version.c     cnumber.h

# The regular expression routines

regexp.o:        $(REGEXP)/regexp.c $(REGEXP)/regexp.h $(REGEXP)/regmagic.h
		 $(CC) -o regexp.o -c $(CFLAGS) $(INCLUDE) \
		   -I$(REGEXP) $(REGEXP)/regexp.c

# The module containing tables of available directors, routers, and
# transports must be rebuilt if any of them are.

drtables.o:      $(HDRS) drtables.c config.h \
                 directors/directors.a routers/routers.a transports/transports.a


# The exim monitor's private modules - the sources live in a private
# subdirectory. The final binary combines the private modules with some
# modules from the main exim binary.

em_StripChart.o: ../exim_monitor/em_StripChart.c
em_globals.o:    ../exim_monitor/em_globals.c ../exim_monitor/em_hdr.h
em_init.o:       ../exim_monitor/em_init.c ../exim_monitor/em_hdr.h
em_log.o:        ../exim_monitor/em_log.c ../exim_monitor/em_hdr.h
em_main.o:       ../exim_monitor/em_main.c ../exim_monitor/em_hdr.h
em_menu.o:       ../exim_monitor/em_menu.c ../exim_monitor/em_hdr.h
em_queue.o:      ../exim_monitor/em_queue.c ../exim_monitor/em_hdr.h
em_string.o:     ../exim_monitor/em_string.c ../exim_monitor/em_hdr.h
em_strip.o:      ../exim_monitor/em_strip.c ../exim_monitor/em_hdr.h
em_text.o:       ../exim_monitor/em_text.c ../exim_monitor/em_hdr.h
em_xs.o:         ../exim_monitor/em_xs.c ../exim_monitor/em_hdr.h
em_version.o:    ../exim_monitor/em_version.c ../exim_monitor/em_hdr.h
$(MONBIN): $(HDRS)
		 $(CC) -o $@ -c $(CFLAGS) $(XINCLUDE) -I. -I../exim_monitor \
		   ../exim_monitor/`echo $@ | sed 's/o$$/c/'`

# The libident library. A file called libident.a is never actually
# created, so this target always does the cd and checks by using
# libident's own Makefile.

libident.a:
		 cd libident; $(MAKE) $(MFLAGS) RANLIB="$(RANLIB)" CC="$(CC)" CFLAGS="$(CFLAGS) $(LIBIDENTCFLAGS)" $(LIBIDENTNAME)
		 @echo " "

# The directors library. A file called directors.a is never actually
# created at this level. This target always does a cd and runs the
# Makefile in the subdirectory.

directors.a:
		 cd directors; $(MAKE) $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" RANLIB="$(RANLIB)" HDRS="$(PHDRS)" INCLUDE=$(INCLUDE)
		 @echo " "

# The routers library. A file called routers.a is never actually
# created at this level. This target always does a cd and runs the
# Makefile in the subdirectory.

routers.a:
		 cd routers; $(MAKE) $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" RANLIB="$(RANLIB)" HDRS="$(PHDRS)" INCLUDE=$(INCLUDE)
		 @echo " "

# The transports library. A file called transports.a is never actually
# created at this level. This target always does a cd and runs the
# Makefile in the subdirectory.

transports.a:
		 cd transports; $(MAKE) $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" RANLIB="$(RANLIB)" HDRS="$(PHDRS)" INCLUDE=$(INCLUDE)
		 @echo " "


# Tidy-up targets

clean:;          /bin/rm -f *.o directors/*.o routers/*.o transports/*.o \
		   libident/*.o

clean_exim:;     /bin/rm -f *.o directors/*.o transports/*.o routers/*.o

# Targets for building stand-alone testing programs for basic testing of
# some of the building blocks.

test_db:         config.h db.c directory.o globals.o log.o store.o tod.o version.o
		 $(CC) -o test_db -DSTAND_ALONE db.c \
		   debug.o directory.o globals.o log.o store.o tod.o version.o
		 /bin/rm -f db.o

test_expand:     config.h expand.c string.o directory.o log.o globals.o \
		   search.o store.o tod.o tree.o
		 $(CC) -o test_expand -DSTAND_ALONE expand.c \
		   directory.o globals.o log.o search.o store.o string.o \
		   tree.o tod.o
		 /bin/rm -f expand.o

test_host:       config.h host.c debug.o directory.o globals.o log.o store.o \
		 string.o tod.o
		 $(CC) -o test_host -DSTAND_ALONE host.c \
		   debug.o directory.o globals.o log.o store.o string.o tod.o \
		   $(LIBS) $(LIBRESOLV)
		 /bin/rm -f host.o

test_log:        config.h log.h log.c directory.o globals.o tod.o
		 $(CC) -o test_log -DSTAND_ALONE log.c \
		   directory.o globals.o tod.o
		 /bin/rm -f log.o

test_parse:      config.h parse.c debug.o directory.o globals.o log.o store.o string.o tod.o version.o
		 $(CC) -o test_parse -DSTAND_ALONE parse.c \
		   debug.o directory.o globals.o log.o store.o string.o tod.o version.o
		 /bin/rm -f parse.o
# End
