#!/bin/sh -
#
# @(#)mk-afs-cells	1.2 (hursley) 10/31/96
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-cells/SCCS/s.mk-afs-cells
#
# NAME		mk-afs-cells
# AUTHOR	Paul Blackburn <mpb@acm.org>
# PURPOSE	Display list of AFS cells from CellServDB
# USAGE		mk-afs-cells [-new] 
#		option "-new" lists only cells that don't have dirs in /afs

CMD=`basename ${0}`
CELLSERVDB=/usr/vice/etc/CellServDB
TF=/tmp/${CMD}.$$

if [ -z "${HOST}" ]; then
	HOST=`localhost 2>/dev/null || (hostname | awk -F. '{print $1}')`
fi

# functions

error() {
        echo "${CMD} error: ${1}" >&2
}

warning() {
        echo "${CMD} warning: ${1}" >&2
}

# main

trap "echo interrupted; rm -f ${TF}; exit 1" 1 2 3 15

if [ ! -d /afs ]; then
	error "/afs not found. AFS not running on ${HOST}"
	exit 1
fi

if [ ! -f ${CELLSERVDB} ]; then
	error "${CELLSERVDB} not found"
	exit 1
fi

< ${CELLSERVDB} awk '{
	char1=substr($1,1,1)
	if ( char1 == ">" )
	{
		cellname=substr($1,2,length($1))
		cellcomment=substr($2,2,length($2))
		for (z=3; z<(NF+1); z++)
		{
			cellcomment=sprintf("%s %s",sitecomment,$(z))
		}
		printf("%s\n",cellname)
	}
}' | sort -u >${TF}

if [ "${1}" = "-new" ]; then
	<${TF} awk '{
		foreigncell=$1
		printf("if [ ! -d /afs/%s ]\n",foreigncell)
		printf("then\n")
		printf("echo %s\n",foreigncell)
		printf("fi\n")
	}' | /bin/sh
else
	cat ${TF}
fi

rm ${TF} 2>/dev/null
