#	$OpenBSD: Makefile.yp,v 1.13 2002/11/01 23:54:18 fgsch Exp $

YPDBDIR=/var/yp
DIR=/etc
AMDDIR=/etc/amd
NOPUSH=""
#
AWK=/usr/bin/awk
CAT=/bin/cat
CP=/bin/cp
RM=/bin/rm
CUT=/usr/bin/cut
ECHO=/bin/echo
MAKEDBM=/usr/sbin/makedbm
SED=/usr/bin/sed
SENDMAIL=/usr/sbin/sendmail
STDHOSTS=/usr/sbin/stdhosts
STDETHERS=/usr/sbin/stdethers
MKALIAS=/usr/sbin/mkalias
MKNETID=/usr/sbin/mknetid
REVNETGROUP=/usr/sbin/revnetgroup
TOUCH=/usr/bin/touch
DOMAIN="`/usr/bin/basename ${.CURDIR}`"
YPPUSH=/usr/sbin/yppush

# Password maps in standard YP is unsecure. This is due to the fact that
# passwords are accessable for anyone. FreeBSD and now OpenBSD has a common
# solution to this, maps can be secure (makedbm -s). If a map is secure only
# a privileged user can access it.
# For secure maps, leave UNSECURE blank.
MAKEDBM-S=$(MAKEDBM) -s
UNSECURE="True"

# If you want ypserv to ask DNS for unknown hosts set USEDNS to -b
USEDNS=

all: passwd group hosts ethers networks rpc services protocols netid netgroup amd.home aliases

passwd.time: $(DIR)/master.passwd
	-@if [ -f $(>) ]; then \
		if [ ! $(UNSECURE) ]; then \
			$(CAT) $(>) | $(CUT) -d: -f1-4,8-10 | \
			$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
			{ print $$1, $$1":*:"$$3":"$$4":"$$5":"$$6":"$$7 }' -|\
			$(MAKEDBM) - passwd.byname; \
			$(CAT) $(>) | $(CUT) -d: -f1-4,8-10 |\
			$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
			{ print $$3, $$1":*:"$$3":"$$4":"$$5":"$$6":"$$7 }' -|\
			$(MAKEDBM) - passwd.byuid; \
		else \
			$(CAT) $(>) | $(CUT) -d: -f1-4,8-10 | \
			$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
			{ print $$1, $$0 }' - | $(MAKEDBM) - passwd.byname; \
			$(CAT) $(>) | $(CUT) -d: -f1-4,8-10 |\
			$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
			{ print $$3, $$0 }' - | $(MAKEDBM) - passwd.byuid; \
		fi; \
		$(CAT) $(>) | \
		$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
		{ print $$1, $$0 }' - | $(MAKEDBM-S) - master.passwd.byname; \
		$(CAT) $(>) | \
		$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
		{ print $$3, $$0 }' - | $(MAKEDBM-S) - master.passwd.byuid; \
		$(TOUCH) $(@); \
		$(ECHO) "updated passwd"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) passwd.byname; \
			$(YPPUSH) -d $(DOMAIN) passwd.byuid; \
			$(YPPUSH) -d $(DOMAIN) master.passwd.byname; \
			$(YPPUSH) -d $(DOMAIN) master.passwd.byuid; \
			$(ECHO) "pushed passwd"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi


group.time: $(DIR)/group
	-@if [ -f $(>) ]; then \
		$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
		{ print $$1, $$0 }' $(>) | \
		$(MAKEDBM) - group.byname; \
		$(AWK) 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ \
		{ print $$3, $$0 }' $(>) | \
		$(MAKEDBM) - group.bygid; \
		$(TOUCH) $(@); \
		$(ECHO) "updated group"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) group.byname; \
			$(YPPUSH) -d $(DOMAIN) group.bygid; \
			$(ECHO) "pushed group"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi

hosts.time: $(DIR)/hosts
	-@if [ -f $(DIR)/hosts ]; then \
		$(STDHOSTS) $(>) | $(SED) -e s/#.*$$// | \
		$(AWK) '{for (i = 2; i <= NF; i++) print $$i, $$0 }' | \
		$(MAKEDBM) $(USEDNS) - hosts.byname; \
		$(STDHOSTS) $(>) | \
		$(AWK) 'BEGIN { OFS="\t"; } { print $$1, $$0 }' | \
		$(MAKEDBM) $(USEDNS) - hosts.byaddr; \
		$(TOUCH) $(@); \
		$(ECHO) "updated hosts"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) hosts.byname; \
			$(YPPUSH) -d $(DOMAIN) hosts.byaddr; \
			$(ECHO) "pushed hosts"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi

ethers.time: $(DIR)/ethers
	-@if [ -f $(DIR)/ethers ]; then \
		$(STDETHERS) $(>) | $(SED) -e s/#.*$$// | \
		$(AWK) 'BEGIN { FS="\t"; OFS="\t"; } { print $$1, $$0 }' | \
		$(MAKEDBM) - ethers.byaddr; \
		$(STDETHERS) $(>) | \
		$(AWK) 'BEGIN { FS="\t"; OFS="\t"; } { print $$2, $$0 }' | \
		$(MAKEDBM) - ethers.byname; \
		$(TOUCH) $(@); \
		$(ECHO) "updated ethers"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) ethers.byaddr; \
			$(YPPUSH) -d $(DOMAIN) ethers.byname; \
			$(ECHO) "pushed ethers"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi

networks.time: $(DIR)/networks
	-@if [ -f $(>) ]; then \
		$(SED) -e "/^#/d" -e s/#.*$$// $(>) | \
		$(AWK) '{print $$1, $$0; for (i = 3;i <= NF;i++) print $$i,$$0}' | \
		$(MAKEDBM) - networks.byname; \
		$(AWK) 'BEGIN { OFS="\t"; } $$1 !~ /^#/ { print $$2, $$0 }' $(>) | \
		$(MAKEDBM) - networks.byaddr; \
		$(TOUCH) $(@); \
		$(ECHO) "updated networks"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) networks.byname; \
			$(YPPUSH) -d $(DOMAIN) networks.byaddr; \
			$(ECHO) "pushed networks"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi

rpc.time: $(DIR)/rpc
	-@if [ -f $(>) ]; then \
		$(AWK) 'BEGIN { OFS="\t"; } $$1 !~ /^#/ { print $$2, $$0 }' $(>) | \
		$(MAKEDBM) - rpc.bynumber; \
		$(TOUCH) $(@); \
		$(ECHO) "updated rpc"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) rpc.bynumber; \
			$(ECHO) "pushed rpc"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi


services.time: $(DIR)/services
	-@if [ -f $(>) ]; then \
		$(AWK) 'BEGIN { OFS="\t"; } $$1 !~ /^#/ { print $$2, $$0 }' $(>) | \
		$(MAKEDBM) - services.byname; \
		$(TOUCH) $(@); \
		$(ECHO) "updated services"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) services.byname; \
			$(ECHO) "pushed services"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi


protocols.time: $(DIR)/protocols
	-@if [ -f $(>) ]; then \
		$(AWK) 'BEGIN { OFS="\t"; } $$1 !~ /^#/ { print $$2, $$0 }' $(>) | \
		$(MAKEDBM) - protocols.bynumber; \
		$(SED) -e "/^#/d" -e s/#.*$$// $(>) | \
		$(AWK) '{print $$1, $$0; for (i = 3;i <= NF;i++) print $$i,$$0}' | \
		$(MAKEDBM) - protocols.byname; \
		$(TOUCH) $(@); \
		$(ECHO) "updated protocols"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) protocols.bynumber; \
			$(YPPUSH) -d $(DOMAIN) protocols.byname; \
			$(ECHO) "pushed protocols"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi


netid.time: $(DIR)/passwd $(DIR)/group $(DIR)/hosts $(DIR)/netid
	-@$(MKNETID) -q -d $(DOMAIN) -p $(DIR)/passwd -g $(DIR)/group -h $(DIR)/hosts -m $(DIR)/netid | \
	  $(MAKEDBM) - netid.byname; \
	  $(TOUCH) $(@); \
	  $(ECHO) "updated netid"; \
	  if [ ! $(NOPUSH) ]; then \
		$(YPPUSH) -d $(DOMAIN) netid.byname; \
		$(ECHO) "pushed netid"; \
	  else \
		: ; \
	  fi


netgroup.time: $(DIR)/netgroup
	-@if [ -f $(>) ]; then \
		$(CAT) $(>) | $(MAKEDBM) - netgroup; \
		$(CAT) $(>) | $(REVNETGROUP) -u -f - | \
		$(MAKEDBM) - netgroup.byuser; \
		$(CAT) $(>) | $(REVNETGROUP) -h -f - | \
		$(MAKEDBM) - netgroup.byhost; \
		$(TOUCH) $(@); \
		$(ECHO) "updated netgroup"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) netgroup; \
			$(YPPUSH) -d $(DOMAIN) netgroup.byuser; \
			$(YPPUSH) -d $(DOMAIN) netgroup.byhost; \
			$(ECHO) "pushed netgroup"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi


amd.home.time: $(AMDDIR)/amd.home
	-@if [ -f $(>) ]; then \
		$(SED) -e "s/#.*$$//" -e "/^$$/d" $(>) | \
		$(AWK) '{ \
			    for (i = 1; i <= NF; i++) \
				if (i == NF) { \
				    if (substr($$i, length($$i), 1) == "\\") { \
					printf("%s", substr($$i, 1, length($$i) - 1)); \
				    } \
				    else \
					printf("%s\n", $$i); \
				} \
				else \
				    printf("%s ", $$i); \
			}' | \
		$(MAKEDBM) - amd.home; \
		$(TOUCH) $(@); \
		$(ECHO) "updated amd.home"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) amd.home; \
			$(ECHO) "pushed amd.home"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi


aliases.time: $(DIR)/mail/aliases
	-@if [ -f $(>) ]; then \
		$(CP) -p $(>) $(DOMAIN)-aliases; \
		$(SENDMAIL) -bi -oA$(PWD)/$(DOMAIN)-aliases; \
		$(MAKEDBM) -U $(DOMAIN)-aliases | $(MAKEDBM) - mail.aliases; \
		$(MKALIAS) mail.aliases mail.byaddr; \
		$(TOUCH) $(@); \
		$(RM) $(DOMAIN)-aliases.db $(DOMAIN)-aliases; \
		$(ECHO) "updated aliases"; \
		if [ ! $(NOPUSH) ]; then \
			$(YPPUSH) -d $(DOMAIN) mail.aliases; \
			$(YPPUSH) -d $(DOMAIN) mail.byaddr; \
			$(ECHO) "pushed aliases"; \
		else \
			: ; \
		fi \
	else \
		$(ECHO) "couldn't find $(>)"; \
	fi



passwd: passwd.time
group: group.time
hosts: hosts.time
ethers: ethers.time
networks: networks.time
rpc: rpc.time
services: services.time
protocols: protocols.time
netid: netid.time
netgroup: netgroup.time
amd.home: amd.home.time
aliases: aliases.time
$(DIR)/passwd:
$(DIR)/group:
$(DIR)/hosts:
$(DIR)/ethers:
$(DIR)/networks:
$(DIR)/rpc:
$(DIR)/services:
$(DIR)/protocols:
$(DIR)/netid:
$(DIR)/master.passwd:
$(DIR)/netgroup:
$(DIR)/mail/aliases:
$(AMDDIR)/amd.home:
