# Semantics:
# 
# A user in the passwd file gets mail locally if he's in
# /etc/localusers and not in /etc/non-local-users.
# 
# The names of aliases in /etc/aliases must be in /etc/localusers, so
# they are extracted automatically.
# 
# The files passwd.localusers and non-local-users should be maintained
# in this directory.  The former contains a list of users in the
# passwd file whose mail should be delivered locally when it is sent
# locally with no host name (i.e., mail sent on charon/picayune/bed with
# just "user" as the address).  The latter contains a list of people
# whose mail should be sent to the mail hub even when @$host.mit.edu
# is explicitly specified.

TD= /tmp/sendmail-tmp
SHELL= /bin/sh
CFS= sendmail.cf.charon sendmail.cf.bloom-picayune sendmail.cf.senator-bedfellow sendmail.cf.sipb-server-1 sendmail.cf.opus sendmail.cf.penguin-lust

all: $(CFS)

# I'm using sed rather than grep in the following two commands,
# because grep exits with non-zero status when it doesn't print
# anything.

sendmail.cf.senator-bedfellow: sendmail.ease
	et -Dfoo -Dsenator_bedfellow sendmail.ease sendmail.cf.senator-bedfellow \
		2>&1 | sed '/Appending to previously defined/d' 1>&2

sendmail.cf.bloom-picayune: sendmail.ease
	et -Dfoo -Dbloom_picayune sendmail.ease sendmail.cf.bloom-picayune \
		2>&1 | sed '/Appending to previously defined/d' 1>&2

sendmail.cf.charon: sendmail.ease
	et -Dfoo -Dcharon sendmail.ease sendmail.cf.charon \
		2>&1 | sed '/Appending to previously defined/d' 1>&2

sendmail.cf.sipb-server-1: sendmail.ease
	et -Dfoo -Dsipb_server_1 sendmail.ease sendmail.cf.sipb-server-1 \
		2>&1 | sed '/Appending to previously defined/d' 1>&2

sendmail.cf.opus: sendmail.ease
	et -Dfoo -Dopus sendmail.ease sendmail.cf.opus \
		2>&1 | sed '/Appending to previously defined/d' 1>&2

sendmail.cf.penguin-lust: sendmail.ease
	et -Dfoo -Dpenguin_lust sendmail.ease sendmail.cf.penguin-lust \
		2>&1 | sed '/Appending to previously defined/d' 1>&2

install: install-aliases install-cf
	install -c -m 0555 make-aliases /etc/make-aliases

install-aliases: check-cf
	-rm -rf $(TD)
	mkdir $(TD)
	chmod 700 $(TD)
	perl parse-aliases.pl /etc/aliases > $(TD)/aliases.users
	sort -u passwd.localusers.`hostname` $(TD)/aliases.users > \
		$(TD)/valid.users
	sort -u non-local-users.`hostname` | comm -23 $(TD)/valid.users - > \
		/etc/localusers.new
	test -r /etc/localusers || touch /etc/localusers
	mv /etc/localusers /etc/localusers.bak
	mv /etc/localusers.new /etc/localusers
	test -r /etc/non-local-users || touch /etc/non-local-users
	mv /etc/non-local-users /etc/non-local-users.bak
	install -c -m 0444 non-local-users.`hostname` /etc/non-local-users
	/usr/ucb/newaliases
	chmod -w /etc/aliases.*
	@echo "*** " 1>&2
	@echo "*** If you've added or deleted an alias, you should probably" 1>&2
	@echo "*** Kill and restart the daemon sendmail." 1>&2
	-rm -rf $(TD)

check-cf: /etc/sendmail.cf

/etc/sendmail.cf: sendmail.cf
	@echo "*** " 1>&2
	@echo "*** Master sendmail.cf (in `pwd`)" 1>&2
	@echo "*** is newer than /etc/sendmail.cf.  You may want to run" 1>&2
	@echo "*** 'make install-cf' in `pwd`." 1>&2

install-cf: sendmail.cf
	perl mreceived.pl < sendmail.cf.`hostname` > sendmail.cf.installed.`hostname`
	install -c -m 0444 sendmail.cf.installed.`hostname` /etc/sendmail.cf

clean:
	-rm -f $(CFS) *~
