#!/bin/sh
#
# This script updates the passwd.* maps for all domains served
# by this host. We make a feeble attempt at locking, so as to
# avoid concurrent builds on the databases. However, this locking
# does _not_ guard us against the NIS maintainer running make while
# we build our maps. Maybe it's time for a ypmake tool...

tmp=/tmp/ypwd.upd.$$
lock=/tmp/yppasswd.lock

echo $$ > $tmp
i=0;
while ! ln $tmp $lock; do
    sleep 10;
    i=`expr $i + 1`;
    if [ $i -gt 60 ]; then
        echo "NIS passwd maps seem permanently locked" ||
            mailx -s "Could not remake NIS passwd.* maps" root
        exit 2
    fi
done

cd /var/yp
mtemp=/tmp/ypw.tmp.$$
merr=/tmp/ypw.err.$$

for dir in *; do
    if [ -d $dir -a "$dir" != "binding" ]; then
	(
	    cd $dir && 
	    if ! make -sk passwd > $mtemp 2>&1; then
		echo "Errors in $PWD:"
		cat $mtemp
		echo
	    fi >> $merr
	)
    fi
done

if [ -s $merr ]; then
    (
    echo "The following errors occurred while remaking the NIS passwd maps"
    echo
    cat $merr
    ) | mailx -s "Errors while remaking NIS passwd.* maps" root
fi
rm -f $mtemp $merr

rm -f $tmp $lock
