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

PMAILBIN=	mailrm.sh newaliases newfqdnaliases zmailer.sh
PMAILSHARE=	scheduler.conf sm.conf

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

install: dirs config mailbin mailshare db cf forms

FRC:

$(PZCONFIG):
	(cd $(TOPDIR) ; make config)

# Ensure all necessary directories exist

dirs:	$(PZCONFIG)
	-@. $(PZCONFIG) ; \
	umask 022 ; \
	if [ ! -d $$MAILSHARE ]; then \
		echo mkdir $$MAILSHARE && mkdir $$MAILSHARE ; \
	fi ; \
	if [ ! -d $$MAILVAR ]; then \
		echo mkdir $$MAILVAR && mkdir $$MAILVAR ; \
	fi ; \
	if [ ! -d $$MAILVAR/db ]; then \
		echo mkdir $$MAILVAR/db && mkdir $$MAILVAR/db ; \
	fi ; \
	if [ ! -d $$MAILVAR/lists ]; then \
		echo mkdir $$MAILVAR/lists && mkdir $$MAILVAR/lists ; \
	fi ; \
	if [ ! -d $$MAILSHARE/forms ]; then \
		echo mkdir $$MAILSHARE/forms && mkdir $$MAILSHARE/forms ; \
	fi ; \
	if [ ! -d $$MAILSHARE/bak ]; then \
		echo mkdir $$MAILSHARE/bak && mkdir $$MAILSHARE/bak ; \
	fi ; \
	if [ ! -d $$MAILBIN ]; then \
		echo mkdir $$MAILBIN && mkdir $$MAILBIN ; \
	fi ; \
	if [ ! -d $$MAILBIN/ta ]; then \
		echo mkdir $$MAILBIN/ta && mkdir $$MAILBIN/ta ; \
	fi ; \
	if [ ! -d $$MAILBIN/bak ]; then \
		echo mkdir $$MAILBIN/bak && mkdir $$MAILBIN/bak ; \
	fi ; \
	if [ ! -d $$POSTOFFICE ]; then \
		echo mkdir $$POSTOFFICE && mkdir $$POSTOFFICE ; \
		chmod 2755 $$POSTOFFICE ; \
	fi; \
	if [ ! -d $$POSTOFFICE/deferred ]; then \
		echo mkdir $$POSTOFFICE/deferred && \
		mkdir $$POSTOFFICE/deferred ; \
		chmod 754 $$POSTOFFICE/deferred ; \
	fi; \
	if [ ! -d $$POSTOFFICE/freezer ]; then \
		echo mkdir $$POSTOFFICE/freezer && \
		mkdir $$POSTOFFICE/freezer ; \
		chmod 754 $$POSTOFFICE/freezer ; \
	fi; \
	if [ ! -d $$POSTOFFICE/postman ]; then \
		echo mkdir $$POSTOFFICE/postman && \
		mkdir $$POSTOFFICE/postman ; \
		chmod 750 $$POSTOFFICE/postman ; \
	fi; \
	if [ ! -d $$POSTOFFICE/public ]; then \
		echo mkdir $$POSTOFFICE/public && \
		mkdir $$POSTOFFICE/public ; \
		chmod 1777 $$POSTOFFICE/public ; \
	fi; \
	if [ ! -d $$POSTOFFICE/queue ]; then \
		echo mkdir $$POSTOFFICE/queue && \
		mkdir $$POSTOFFICE/queue ; \
		chmod 750 $$POSTOFFICE/queue ; \
		echo mkdir "$$POSTOFFICE/queue/{A,B,..,Z}" ; \
		for hh in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do \
			mkdir $$POSTOFFICE/queue/$$hh ;	\
			chmod 755 $$POSTOFFICE/queue/$$hh ;	\
		done ; \
	fi; \
	if [ ! -d $$POSTOFFICE/router ]; then \
		echo mkdir $$POSTOFFICE/router && \
		mkdir $$POSTOFFICE/router ; \
		chmod 1777 $$POSTOFFICE/router ; \
	fi; \
	if [ ! -d $$POSTOFFICE/transport ]; then \
		echo mkdir $$POSTOFFICE/transport && \
		mkdir $$POSTOFFICE/transport ; \
		chmod 755 $$POSTOFFICE/transport ; \
		echo mkdir "$$POSTOFFICE/transport/{A,B,..,Z}" ; \
		for hh in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z; do \
			mkdir $$POSTOFFICE/transport/$$hh ;	\
			chmod 755 $$POSTOFFICE/transport/$$hh ;	\
		done ; \
	fi

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


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


config: +config
#
#	Config so that  newaliases retains  $MAILBIN/router  ??
#
+config: $(PZCONFIG) $(PMAILBIN)
	touch +config	# The MAILBIN is substituted by the autoconf!

old+config: $(PZCONFIG) $(PMAILBIN)
	-@. $(PZCONFIG) ; \
	for file in $(PMAILBIN) ; \
	do \
		echo updating $$file ; \
		sed	-e 's:^\ZCONFIG=.*$$:ZCONFIG='$$ZCONFIG':' \
			-e 's:^#![^ ]*router :#!'$$MAILBIN'/router :' \
			< $$file  > $$file.new;			\
		mv $$file $$file.old ; mv $$file.new $$file;	\
	done
	touch +config


mailbin: +mailbin

+mailbin: $(PZCONFIG) $(PMAILBIN)
	-@. $(PZCONFIG) ; \
	for file in $(PMAILBIN) ; \
	do \
		name=`expr $$file : '\([^.]*\)'` ; \
		test -f $$MAILBIN/$$name && \
			test -f $$MAILBIN/bak/$$name || \
			echo mv -f $$MAILBIN/$$name $$MAILBIN/bak/$$name && \
			mv -f $$MAILBIN/$$name $$MAILBIN/bak/$$name ; \
		echo $(INSTALL) -m 755 $$file $$MAILBIN/$$name ; \
		$(INSTALL) -m 755 $$file $$MAILBIN/$$name ; \
	done
	@touch +mailbin

mailshare:	+mailshare

+mailshare:	$(PZCONFIG) $(PMAILSHARE)
	-@. $(PZCONFIG) ; pwd=`pwd` ; \
	for file in $(PMAILSHARE) ; \
	do \
		test -f $$MAILSHARE/$$file && \
			test -f $$MAILSHARE/bak/$$file || \
			echo mv -f $$MAILSHARE/$$file $$MAILSHARE/bak/$$file &&\
			mv -f $$MAILSHARE/$$file $$MAILSHARE/bak/$$file ; \
		echo $(INSTALL) -m 644 $$file $$MAILSHARE/$$file ; \
		(echo "# Do not edit this file, instead edit $$pwd/$$file" ; \
		echo '#' ; cat $$file) > ms.$$$$ ; \
		$(INSTALL) -m 644 ms.$$$$ $$MAILSHARE/$$file ; \
		rm -f ms.$$$$ ; \
	done
	@touch +mailshare


forms:	+forms

+forms:	forms/???*
	@. $(PZCONFIG) ; \
	FORMSDIR=$$MAILSHARE/forms ; \
	if [ ! -d $$FORMSDIR/bak ]; then \
		mkdir $$FORMSDIR/bak ; \
	fi ; \
	for file in $? ; \
	do \
		name=`expr $$file : '.*/\(.*\)'` ; \
		if [ $$name != bak -a -f $$FORMSDIR/$$name ]; then \
			echo mv -f $$FORMSDIR/$$name $$FORMSDIR/bak/$$name ; \
			mv -f $$FORMSDIR/$$name $$FORMSDIR/bak/$$name ; \
		fi ; \
		if [ $$name != bak ]; then \
			echo $(INSTALL) -m 644 $$file $$FORMSDIR/$$name ; \
			$(INSTALL) -m 644 $$file $$FORMSDIR/$$name ; \
		fi ; \
	done
	@touch +forms


db:	FRC
	-@. $(PZCONFIG) ; \
	echo "Don't forget to set up MAILVAR/db by hand -- if there is nothing, we seed it now" ; \
	if [ ! -f $$MAILVAR/db/aliases ]; then \
		echo $(INSTALL) -m 644 db/aliases $$MAILVAR/db/aliases ; \
		$(INSTALL) -m 644 db/aliases $$MAILVAR/db/aliases ; \
	fi ; \
	if [ ! -f $$MAILVAR/db/fqdnaliases ]; then \
		echo $(INSTALL) -m 644 db/fqdnaliases $$MAILVAR/db/fqdnaliases ; \
		$(INSTALL) -m 644 db/fqdnaliases $$MAILVAR/db/fqdnaliases ; \
	fi ; \
	if [ ! -f $$MAILVAR/db/routes ]; then \
		echo $(INSTALL) -m 644 db/routes $$MAILVAR/db/routes ; \
		$(INSTALL) -m 644 db/routes $$MAILVAR/db/routes ; \
	fi ; \
	if [ ! -f $$MAILVAR/db/localnames ]; then \
		echo $(INSTALL) -m 644 db/localnames $$MAILVAR/db/localnames ; \
		$(INSTALL) -m 644 db/localnames $$MAILVAR/db/localnames ; \
	fi

cf:	FRC
	-@. $(PZCONFIG) ; \
	if [ ! -d $$MAILSHARE/cf ]; then \
		mkdir $$MAILSHARE/cf ; \
		mkdir $$MAILSHARE/cf/fc ; \
		cp cf/*.cf $$MAILSHARE/cf ; \
		: cp cf/SMTP+UUCP.cf $$MAILSHARE/router.cf ; \
	fi

clean:	FRC
	rm -f ./+* *~
distclean: clean
	rm -f Makefile
depend:
