:
#      @(#)ypmake.sh	4.4.1.2 System V NFS  source
#      SCCS IDENTIFICATION
#	System V NFS - Release 3.2.4
#
#	Copyright 1986, 1987, 1988, 1989 Lachman Associates, Incorporated (LAI)
#
#	All Rights Reserved.
#
#	The copyright above and this notice must be preserved in all
#	copies of this source code.  The copyright above does not
#	evidence any actual or intended publication of this source
#	code.
#
#	This is unpublished proprietary trade secret source code of
#	Lachman Associates.  This source code may not be copied,
#	disclosed, distributed, demonstrated or licensed except as
#	expressly authorized by Lachman Associates.
#

#      @(#)ypmake	4.8 Lachman System V NFS  source

#
#	@(#)ypmake 1.1 86/02/05 SMI
#


#
#
#
# Usuage: ypmake [NOPUSH=1] [SILENT=1] "all" | {map1 map2 ...}
#		NOPUSH=1 do not push the maps
#		SILENT=1 do the work silently
#		"all" make all maps OR
#		{map1 map2 ...} a list of maps to make
#
# ypmake emulates make as much as possible for those systems that do
# have the make program.
#
# It does NOT check modification times as make does-- it is not that
# smart. It also does not default to all-- all must be explicitly 
# stated on the command line.
#

log(){
	echo "$*" 
	echo "ypmake: $*" >> /usr/adm/nislog
}

DIR=/etc
DOM=`/usr/bin/domainname`
NOPUSH=""
SILENT=""
PASSWD=$DIR/passwd
YPPASSWD=$DIR/passwd.yp
GROUP=$DIR/group.yp
ALIASES=/usr/lib/mail/aliases
AWK=oawk
ARGLIST=all

if [ -x /bin/make ] ; then
	/bin/make $*
	exit $?
fi

while test $# -gt 0 
do
	case $1 in

	NOPUSH=1) 
	    NOPUSH="1"
	    shift
	    ;;

	SILENT=1) 
	    SILENT="1"
	    shift
	    ;;

	 *) 
            ARGLIST=$ARGLIST" "$1
	    shift
	    ;;

	esac
done

set $ARGLIST

echo $ARGLIST > /tmp/yp$$

if [ `grep -c "all" /tmp/yp$$` != "0" -a $# -eq 1 ]; then 
	ARGLIST="passwd group hosts ethers networks rpc services protocols netgroup aliases" 
fi
rm /tmp/yp$$
	
for i in $ARGLIST ; do
	case $i in

	passwd)
		if [ `grep -c "passwd.nam" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then \
			/etc/yp/ypsecure ; \
			if [ $? -eq 1 ] ; then \
				echo "couldn't propogate map due to security level" ; \
				break ; \
			fi ; \
			`/etc/yp/ypshad2pwd /etc/passwd /etc/shadow /etc/passwd.yp` ;\
			if [ -f $YPPASSWD ]; then \
				$AWK 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ { print $1, $0 }' \
				  $YPPASSWD  | /etc/yp/makedbm - /etc/yp/$DOM/passwd.nam; \
				$AWK 'BEGIN { FS=":"; OFS="\t"; } /^[a-zA-Z0-9_]/ { print $3, $0 }' \
				  $YPPASSWD | /etc/yp/makedbm - /etc/yp/$DOM/passwd.uid; \
				touch passwd.time; \
				sync ; \
				sync ; \
				if [  $SILENT"X" != "1X" ]; then \
					echo "updated passwd"; \
				fi;\
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush passwd.uid; \
					/etc/yp/yppush passwd.nam; \
					if [ $SILENT"X" = "X" ]; then \
						echo "pushed passwd"; \
					fi;\
				else \
					: ; \
				fi; \
				if [ -f /etc/yp/map2ascii/passwd.nam ]; then \
					/etc/yp/map2ascii/passwd.nam; \
				fi \
			else \
				log "couldn't find $YPPASSWD"; \
			fi \
		else \
			:;
		fi

		;;

	group)
		if [ `grep -c "group.nam" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			/etc/yp/ypsecure ; \
			if [ $? -eq 1 ] ; then \
				echo "couldn't propogate map due to security level" ; \
				break ; \
			fi ; \
			if [ -f $GROUP ]; then \
				$AWK 'BEGIN { FS=":"; OFS="\t"; } { print $1, $0 }' \
				    $GROUP | /etc/yp/makedbm - /etc/yp/$DOM/group.nam; \
				$AWK 'BEGIN { FS=":"; OFS="\t"; } { print $3, $0 }' \
				    $GROUP | /etc/yp/makedbm - /etc/yp/$DOM/group.gid; \
				touch group.time; \
				echo "updated group"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush group.nam; \
					/etc/yp/yppush group.gid; \
					echo "pushed group"; \
				else \
					: ; \
				fi; \
				if [ -f /etc/yp/map2ascii/group.nam ]; then \
					/etc/yp/map2ascii/group.nam; \
				fi \
			else \
				log "couldn't find $GROUP"; \
			fi \
		else \
			: ; \
		fi
		;;

	hosts)
		if [ `grep -c "hosts.nam" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/hosts ]; then \
				sed -e "/^#/d" -e s/#.*$// $DIR/hosts | /etc/yp/stdhosts \
				    | $AWK '{for (i = 2; i <= NF; i++) print $i, $0}' \
				    | /etc/yp/makedbm - /etc/yp/$DOM/hosts.nam; \
				/etc/yp/stdhosts $DIR/hosts | \
				    $AWK 'BEGIN { OFS="\t"; } $1 !~ /^#/ { print $1, $0 }' \
				    | /etc/yp/makedbm - /etc/yp/$DOM/hosts.adr; \
				touch hosts.time; \
				echo "updated hosts"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush hosts.nam; \
					/etc/yp/yppush hosts.adr; \
					echo "pushed hosts"; \
				else \
					: ; \
				fi \
			else \
				log "couldn't find $DIR/hosts"; \
			fi \
		else \
			: ; \
		fi
		;;

	ethers)
		if [ `grep -c "ethers.adr" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/ethers ]; then \
				sed -e "/^#/d" -e s/#.*$// $DIR/ethers \
				| $AWK 'BEGIN { X["A"]="a"; X["B"]="b"; X["C"]="c"; X["D"]="d";X["E"]="e"; X["F"]="f"; } \
				    { for (i = 1; i <= length($1); i++) \
				      if (substr($1, i, 1) >= "A" && substr($1, i, 1) <= "Z") \
					printf("%s", X[substr($1, i, 1)]) ; \
				      else \
					printf("%s", substr($1, i, 1)); printf(" "); \
				    print $2}' \
				|$AWK '{print $1, $0; for (i = 3;i <= NF;i++) print $i,$0}' \
				| /etc/yp/makedbm - /etc/yp/$DOM/ethers.adr; \
				$AWK 'BEGIN { OFS="\t"; } $1 !~ /^#/ { print $2, $0 }' \
				   $DIR/ethers | \
				/etc/yp/makedbm - /etc/yp/$DOM/ethers.nam;\
				touch ethers.time; \
				echo "updated ethers"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush ethers.nam; \
					/etc/yp/yppush ethers.adr; \
					echo "pushed ethers"; \
				else \
					: ; \
				fi \
			else \
				log "couldn't find $DIR/ethers"; \
			fi \
		else \
			: ; \
		fi
		;;

	networks)
		if [ `grep -c "netwks.adr" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/networks ]; then \
				sed -e "/^#/d" -e s/#.*$// $DIR/networks | $AWK \
				    '{print $1, $0; for (i = 3;i <= NF;i++) print $i,$0}' \
				    | /etc/yp/makedbm - /etc/yp/$DOM/netwks.nam; \
				$AWK 'BEGIN { OFS="\t"; } $1 !~ /^#/ { print $2, $0 }' \
				   $DIR/networks | /etc/yp/makedbm - /etc/yp/$DOM/netwks.adr;\
				touch networks.time; \
				echo "updated networks"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush netwks.nam; \
					/etc/yp/yppush netwks.adr; \
					echo "pushed networks"; \
				else \
					: ; \
				fi \
			else \
				log "couldn't find $DIR/networks"; \
			fi \
		else \
			: ;
		fi
		;;

	services)
		if [ `grep -c "srvcs.nam" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/services ]; then \
				$AWK 'BEGIN { OFS="\t"; } $1 !~ /^#/ { print $2, $0 }' \
				    $DIR/services| /etc/yp/makedbm - /etc/yp/$DOM/srvcs.nam;\
				touch services.time; \
				echo "updated services"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush srvcs.nam; \
					echo "pushed services"; \
				else \
					: ; \
				fi \
			else \
				log "couldn't find $DIR/services"; \
			fi \
		else \
			: ; \
		fi
		;;

	rpc)
		if [ `grep -c "rpc.no" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/rpc ]; then \
				$AWK 'BEGIN { OFS="\t"; } $1 !~ /^#/ { print $2, $0 }' \
				    $DIR/rpc| /etc/yp/makedbm - /etc/yp/$DOM/rpc.no;\
				touch rpc.time; \
				echo "updated rpc"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush rpc.no; \
					echo "pushed rpc"; \
				else \
					: ; \
				fi \
			else \
				log "couldn't find $DIR/rpc"; \
			fi \
		else \
			: ; \
		fi
		;;

	protocols)
		if [ `grep -c "proto.no" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/protocols ]; then \
				$AWK 'BEGIN { OFS="\t"; } $1 !~ /^#/ { print $2, $0 }' \
				    $DIR/protocols | /etc/yp/makedbm - \
				    /etc/yp/$DOM/proto.no; \
				sed -e "/^#/d" -e s/#.*$// $DIR/protocols | $AWK \
				    '{print $1,$0; for (i = 3;i <= NF;i++) print $i, $0}' \
				    | /etc/yp/makedbm - /etc/yp/$DOM/proto.nam; \
				touch protocols.time; \
				echo "updated protocols"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush proto.nam; \
					/etc/yp/yppush proto.no; \
					echo "pushed protocols"; \
					else \
					: ; \
				fi \
			else \
				log "couldn't find $DIR/protocols"; \
			fi \
		else \
			: ; \
		fi
		;;

	netgroup)
		if [ `grep -c "netgrp.usr" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/netgroup ]; then \
				/etc/yp/makedbm $DIR/netgroup /etc/yp/$DOM/netgroup; \
				/etc/yp/revnetgroup -u | /etc/yp/makedbm - /etc/yp/$DOM/netgrp.usr; \
				/etc/yp/revnetgroup -h | /etc/yp/makedbm - /etc/yp/$DOM/netgrp.hst; \
				touch netgroup.time; \
				echo "updated netgroup"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush netgroup; \
					/etc/yp/yppush netgrp.usr; \
					/etc/yp/yppush netgrp.hst; \
					echo "pushed netgroup"; \
				else \
					: ; \
				fi \
			else \
				log "couldn't find $DIR/netgroup"; \
			fi \
		else
			: ; \
		fi
		;;

	aliases)
		if [ `grep -c "mail.alias" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $ALIASES ]; then \
				cp $ALIASES /etc/yp/$DOM/mail.alias; \
				/usr/lib/sendmail -bi -oA/etc/yp/$DOM/mail.alias; \
				rm /etc/yp/$DOM/mail.alias; \
				touch aliases.time; \
				echo "updated aliases"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush mail.alias; \
					echo "pushed aliases"; \
				else \
					: ; \
				fi \
			else \
				log "couldn't find $ALIASES"; \
			fi \
		else \
			: ; \
		fi 
		;;

	auto.master)
		if [ `grep -c "auto.master" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/auto.master ]; then \
				sed -e "/^#/d" -e s/#.*$$// $DIR/auto.master \
				| /etc/yp/makedbm - /etc/yp/$DOM/auto.mstr; \
				touch auto.mstr.time; \
				echo "updated auto.master"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush auto.mstr; \
					echo "pushed auto.master"; \
				else \
					: ; \
				fi \
			else \
				echo "couldn't find $DIR/auto.master"; \
			fi \
		else \
			: ; \
		fi
		;;

	auto.home)
		if [ `grep -c "auto.home" /etc/yp/YP_MAP_X_LATE ` != "0" ]; then\
			if [ -f $DIR/auto.home ]; then \
				sed -e "/^#/d" -e s/#.*$$// $DIR/auto.home \
				| /etc/yp/makedbm - /etc/yp/$DOM/auto.home; \
				touch auto.home.time; \
				echo "updated auto.home"; \
				if [ $NOPUSH"X" != "1X" ]; then \
					/etc/yp/yppush auto.home; \
					echo "pushed auto.home"; \
				else \
					: ; \
				fi \
			else \
				echo "couldn't find $DIR/auto.home"; \
			fi \
		else \
			: ; \
		fi
		;;

	all)
		: ;;
	*)
	   	echo "ypmake: unknown argugment "$i
	esac
done

exit 0
