#
# Makefile for the NIS databases
#
# $Id: ypMakefile.in,v 1.3 1997/01/09 20:21:22 kukuk Exp $
#
# This Makefile should only be run on the NIS master server of a domain.
# All updated maps will be pushed to all NIS slave servers listed in the
# /var/yp/ypservers file. Please make sure that the hostnames of all
# NIS servers in your domain are listed in /var/yp/ypservers.
#
# This Makefile can be modified to support more NIS maps if desired.
#

# Set the following variable to "-b" to have NIS servers use the domain 
# name resolver for hosts not in the current domain. This is only needed,
# if you have SunOS slave YP server, which gets here maps from this
# server. The NYS YP server will ignore the YP_INTERDOMAIN key.
#B=-b
B=

# If this machine is an NIS master, comment out this next line so
# that changes to the NIS maps can be propagated to the slave servers.
# (By default we assume that we are only serving a small domain with
# only one server.)
#
NOPUSH = "True"

# We do not put password entries with lower UIDs (the root and system
# entries) in the NIS password database, for security. MINUID is the
# lowest uid that will be included in the password maps.
#
MINUID=20
 
# These are commands which this Makefile needs to properly rebuild the
# NIS databases. Don't change these unless you have a good reason. Also
# be sure not to place an @ in front of /usr/bin/awk: it isn't necessary
# and it'll break everything in sight.
#
AWK = /usr/bin/gawk
RM  = @/bin/rm -f
RCAT = /bin/cat
CAT = @$(RCAT)

#
# These are the source directories for the NIS files; normally
# that is /etc but you may want to move the source for the password
# and group files to (for example) /var/yp/ypfiles. The directory
# for passwd and group is defined by YPPWDDIR, the rest is taken
# from YPSRCDIR.
#
YPSRCDIR = /etc
YPPWDDIR = /etc
YPBINDIR = /usr/libexec/yp
YPSBINDIR = /usr/sbin
YPDIR = /var/yp
YPMAPDIR = $(YPDIR)/$(DOMAIN)

DBLOAD = $(YPBINDIR)/makedbm -m `hostname`
MKNETID = $(YPBINDIR)/mknetid
YPPUSH = $(YPSBINDIR)/yppush
DOMAIN = `basename \`pwd\``
LOCALDOMAIN = `/bin/domainname`
REVNETGROUP = $(YPBINDIR)/revnetgroup

# These are the files from which the NIS databases are built. You may edit
# these to taste in the event that you wish to keep your NIS source files
# seperate from your NIS server's actual configuration files. 
#
GROUP     = $(YPPWDDIR)/group
PASSWD    = $(YPPWDDIR)/passwd
SHADOW	  = $(YPPWDDIR)/shadow
ALIASES	  = $(YPSRCDIR)/aliases
ETHERS    = $(YPSRCDIR)/ethers	   # ethernet addresses (for rarpd)
BOOTPARAMS= $(YPSRCDIR)/bootparams # for booting Sun boxes (bootparamd)
HOSTS     = $(YPSRCDIR)/hosts
NETWORKS  = $(YPSRCDIR)/networks
PROTOCOLS = $(YPSRCDIR)/protocols
PUBLICKEYS= $(YPSRCDIR)/publickey
RPC 	  = $(YPSRCDIR)/rpc
SERVICES  = $(YPSRCDIR)/services
NETGROUP  = $(YPSRCDIR)/netgroup
NETID	  = $(YPSRCDIR)/netid
AMD_HOME  = $(YPSRCDIR)/amd.home
YPSERVERS = $(YPDIR)/ypservers	# List of all NIS servers for a domain

target: 
	@if [ ! -d $(LOCALDOMAIN) ]; then mkdir $(LOCALDOMAIN); fi; \
	cd $(LOCALDOMAIN) ; echo "NIS Map update started on `date`" ; \
	if [ ! $(NOPUSH) ]; then make -f ../Makefile ypservers; fi; \
	make -f ../Makefile all; echo "NIS Map update completed."

# If you don't want some of these maps built, feel free to comment
# them out from this list.
# Note that we don't build the ethers or bootparams maps by default
# since /etc/ethers and /etc/bootparams are not likely to be present
# on all systems.
#

all:  passwd hosts group netid networks protocols rpc services netgrp \
	 mail shadow ypservers publickey ethers # amd.home bootparams

ethers:	   ethers.byname ethers.byaddr
hosts:	   hosts.byname hosts.byaddr
networks:  networks.byaddr networks.byname
protocols: protocols.bynumber protocols.byname
rpc:	   rpc.byname rpc.bynumber
services:  services.byname
passwd:    passwd.byname passwd.byuid
group:     group.byname group.bygid
shadow:	   shadow.byname
netid:	   netid.byname
netgrp:	   netgroup netgroup.byhost netgroup.byuser
publickey: publickey.byname
mail:	   mail.aliases

ypservers: $(YPSERVERS)
	@echo "Updating $@..."
	$(CAT) $(YPSERVERS) | \
	$(AWK) '{ if ($$1 != "" && $$1 !~ "#") print $$0"\t"$$0 }' $^ \
		| $(DBLOAD) -i $(YPSERVERS) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


ethers.byname: $(ETHERS)
	@echo "Updating $@..."
	$(CAT) $(ETHERS) | \
	$(AWK) '{ if ($$1 != "" && $$1 !~ "#" && $$1 != "+") \
		print $$2"\t"$$0 }' $^ | $(DBLOAD) -i $(ETHERS) \
						-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


ethers.byaddr: $(ETHERS)
	@echo "Updating $@..."
	$(CAT) $(ETHERS) | \
	$(AWK) '{ if ($$1 != "" && $$1 !~ "#" && $$1 != "+") \
		print $$1"\t"$$0 }' $^ | $(DBLOAD) -i $(ETHERS) \
						-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


bootparams: $(BOOTPARAMS)
	@echo "Updating $@..."
	$(CAT) $(BOOTPARAMS) | \
	$(AWK) '{ if ($$1 != "" && $$1 !~ "#" && $$1 != "+") \
		print $$0 }' $^ | $(DBLOAD) -i $(BOOTPARAMS) \
						-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


netgroup: $(NETGROUP)
	@echo "Updating $@..."
	$(CAT) $(NETGROUP) | \
	$(AWK) '{ if ($$1 != "" && $$1 !~ "#" && $$1 != "+") \
		print $$0 }' $^ | $(DBLOAD) -i $(NETGROUP) \
						-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


netgroup.byhost: $(NETGROUP)
	@echo "Updating $@..."
	$(CAT) $(NETGROUP) | $(REVNETGROUP) -h | \
		$(DBLOAD) -i $(NETGROUP) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi

netgroup.byuser: $(NETGROUP)
	@echo "Updating $@..."
	$(CAT) $(NETGROUP) | $(REVNETGROUP) -u | \
		$(DBLOAD) -i $(NETGROUP) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


hosts.byname: $(HOSTS)
	@echo "Updating $@..."
	$(CAT) $(HOSTS) | \
	$(AWK) '/^[0-9]/ { for (n=2; n<=NF && $$n !~ "#"; n++) \
		print $$n"\t"$$0 }' $^ | $(DBLOAD) $(B) -l -i $(HOSTS)  \
				-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi

hosts.byaddr: $(HOSTS)
	@echo "Updating $@..."
	$(CAT) $(HOSTS) | \
	$(AWK) '{ if ($$1 !~ "#" && $$1 != "") print $$1"\t"$$0 }' $^ \
		| $(DBLOAD) $(B) -i $(HOSTS) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


networks.byname: $(NETWORKS)
	@echo "Updating $@..."
	$(CAT) $(NETWORKS) | \
	$(AWK) \
	   '{ if($$1 !~ "#" && $$1 != "") { print $$1"\t"$$0; \
			  for (n=3; n<=NF && $$n !~ "#"; n++) \
			      print $$n"\t"$$0 \
			}}' $^ | $(DBLOAD) -i $(NETWORKS) \
			-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


networks.byaddr: $(NETWORKS)
	@echo "Updating $@..."
	$(CAT) $(NETWORKS) | \
	$(AWK) '{ if ($$1 !~ "#" && $$1 != "") print $$2"\t"$$0 }' $^ \
		| $(DBLOAD) -i $(NETWORKS) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


protocols.byname: $(PROTOCOLS)
	@echo "Updating $@..."
	$(CAT) $(PROTOCOLS) | \
	$(AWK) \
	   '{ if ($$1 !~ "#" && $$1 != "") { print $$1"\t"$$0; \
			  for (n=3; n<=NF && $$n !~ "#"; n++) \
			      print $$n"\t"$$0 \
			}}' $^ | $(DBLOAD) -i $(PROTOCOLS) \
						-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


protocols.bynumber: $(PROTOCOLS)
	@echo "Updating $@..."
	$(CAT) $(PROTOCOLS) | \
	$(AWK) '{ if ($$1 !~ "#" && $$1 != "") print $$2"\t"$$0 }' $^ \
		| $(DBLOAD) -i $(PROTOCOLS) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


rpc.byname: $(RPC)
	@echo "Updating $@..."
	$(CAT) $(RPC) | \
	$(AWK) \
	   '{ if ($$1 !~ "#"  && $$1 != "") { print $$1"\t"$$0; \
			  for (n=3; n<=NF && $$n !~ "#"; n++) \
			      print $$n"\t"$$0 \
			}}' $^ | $(DBLOAD) -i $(RPC) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


rpc.bynumber: $(RPC)
	@echo "Updating $@..."
	$(CAT) $(RPC) | \
	$(AWK) '{ if ($$1 !~ "#" && $$1 != "") print $$2"\t"$$0 }' $^ \
		| $(DBLOAD)  -i $(RPC) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


services.byname: $(SERVICES)
	@echo "Updating $@..."
	$(CAT) $(SERVICES) | \
	$(AWK) \
	   '{ if ($$1 !~ "#" && $$1 != "") { print $$2"\t"$$0; \
			  for (n=3; n<=NF && $$n !~ "#"; n++) \
			      print $$n"\t"$$0 \
			}}' $^ | $(DBLOAD) -i $(SERVICES) \
			-o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


passwd.byname: $(PASSWD)
	@echo "Updating $@..."
	$(CAT) $(PASSWD) | \
	$(AWK) -F: '!/^[-+]/ { if ($$1 != "" && $$3 >= $(MINUID) ) \
					print $$1"\t"$$0 }' $^ \
		| $(DBLOAD) -i $(PASSWD) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


passwd.byuid: $(PASSWD)
	@echo "Updating $@..."
	$(CAT) $(PASSWD) | \
	$(AWK) -F: '!/^[-+]/ { if ($$1 != "" && $$3 >= $(MINUID) ) \
					print $$3"\t"$$0 }' $^ \
		| $(DBLOAD) -i $(PASSWD) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


shadow.byname: $(SHADOW)
	@echo "Updating $@..."
	$(CAT) $(SHADOW) | \
	$(AWK) -F: '!/^[-+]/ { if ($$1 != "" ) \
					print $$1"\t"$$0 }' $^ \
		| $(DBLOAD) -s -i $(SHADOW) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


group.byname: $(GROUP)
	@echo "Updating $@..."
	$(CAT) $(GROUP) | \
	$(AWK) -F: '!/^[-+]/ { if ($$1 != "") print $$1"\t"$$0 }' $^ \
		| $(DBLOAD) -i $(GROUP) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


group.bygid: $(GROUP)
	@echo "Updating $@..."
	$(CAT) $(GROUP) | \
	$(AWK) -F: '!/^[-+]/ { if ($$1 != "") print $$3"\t"$$0 }' $^ \
		| $(DBLOAD) -i $(GROUP) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi

$(NETID):
netid.byname: $(GROUP) $(PASSWD) $(HOSTS) $(NETID)
	@echo "Updating $@..."
	@$(MKNETID) -q -p $(PASSWD) -g $(GROUP) -h $(HOSTS) -d $(DOMAIN) \
		-n $(NETID) | $(DBLOAD) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi

mail.aliases: $(ALIASES)
	@echo "Updating $@..."
	$(CAT) $(ALIASES) | \
	$(AWK) '{ if ($$1 != "" && $$1 !~ "#" && $$1 != "+") \
		print $$0 }' $^ | $(DBLOAD) --aliases \
			-i $(ALIASES) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi

publickey.byname: $(PUBLICKEYS)
	@echo "Updating $@..."
	$(CAT) $(PUBLICKEYS) | \
	$(AWK) '{ if($$1 !~ "#" && $$1 != "") \
		{ print $$1"\t"$$2 }}' $^ | $(DBLOAD) -i $(PUBLICKEYS) \
			 -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi


amd.home: $(AMD_HOME)
	@echo "Updating $@..."
	-@sed -e "s/#.*$$//" -e "/^$$/d" $(AMD_HOME) | \
	$(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);\
		}' | \
	$(DBLOAD) -o $(YPMAPDIR)/$@ - $@
	@if [ ! $(NOPUSH) ]; then $(YPPUSH) -d $(DOMAIN) $@; fi
	@if [ ! $(NOPUSH) ]; then echo "Pushed $@ map." ; fi
