#
#
# Copyright (C) 1994 Swedish University Network (SUNET)
#
#
# This program is developed by UDAC, Uppsala University by commission
# of the Swedish University Network (SUNET). 
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 FITTNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#
#                                          Martin.Wendel@udac.uu.se
#					   Torbjorn.Wictorin@udac.uu.se
#
#                                          UDAC	
#                                          P.O. Box 174
#                                          S-751 04 Uppsala
#                                          Sweden
#
# Start of user defines
#######################

# Binaries will be installed in bindir and configuration files in
# libdir. Modify as needed.
#
prefix = /usr/local
exec_prefix = $(prefix)
binprefix =
manprefix =

bindir = $(exec_prefix)/bin
libdir = $(exec_prefix)/lib
mandir = $(prefix)/man
manext = 8

MAINCF = $(libdir)/emil.cf
CHARFILE = $(libdir)/charsets.cpl
LOG_FACILITY = LOG_MAIL

#
# Add extra facilities to the CLIENTS field.
#
# -DSMTP  	Enables the use of SMTP delivery.
# -DNAMED_BIND	Use nameserver to look up hostname when using SMTP.
# -DUSE_MX	Use MX-records instead of A-records.
#
# NAMED_BIND and USE_MX require the res_search() routine. If not
# found by configure (for example on standard SunOS) these will
# be ignored.
#
CLIENTS = -DSMTP -DNAMED_BIND -DUSE_MX

#
# Modify SMTPHOSTNAME (if using SMTP) to the FQDN of this host.
#
SMTPHOSTNAME = host.domain

# End of user defines
#####################

srcdir = @srcdir@
VPATH = @srcdir@
CC = @CC@
CPPFLAGS = -I.
CFLAGS = -g -DDEBUG
LDFLAGS = -g
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
EMILPATH = $(bindir)/emil
VERSION = 2.1.0-beta3
cffile = \"$(MAINCF)\"
charsetsfile = \"$(CHARFILE)\"
sendmailpath = \"$(SENDMAILPATH)\"
sendmailprog = \"$(SENDMAILPROG)\"
senderflag = \"$(SENDERFLAG)\"
MFLAGS = "CHARFILE=$(CHARFILE)" "CFLAGS=$(CFLAGS)" "LDFLAGS=$(LDFLAGS)"

DEFS = @DEFS@ $(CLIENTS) -DMAINCF=$(cffile) -DCHARFILE=$(charsetsfile) -DSENDMAILPATH=$(sendmailpath) -DSENDMAILPROG=$(sendmailprog) -DSENDERFLAG=$(senderflag) -DLOG_FACILITY=$(LOG_FACILITY) -DSMTPHOSTNAME=\"$(SMTPHOSTNAME)\" -DEMIL_VERSION=\"$(VERSION)\" -DEMILPATH=\"$(EMILPATH)\"
LIBS = @LIBS@
RANLIB = @RANLIB@
YACC = @YACC@
LEX = @LEX@
AR=		ar
SHELL=		/bin/sh

CHARDIR = $(srcdir)/charset


SRCS=	7bit.c appledouble.c applefile.c applesingle.c base64.c binary.c binhex.c \
	buffer.c convert.c filenames.c forks.c format.c hbase64.c header.c \
	hqp.c hqxbin.c load.c logger.c mailtool.c main.c message.c mime.c \
	mimeparse.c ns.c out.c qp.c rfc1522.c rfc822.c rfc822parse.c \
	smtp.c structure.c sunparse.c uuencode.c \
	s_attr.c d_attr.c a_attr.c
OBJS=	7bit.o appledouble.o applefile.o applesingle.o base64.o binary.o binhex.o \
	buffer.o convert.o filenames.o forks.o format.o hbase64.o header.o \
	hqp.o hqxbin.o load.o logger.o mailtool.o main.o message.o mime.o \
	mimeparse.o ns.o out.o qp.o rfc1522.o rfc822.o rfc822parse.o \
	smtp.o structure.o sunparse.o uuencode.o \
	s_attr.o d_attr.o a_attr.o
HDRS=	emil.h attr.h acconfig.h func.h

DISTFILES = $(SRCS) $(HDRS) COPYING ChangeLog Makefile.in emil.cf README.2.0.4\
README.FIRST README COPYRIGHT INSTALL configure.in mkinstalldirs emil.man \
TUTORIAL.html emil.cf.man helper.c getoption.l getoption.y configure \
config.h.in TODO README.2.1.0 \
BUGS DOCS charset.c charset/*.[ch] charset/*.req charset/*.def charset/*.doc \
charset/*.in charset/*.cm 

TESTFILES = testdir/*test testdir/emil.cf testdir/message[1-9]* \
testdir/README

all: emil man helper finish

finish:
	@echo " "
	@echo " "
	@echo "Now type 'make install' to install the necessery files"
	@echo " "
	@echo "Try the program \"helper\" to get support on how to setup"
	@echo "conversion on the local mailer of sendmail"

.c.o:   emil.h
	$(CC) -c $(CPPFLAGS) $(DEFS) -I$(srcdir) $(CFLAGS) $(UNIQUE) $<

helper: helper.c emil.h
	$(CC) -o helper $(CPPFLAGS) $(DEFS) -I$(srcdir) $(CFLAGS) $(UNIQUE) helper.c

CHARSET: emil.h
	cd $(CHARDIR); $(MAKE) $(MFLAGS) all

charset.o: charset.c emil.h $(CHARDIR)/strncnv.c
	$(CC) -c -I$(CHARDIR) $(DEFS) -DEMIL -I$(srcdir) $(CFLAGS) $(UNIQUE) charset.c


emil: CHARSET emilx


emilx: $(OBJS) charset.o getoption.o $(CHARDIR)/dfopen.o emil.h
	$(CC) -o emil $(LDFLAGS) $(LIBS) $(OBJS) charset.o getoption.o $(CHARDIR)/dfopen.o

getoption.o:		y.tab.c lex.yy.c emil.h
			$(CC) -c $(DEFS) $(CFLAGS) -o getoption.o y.tab.c

y.tab.c:		getoption.y
			$(YACC) -l -v getoption.y

lex.yy.c:		getoption.l
			$(LEX)  -i getoption.l


man: 
	cp emil.man emil.$(manext)
	cp emil.cf.man emil.cf.$(manext)
	@rm -f config.ed
	@echo "1,\$$s'^MAINCF'$(MAINCF)'g" > config.ed
	@echo "w"		>> config.ed
	@echo "q"		>> config.ed
	ed emil.cf.$(manext) <config.ed
	@rm -f config.ed
	@echo "1,\$$s'^MAINCF'$(MAINCF)'g" > config.ed
	@echo "1,\$$s'^CHARFILE'$(CHARFILE)'g"	>> config.ed
	@echo "w"		>> config.ed
	@echo "q"		>> config.ed
	ed emil.$(manext) <config.ed

manprint: man
	psroff -man emil.$(manext)
	psroff -man emil.cf.$(manext)

test:
	@cd testdir; \
	./emiltest Group ;\
	./emiltest Compound ;\
	./emiltest Buffer ;\
	./emiltest Misc;

install: installdirs
	@if test -f emil ; then \
		if test -f $(bindir)/emil ; then \
			echo " " ;\
			echo "Saving $(bindir)/emil in $(bindir)/emil.bak" ; \
			mv $(bindir)/emil $(bindir)/emil.bak ; \
		fi ; \
		echo " " ;\
		echo "Installing emil as $(bindir)/emil" ; \
		cp emil $(bindir) ; \
		chmod 0755 $(bindir)/emil ; \
	fi ;
	@if test -f emil.cf ; then \
		if test -f $(MAINCF) ; then \
			echo " " ;\
			echo "Keeping $(MAINCF) as is" ; \
			echo " " ;\
			echo "*********************************************" ;\
			echo "The syntax of emil.cf has changed somewhat." ;\
			echo "A new mailer field has been added." ;\
			echo "You should also check the Match lines in ./emil.cf" ;\
			echo "and include the ones not in your original" ; \
			echo "$(MAINCF)." ; \
			echo "*********************************************" ;\
		else \
			echo " " ;\
			echo "Installing emil.cf as $(MAINCF)" ; \
			 cp emil.cf $(MAINCF) ; \
			 chmod 644 $(MAINCF) ; \
		fi ; \
	fi ;
	@if test -f charset/charsets.cpl ; then \
		if test -f $(CHARFILE) ; then \
			echo " " ;\
			echo "Keeping $(CHARFILE) as is" ; \
		else \
			echo " " ;\
			echo "Installing charset/charsets.cpl as $(CHARFILE)";\
			 cp charset/charsets.cpl $(CHARFILE); \
			 chmod 444 $(CHARFILE) ; \
		fi ; \
	fi ;
	@if test -f emil.$(manext) ; then \
		echo " " ;\
		echo "Installing emil.$(manext) in $(mandir)/man$(manext)";\
		cp emil.$(manext) $(mandir)/man$(manext); \
		chmod 644 $(mandir)/man$(manext)/emil.$(manext) ; \
	fi ;
	@if test -f emil.cf.$(manext) ; then \
		echo " " ;\
		echo "Installing emil.cf.$(manext) in $(mandir)/man$(manext)";\
		cp emil.cf.$(manext) $(mandir)/man$(manext); \
		chmod 644 $(mandir)/man$(manext)/emil.cf.$(manext) ; \
	fi ;
	@echo " " ;
	@echo "Installation done." ;

installdirs:
	$(srcdir)/mkinstalldirs $(bindir) $(libdir) $(mandir)/man$(manext)

uninstall: 
	if test -f $(bindir)/emil ; then \
		rm $(bindir)/emil ; \
	fi ;\
	if test -f $(MAINCF) ; then \
		 rm $(MAINCF); \
	fi ;\
	if test -f $(CHARFILE) ; then \
		 rm $(CHARFILE); \
	fi ;\
	if test -f $(mandir)/man$(manext)/emil.$(manext) ; then \
		 rm $(mandir)/man$(manext)/emil.$(manext); \
	fi ;\
	if test -f $(mandir)/man$(manext)/emil.cf.$(manext) ; then \
		 em $(mandir)/man$(manext)/emil.cf.$(manext) ; \
	fi

Makefile: Makefile.in config.status
	$(SHELL) config.status

config.status: configure
	$(SHELL) config.status --recheck

configure: configure.in
	cd $(srcdir); autoconf

config.h.in: configure.in
	cd $(srcdir); autoheader

TAGS: $(SRCS)
	etags $(SRCS)

clean:
	rm -f *.o emil helper y.tab.c lex.yy.c getoption.o getoption.c core ; \
	cd charset; make clean

mostlyclean: clean

distclean: clean
	rm -f Makefile config.status config.h;\
	cd charset; make distclean

realclean: distclean
	rm -f TAGS

dist: $(DISTFILES)
	if test ! -d emil-$(VERSION); then \
		mkdir emil-$(VERSION); \
	fi ; \
	tar cvf - $(DISTFILES) | (cd emil-$(VERSION); tar xf -) ; \
	tar cf - emil-$(VERSION) | gzip > emil-$(VERSION).tar.gz

testdist: $(TESTFILES)
	tar cf testdir-$(VERSION).tar testdir; \
	gzip testdir-$(VERSION).tar

