#!/bin/sh -
#
# @(#)mk-afs-fcells	1.11 (hursley) 10/31/96
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-fcells/SCCS/s.mk-afs-fcells
#
# NAME		mk-afs-fcells
# AUTHOR	Paul Blackburn <mpb@acm.org>
# PURPOSE	Compute a script to enable access to all AFS cells
#		listed in CellServDB (except ${afscellname})
# USAGE		See usage() below.

X=${afs_install_base}/usr/local/sbin/mk-afs-cf

if [ ! -f ${X} ]; then
	echo "fatal error: ${X} not found" >&2
	exit 1
fi

# source global config for afs installation

. ${X}

usage() {
	echo "Usage: ${CMD} [CellServDB_file] [-short]"
	echo "Mount foreign AFS cells based on /usr/vice/etc/CellServDB"
	echo "Optional command line arguments:"
	echo "CellServDB_file  - alternative CellServDB file"
	echo "-short           - create a symbolic link to abbreviated cellname"
	echo "                   (eg: aixssc.uk.ibm.com <- aixssc)"
}

# main

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

tstamp "version 1.11 commenced on `date '+%a %d %h %y'`"

if [ ! -d /afs ]; then
	fatal "/afs not found"
fi

X=/usr/afsws/bin/fs
if [ ! -x ${X} ]; then
	fatal "unable to execute ${X}"
fi

if [ -z "${afscellname}" ]; then
	CELLNAME=`${X} wscell | cut -d\' -f2`
else
	CELLNAME=${afscellname}
fi

# do we have an alternative CellServDB filename on command line?

if [ ! -z "${1}" ]; then
	while [ ! -z "$1" ]; do
		case ${1} in
			-short )	shortname="true";;
			* )		CELLSERVDB=${1};;
		esac
	done
fi

tstamp "Generate a list of foreign AFS cells from CellServDB"

if [ ! -f ${CELLSERVDB} ]; then
	fatal "CellServDB file missing: ${CELLSERVDB}"
	if [ ! -s ${CELLSERVDB} ]; then
		fatal "CellServDB file empty: ${CELLSERVDB}"
	fi
fi

< ${CELLSERVDB} awk '{
	char1=substr($1,1,1)
	if ( char1 == ">" ) {
		if ( servs != "" ) {
			printf("%s%s\n",cellname,servs)
			servs=""
		}
		cellname=substr($1,2,length($1))
		x=index($0,"#")+1
		cellcomment= substr($0,x,length($0)-x)
	}
	else {
		x=index($0,"#")+1
		servs=sprintf("%s %s",servs,substr($0,x,length($0)-x+1))
	}
}
END {
	printf("%s%s\n",cellname,servs)
	}'| sort -u >${TF}

doit "mv ${TF} ${TF}-"
doit "grep -v ${afscellname} <${TF}- >${TF}"
doit "rm ${TF}-"

tstamp "AFS cells (and their servers) to be added:"
cat ${TF}

tstamp "AFS Installation Guide page 2-68"

echo "CMD=${CMD}; export CMD" >${SCRIPT}
echo "CELLNAME=${CELLNAME}; export CELLNAME" >>${SCRIPT}
echo >>${SCRIPT}

<${TF} awk '{
	printf("#---------------------------------------------------------\n")
	foreigncell=$1
	printf("FOREIGNCELL=%s; export FOREIGNCELL\n",foreigncell)
	printf("SHORTFORM=\`echo \$FOREIGNCELL | cut -d. -f1\`\n")
	printf("export SHORTFORM\n")

	printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: ")
	printf("# Enabling access to \${FOREIGNCELL}\n")

	printf("X=\"cd /afs/.%s && fs mkmount temp root.afs\"\n",cellname)
	printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${X}\n")
	printf("eval \${X}\n")
	printf("RET=\$?\n")
	printf("if [ \${RET} != 0 ]; then\n")
	printf("\tM=\"error: return code=\${RET}\"\n")
	printf("\techo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${M}\n")
	printf("fi\n\n")

#	printf("X=\"cd temp && fs mkmount %s root.cell -c %s -root\"\n",
	printf("X=\"cd temp && fs mkmount %s root.cell -c %s\"\n",
		foreigncell,foreigncell)
	printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${X}\n")
	printf("eval \${X}\n")
	printf("RET=\$?\n")
	printf("if [ \${RET} != 0 ]; then\n")
	printf("\tM=\"error: return code=\${RET}\"\n")
	printf("\techo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${M}\n")
	printf("fi\n\n")

	if ( shortname == "true" ) {
		printf("X=\"ln -s \$FOREIGNCELL \$SHORTFORM\"\n")
		printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${X}\n")
		printf("eval \${X}\n")
		printf("RET=\$?\n")
		printf("if [ \${RET} != 0 ]; then\n")
		printf("\tM=\"error: return code=\${RET}\"\n")
		printf("\techo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${M}\n")
		printf("fi\n\n")
	}

	printf("X=\"cd /afs/.%s && fs rmmount temp\"\n",cellname)
	printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${X}\n")
	printf("eval \${X}\n")
	printf("RET=\$?\n")
	printf("if [ \${RET} != 0 ]; then\n")
	printf("\tM=\"error: return code=\${RET}\"\n")
	printf("\techo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${M}\n")
	printf("fi\n\n")

	printf("X=\"vos release root.afs\"\n")
	printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${X}\n")
	printf("eval \${X}\n")
	printf("RET=\$?\n")
	printf("if [ \${RET} != 0 ]; then\n")
	printf("\tM=\"error: return code=\${RET}\"\n")
	printf("\techo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${M}\n")
	printf("fi\n\n")

	printf("X=\"fs checkvolumes\"\n")
	printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${X}\n")
	printf("eval \${X}\n")
	printf("RET=\$?\n")
	printf("if [ \${RET} != 0 ]; then\n")
	printf("\tM=\"error: return code=\${RET}\"\n")
	printf("\techo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${M}\n")
	printf("fi\n\n")

	printf("X=\"fs newcell %s\"\n",$0)
	printf("echo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${X}\n")
	printf("eval \${X}\n")
	printf("RET=\$?\n")
	printf("if [ \${RET} != 0 ]; then\n")
	printf("\tM=\"error: return code=\${RET}\"\n")
	printf("\techo \`date '+''%%H''%%M'':%%S'\` \${CMD}: \${M}\n")
	printf("fi\n\n")

}' cellname=${CELLNAME} cmd=${CMD} shortname=${shortname} >> ${SCRIPT} 

if [ -z "${dryrun}" ]; then
	doit "/bin/sh ${SCRIPT}"
else
	echo "dry run only, no action taken"
fi

rm ${TF}
tstamp "completed"
#last echo throws a newline in the log
echo
