srcdir=         @srcdir@
VPATH=          @srcdir@

SHELL=		/bin/sh
TOPDIR=		..
PZCONFIG=	$(TOPDIR)/zmailer.Config
INSTALL=	@INSTALL@
MKDIR=		@MKDIR@
MD5SUM=		@MD5SUM@

PMAILSHARE=	scheduler.conf sm.conf smtpserver.conf scheduler.auth \
		smtp-tls.conf
PMAILBIN=	mailrm.sh newaliases newfqdnaliases newdb zmailer.sh \
		newdbprocessor post-install.sh

all:
	@echo Usage: '"make [ dirs | config | mailbin | mailshare | db | cf | forms | guides | install | install-bin ]"'

install: dirs mailbin mailshare db cf forms guides

install-bin: dirs mailbin cf db

FRC:

$(PZCONFIG):
	@cd $(TOPDIR) ; $(MAKE) $(MFLAGS) zmailer.Config

$(MD5SUM):
	if [ "$(MD5SUM)" = "$(TOPDIR)/utils/md5sum" ]; then	\
	   cd $(TOPDIR); $(MAKE) $(MFLAGS) md5sum ;		\
	fi

# Ensure all necessary directories exist

dirs:	$(PZCONFIG)
	-@(. $(PZCONFIG) ;					\
	umask 022 ;						\
	if [ -n "$(prefix)" ] ; then				\
		$(MKDIR) -m 755 $(prefix) ;			\
	fi ;							\
	if [ ! -d $(prefix)$$MAILSHARE ]; then			\
		echo $(MKDIR) $(prefix)$$MAILSHARE &&		\
			$(MKDIR) $(prefix)$$MAILSHARE ; 	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILVAR ]; then			\
		echo $(MKDIR) $(prefix)$$MAILVAR &&		\
			$(MKDIR) $(prefix)$$MAILVAR ;		\
	fi ;							\
	if [ ! -d $(prefix)$$MAILVAR/db ]; then			\
		echo $(MKDIR) $(prefix)$$MAILVAR/db &&		\
			$(MKDIR) $(prefix)$$MAILVAR/db ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILVAR/db/proto ]; then		\
		echo $(MKDIR) $(prefix)$$MAILVAR/db/proto &&	\
			$(MKDIR) $(prefix)$$MAILVAR/db/proto ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILVAR/lists ]; then		\
		echo $(MKDIR) $(prefix)$$MAILVAR/lists &&	\
			$(MKDIR) $(prefix)$$MAILVAR/lists ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILVAR/fqlists ]; then		\
		echo $(MKDIR) $(prefix)$$MAILVAR/dqlists &&	\
			$(MKDIR) $(prefix)$$MAILVAR/fqlists ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILSHARE/forms ]; then		\
		echo $(MKDIR) $(prefix)$$MAILSHARE/forms &&	\
			$(MKDIR) $(prefix)$$MAILSHARE/forms ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILSHARE/forms/proto ]; then	\
		echo $(MKDIR) $(prefix)$$MAILSHARE/forms/proto && \
			$(MKDIR) $(prefix)$$MAILSHARE/forms/proto ; \
	fi ;							\
	if [ ! -d $(prefix)$$MAILSHARE/proto ]; then		\
		echo $(MKDIR) $(prefix)$$MAILSHARE/proto &&	\
			$(MKDIR) $(prefix)$$MAILSHARE/proto ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILSHARE/bak ]; then		\
		echo $(MKDIR) $(prefix)$$MAILSHARE/bak &&	\
			$(MKDIR) $(prefix)$$MAILSHARE/bak ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILSHARE/guides ]; then		\
		echo $(MKDIR) $(prefix)$$MAILSHARE/guides &&	\
			$(MKDIR) $(prefix)$$MAILSHARE/guides ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILBIN ]; then			\
		echo $(MKDIR) $(prefix)$$MAILBIN &&		\
			$(MKDIR) $(prefix)$$MAILBIN ;		\
	fi ;							\
	if [ ! -d $(prefix)$$MAILBIN/ta ]; then			\
		echo $(MKDIR) $(prefix)$$MAILBIN/ta &&		\
			$(MKDIR) $(prefix)$$MAILBIN/ta ;	\
	fi ;							\
	if [ ! -d $(prefix)$$MAILBIN/bak ]; then		\
		echo $(MKDIR) $(prefix)$$MAILBIN/bak &&		\
			$(MKDIR) $(prefix)$$MAILBIN/bak ;	\
	fi ;							\
	if [ ! -d $(prefix)$$POSTOFFICE ]; then			\
		echo $(MKDIR) $(prefix)$$POSTOFFICE &&		\
			$(MKDIR) $(prefix)$$POSTOFFICE ;	\
		chmod 2755 $(prefix)$$POSTOFFICE ;		\
		chmod g+s  $(prefix)$$POSTOFFICE ;		\
	fi;							\
	echo " " ;						\
	echo "Running  post-install  creates $$POSTOFFICE subdirs!"; \
	echo " " ;						\
	if [ ! -d $(prefix)$$LOGDIR ]; then			\
		echo $(MKDIR) $(prefix)$$LOGDIR &&		\
		$(MKDIR) $(prefix)$$LOGDIR ;			\
		chmod 755 $(prefix)$$LOGDIR ;			\
	fi )

clientdirs:	$(PZCONFIG)
	-@(. $(PZCONFIG) ;				\
	if [ ! -d $(prefix)$$MAILBIN ]; then		\
		echo $(MKDIR) $(prefix)$$MAILBIN &&	\
			$(MKDIR) $(prefix)$$MAILBIN ;	\
	fi )


# This is only good for testing "make dirs"
cleandirs: FRC $(PZCONFIG)
	-@( . $(PZCONFIG) ; \
	echo tar -cf /tmp/zmstuff.tar $(prefix)$$MAILBIN $(prefix)$$MAILSHARE $(prefix)$$POSTOFFICE && \
	tar -cf /tmp/zmstuff.tar $(prefix)$$MAILBIN $(prefix)$$MAILSHARE $(prefix)$$POSTOFFICE && \
	echo rm -rf $(prefix)$$MAILBIN $(prefix)$$MAILSHARE $(prefix)$$POSTOFFICE && \
	rm -rf $(prefix)$$MAILBIN $(prefix)$$MAILSHARE $(prefix)$$POSTOFFICE ; \
	rm -f +dirs )


mailbin: $(PZCONFIG) $(PMAILBIN) $(MD5SUM)
	-@(. $(PZCONFIG) ;					\
	if [ ! -d $(prefix)$$MAILBIN/bak ]; then $(MKDIR) $(prefix)$$MAILBIN/bak ; fi ; \
	for file in $(PMAILBIN) ;				\
	do							\
		name=`expr $$file : '\([^.]*\)'` ;		\
		echo $(INSTALL) -m 755 $$file $(prefix)$$MAILBIN/$$name ; \
		$(INSTALL) -m 755 $$file $(prefix)$$MAILBIN/$$name ; \
	done)

mailshare:	$(PZCONFIG) $(PMAILSHARE) $(MD5SUM)
	-@(. $(PZCONFIG) ; pwd=`pwd` ;				\
	MAILSHARE=$(prefix)$$MAILSHARE ;			\
	if [ ! -d $$MAILSHARE/bak ]; then $(MKDIR) $$MAILSHARE/bak ; fi ; \
	for file in $(PMAILSHARE) ;				\
	do							\
		(echo "# Do not edit this file, instead edit $$pwd/$$file" ; \
		echo '#' ; cat $$file) > ms.$$$$ ;		\
		echo $(INSTALL) -m 644 $$file $$MAILSHARE/proto/$$file ; \
		$(INSTALL) -m 644 ms.$$$$ $$MAILSHARE/proto/$$file ; \
		rm -f ms.$$$$ ;					\
	done )


forms:	FRC $(MD5SUM)
	@( . $(PZCONFIG) ;			\
	FORMSDIR="$(prefix)$$MAILSHARE/forms" ; \
	if [ ! -d "$$FORMSDIR/bak" ]; then	\
		$(MKDIR) "$$FORMSDIR/bak" ;	\
	fi ;					\
	if [ ! -d "$$FORMSDIR/proto" ]; then	\
		$(MKDIR) "$$FORMSDIR/proto" ;	\
	fi ;					\
	for file in ${srcdir}/forms/????* ;	\
	do					\
		name="`basename $$file`" ;	\
		echo $(INSTALL) -m 644 $$file $$FORMSDIR/proto/$$name ;	\
		$(INSTALL) -m 644 $$file $$FORMSDIR/proto/$$name ;	\
	done )


db:	FRC
	-@( . $(PZCONFIG) ;					\
	MAILVAR=$(prefix)$$MAILVAR ;				\
	MAILSHARE=$(prefix)$$MAILSHARE;				\
	if [ ! -d $$MAILVAR/db ]; then				\
		$(MKDIR) $$MAILVAR/db ;				\
	fi ;							\
	if [ ! -d $$MAILVAR/db/proto ]; then			\
		$(MKDIR) $$MAILVAR/db/proto ;			\
	fi ;							\
	echo "Don't forget to set up MAILVAR/db by hand -- if there is nothing, we seed it now" ; \
	$(INSTALL) -m 644 db/aliases $$MAILVAR/db/proto/aliases ; \
	for x in fqdnaliases   dbases.conf  localnames routes \
		 smtp-policy.mx smtp-policy.src smtp-policy.relay \
		 userdb kill-headers ;		\
	do							\
		$(INSTALL) -m 644 $(srcdir)/db/$$x $$MAILVAR/db/proto/$$x ; \
	done ; )

cf:	FRC  $(MD5SUM)
	-@(. $(PZCONFIG) ;			\
	MAILSHARE=$(prefix)$$MAILSHARE;		\
	if [ ! -d $$MAILSHARE/proto ]; then	\
		$(MKDIR) $$MAILSHARE/proto ;	\
	fi ;					\
	if [ ! -d $$MAILSHARE/cf ]; then	\
		$(MKDIR) $$MAILSHARE/cf ;	\
	fi ;					\
	if [ ! -d $$MAILSHARE/cf/proto ]; then	\
		$(MKDIR) $$MAILSHARE/cf/proto ; \
	fi ;					\
	if [ ! -d $$MAILSHARE/cf/fc ]; then	\
		$(MKDIR) $$MAILSHARE/cf/fc ;	\
	fi ;					\
	cp -p $(srcdir)/cf/*.cf $$MAILSHARE/cf/proto ;		\
	cp -p cf/*.cf $$MAILSHARE/cf/proto/ ;			\
	cp -p cf/SMTP.cf $$MAILSHARE/proto/router.cf ;		\
	/bin/rm -f `find $$MAILSHARE/. -name "*.fc" -print`	\
		foo-file ;					\
	)


guides:	FRC
	-. $(PZCONFIG) ;					\
	MAILSHARE=$(prefix)$$MAILSHARE ;			\
	src=$(srcdir)/$(TOPDIR)/doc/guides ;			\
	cp -p $$src/* $$MAILSHARE/guides/


clean:	FRC
	rm -f ./+* *~
distclean: clean
	rm -f Makefile sm.conf zmailer.sh
	rm -f newfqdnaliases newaliases newdb mailrm.sh
	rm -f scheduler.conf post-install.sh smtp-tls.conf
	rm -f smtpserver.conf newdbprocessor scheduler.auth
depend:
