# 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.

SHELL      = /bin/sh
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 makefile is rebuilt, it is run
# in order to do the work using the new settings; this turns out to be
# immensely tedious to get to work. "Make" is not a convenient programming
# language. Firstly, you can't force make to stop without it giving an
# error message, so you need a "makefile rebuilt" flag to stop subsequent
# targets in the top makefile from being processed - although that does no
# harm, it does cause annoying messages. There is also the difficulty of
# detecting when the rebuild has happened so that a nested make can be
# called. The same flag is used for both this things: it is a file called
# makefile-rebuilt. The premakefile target ensures it does not exist; the
# postmakefile target calls an nested make if it does, and then recreates it
# (since the nested make will have remoted it again). Then the targets
# that are affected (libident.a, directors.a, routers.a, and transports.a)
# test for the file and don't try to rebuild if it is set. In the last of
# these, we remove the file. As nothing should fail while this is going on,
# we should never get left with the file existing when it shouldn't. However,
# that doesn't matter as it is removed at the start; this is necessary in case
# an explicit "make makefile" is called because that will leave it around.

all:         checklocalmake $(EDITME) $(SAMPLELINK) premakefile makefile \
             postmakefile 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

# Ensure that the makefile-rebuilt flag file does not exist

premakefile:
	     @/bin/rm -f makefile-rebuilt

# Build the local makefile and create makefile-rebuilt if it was rebuilt

makefile:    ../OS/Makefile-Base ../OS/Makefile-Default ../scripts/Configure $(EDITME)
	     $(SCRIPTS)/Configure-makefile
	     @touch makefile-rebuilt

# If the local makefile was rebuilt, run it, and then put the flag
# back, because running the makefile will have removed it (necessarily,
# in order to get the drivers to build).

postmakefile:
	     @if [ -f makefile-rebuilt ] ; then \
	     $(MAKE) -f makefile ; \
	     touch makefile-rebuilt ; \
	     fi

# 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) $(CFLAGS) -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:   makefile 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)? \
		 -e s?CHOWN_COMMAND?$(CHOWN_COMMAND)? \
		 -e s?CHGRP_COMMAND?$(CHGRP_COMMAND)? \
		 -e s?MV_COMMAND?$(MV_COMMAND)? \
		 -e s?RM_COMMAND?$(RM_COMMAND)? \
	       ../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:     makefile 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:     makefile config.h ../src/exiwhat.src
	     @rm -f exiwhat
	     sed -e s?CONFIGURE_FILE?$(CONFIGURE_FILE)? \
	       -e s?BIN_DIRECTORY?$(BIN_DIRECTORY)? \
	       -e s?EXIWHAT_PS_ARG?$(EXIWHAT_PS_ARG)? \
	       -e s?EXIWHAT_KILL_ARG?$(EXIWHAT_KILL_ARG)? \
	       -e s?EXIWHAT_EGREP_ARG?$(EXIWHAT_EGREP_ARG)? \
	       ../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:      makefile 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)? \
		 -e s?BASENAME_COMMAND?$(BASENAME_COMMAND)? \
		 -e s?HOSTNAME_COMMAND?$(HOSTNAME_COMMAND)? \
		 ../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) $(IPV6_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) $(IPV6_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) $(CFLAGS) -o exim_fixdb $(LFLAGS) $(OBJ_FIXDB) $(LIBS) \
	       $(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) $(CFLAGS) -o exim_dumpdb $(LFLAGS) $(OBJ_DUMPDB) $(LIBS) \
	       $(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) $(CFLAGS) -o exim_tidydb $(LFLAGS) $(OBJ_TIDYDB) $(LIBS) \
	       $(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) $(CFLAGS) -o exim_dbmbuild $(LFLAGS) exim_dbmbuild.c os.o \
	       $(LIBS) $(EXTRALIBS) $(DBMLIB)
	     @echo " "
	     @echo ">>> exim_dbmbuild utility built"
	     @echo " "

# The X-based Exim monitor program's binary part. There's a macro for cutting
# out the modified TextPop module, because some antique link editors cannot
# handle the fact that it is redefining things that are found later in the
# Xaw library.

MONBIN =     em_StripChart.o $(EXIMON_TEXTPOP) 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 header.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) $(LIBS_EXIMON) $(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) $(IPV6_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      config.h
filter.o:        $(HDRS) filter.c      config.h
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       config.h
moan.o:          $(HDRS) moan.c
os.o:            $(HDRS) os.c
parse.o:         $(HDRS) parse.c       config.h
queue.o:         $(HDRS) queue.c       config.h
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     config.h
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      config.h
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_TextPop.o:    ../exim_monitor/em_TextPop.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. We have to use a fudge for times when the
# makefile has been rebuilt, because it still comes along this target
# afterwards when it gets back to the old makefile. The makefile target
# touches makefile-rebuilt after a successful call to the new makefile.
# We test it here and do nothing if it exists.

libident.a:
		 @if [ ! -f makefile-rebuilt ] ; then \
		 cd libident; $(MAKE) $(MFLAGS) RANLIB="$(RANLIB)" CC="$(CC)" \
		   CFLAGS="$(CFLAGS) $(LIBIDENTCFLAGS)" $(LIBIDENTNAME); \
		 echo " "; fi

# 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. Same fudge for rebuilt makefiles as
# for libident.

directors.a:
		 @if [ ! -f makefile-rebuilt ] ; then \
		 cd directors; $(MAKE) $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
		   RANLIB="$(RANLIB)" HDRS="$(PHDRS)" \
		   INCLUDE="$(INCLUDE) $(IPV6_INCLUDE)"; \
		 echo " "; fi

# 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. Same fudge for rebuilt makefiles as
# for libident.

routers.a:
		 @if [ ! -f makefile-rebuilt ] ; then \
		 cd routers; $(MAKE) $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
		   RANLIB="$(RANLIB)" HDRS="$(PHDRS)" \
		   INCLUDE="$(INCLUDE) $(IPV6_INCLUDE)"; \
		 echo " "; fi

# 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. Same fudge for rebuilt makefiles as
# for libident.

transports.a:
		 @if [ ! -f makefile-rebuilt ] ; then \
		 cd transports; $(MAKE) $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)" \
		   RANLIB="$(RANLIB)" HDRS="$(PHDRS)" \
		   INCLUDE="$(INCLUDE) $(IPV6_INCLUDE)"; \
		 echo " "; else /bin/rm -f makefile-rebuilt; fi


# Tidy-up targets

clean:;          @echo ""; echo '*** "make clean" just removes all .o files'
		 @echo '*** Use "make makefile" to force a rebuild of the makefile'
		 @echo ""
		 /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) $(CFLAGS) -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 \
		   regexp.o search.o store.o tod.o tree.o
		 $(CC) $(CFLAGS) -o test_expand -DSTAND_ALONE expand.c debug.o \
		   directory.o globals.o log.o regexp.o search.o store.o \
		   string.o tree.o tod.o $(DBMLIB) $(LIBS)
		 /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) $(CFLAGS) -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) $(CFLAGS) -o test_log -DSTAND_ALONE log.c \
		   directory.o globals.o tod.o
		 /bin/rm -f log.o

test_os:         os.h os.c
		 $(CC) $(CFLAGS) -o test_os -DSTAND_ALONE os.c $(LIBS)
		 /bin/rm -f os.o

test_parse:      config.h parse.c debug.o directory.o globals.o log.o store.o string.o tod.o version.o
		 $(CC) $(CFLAGS) -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

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

# End
