#!/bin/ksh -
#
# @(#)mk-afs-syncCSDB	1.5 (hursley) 11/1/96
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-syncCSDB/SCCS/s.mk-afs-syncCSDB
#
# NAME		mk-afs-syncCSDB
# AUTHOR	Paul Blackburn <mpb@acm.org>
# WRITTEN	July 1995
# PURPOSE	Update local CellservDB file and update AFS kernel sitelist
# CREDITS	Anton Knaus <awk@ece.cmu.edu> for
#               	/afs/ece.cmu.edu/usr/awk/Public/newCellServDB
#
# USAGE		Normally invoked via daily cron job:
#
#			0 3 * * * /usr/local/sbin/mk-afs-syncCSDB
#
#		NB You may want to change the "src=" line (below) to identify
#		your own master copy of CellServDB

tstamp() {
	echo "`date '+%H''%M'':%S'` ${CMD}: ${1}"
}

CMD=$(basename ${0})

# Switch stdout and stderr to a log file (for the post mortem!)
LOG=/tmp/${CMD}
exec 4>&2
exec 3>&1
exec 1>${LOG}
exec 2>&1
tstamp "commenced on $(date '+%a %d %h %y')"

# "@cell" is a symbolic link to /afs/$this_cell_name

src=/afs/@cell/service/etc/CellServDB
dst=/usr/vice/etc/CellServDB

if [ ! -s ${src} ]; then
        echo "zero length file: ${src}" >&2
	exit 1
fi

# Check if master is newer than local copy 

if [ ${src} -nt ${dst} ]; then
	cp ${dst} ${dst}- && cp ${src} ${dst}
else
	echo "master copy no newer: no processing to be done"
	tstamp "completed"
	exit
fi

if [ $? != 0 ]; then
	echo "failed to copy new CellservDB" >&2
	tstamp "completed"
	exit 1
fi

# What follows is mostly <awk@ece.cmu.edu> Anton Knaus's newCellServDB
# /afs/ece.cmu.edu/usr/awk/Public/newCellServDB
#
#   Script for updating the list of AFS cells in  
#   the kernel. (Too bad fs can't delete old cells.)
#
#   Print out the fs commands as they execute.
#
#   HISTORY
#
#   12-15-95: George Cebulka, george@ece.cmu.edu
#	added test to see if this is an OS conversion.
#
#   22 October 1993 Anton Knaus (awk@ece.cmu.edu)
#   Created.
#
PATH=/bin:/usr/afsws/bin:/usr/ucb
export PATH

CELLSERVDB=${1-/usr/vice/etc/CellServDB}

case `uname` in
OSF|AIX)
	GQ=-q
	;;
ULTRIX)
	GQ=-s
	;;
esac

#
#   Don't execute if AFS is not mounted.
#
df | grep ${GQ} "AFS"
if [ $? = 1 ]; then
	echo "** Error: AFS is not mounted." >&2
	exit 1;
fi

#
#  Build fs commands and pipe through shell
#  for execution.		
#
sed  -e '
	s/>\([^ 	 ]*\).*/fs newcell -name \1 -servers/
:again
	N;
	/>/{ 
		P; 
		D;
	}
	s/\n\([^ 	 ]*\).*/ \1/;
	s/\n/ /;
	${ 
		P; 
		D;
	}
	b again
' ${CELLSERVDB} | /bin/sh -x 

tstamp "completed"
