VPATH = $(PWD)

################################## EDIT THESE ################################
# This is the address where people should e-mail users on your system at.
REPLY_TO = bcr@physics.purdue.edu

# This where your smail director is.
SMAIL_LIB = /usr/lib/smail

# This is where the new binaries should be installed
INSTALL_IN = /usr/local/lib/term

##############################################################################
# The rest of this is the makefile rules.

LN = ln -sf
CP = cp -f
RM = rm -f

setup: all

all: all1 all2

all1: termgetopt termmkline termmksort termdcasehost \
	termmkdbm termpathmerge termgleem termunsharmap \
	termwrite termuuwho termsmail crontab 

all2:	$(SMAIL_LIB)/config $(SMAIL_LIB)/transports $(SMAIL_LIB)/routers \
	$(INSTALL_IN)/update_mail

$(INSTALL_IN)/update_mail: update_mail
	$(CP) update_mail $(INSTALL_IN)/update_mail

$(SMAIL_LIB)/routers: routers 
	$(CP) routers $(SMAIL_LIB)/routers

$(SMAIL_LIB)/config: config
	sed "s/HOSTNAME/`$(HOSTNAME)`/g" < config > $(SMAIL_LIB)/config
	touch config

$(SMAIL_LIB)/transports: transports
	if [ $(REPLY_TO) = bcr@physics.purdue.edu ]; then \
		echo You must edit this makefile first. ; \
		exit 1 ; \
	else true; fi
	sed 's/REPLY_TO/$(REPLY_TO)/g' < $(PWD)/transports > $(SMAIL_LIB)/transports
	touch transports

termsmail: $(INSTALL_IN)/termsmail
$(INSTALL_IN)/termsmail: $(shell which smail)
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termuuwho: $(INSTALL_IN)/termuuwho
$(INSTALL_IN)/termuuwho: $(shell which uuwho)
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termwrite: $(INSTALL_IN)/termwrite
$(INSTALL_IN)/termwrite: $(shell which write)
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termgetopt: $(INSTALL_IN)/termgetopt
$(INSTALL_IN)/termgetopt: $(SMAIL_LIB)/getopt
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termmkline: $(INSTALL_IN)/termmkline
$(INSTALL_IN)/termmkline: $(SMAIL_LIB)/mkline
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termmksort: $(INSTALL_IN)/termmksort
$(INSTALL_IN)/termmksort: $(SMAIL_LIB)/mksort
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termdcasehost: $(INSTALL_IN)/termdcasehost
$(INSTALL_IN)/termdcasehost: $(SMAIL_LIB)/dcasehost
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termmkdbm: $(INSTALL_IN)/termmkdbm
$(INSTALL_IN)/termmkdbm: $(SMAIL_LIB)/mkdbm
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termpathmerge: $(INSTALL_IN)/termpathmerge
$(INSTALL_IN)/termpathmerge: $(SMAIL_LIB)/pathmerge
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termgleem: $(INSTALL_IN)/termgleem
$(INSTALL_IN)/termgleem: $(SMAIL_LIB)/gleem
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

termunsharmap: $(INSTALL_IN)/termunsharmap
$(INSTALL_IN)/termunsharmap: $(SMAIL_LIB)/unsharmap
	cd $(INSTALL_IN); termify $?
	$(LN) $@ $?

crontab:
	@crontab -l | awk \
		'{if ((x += ($$1 != "#")) > 0 && $$0 !~ /update_mail /)print}'\
		 > crontab
	echo '* * * * * 	$(INSTALL_IN)/update_mail > /dev/null 2>&1' >> crontab
	crontab -r crontab
	$(RM) crontab
	
