#!/bin/ksh -
#
# @(#)mk-afs-sync-mounts	1.5 (hursley) 11/1/96
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-sync-mounts/SCCS/s.mk-afs-sync-mounts
#
# NAME		mk-afs-sync-mounts
# AUTHOR	Paul Blackburn <mpb@acm.org>
# WRITTEN	July 1995
# PURPOSE	Ensure all cells defined in /usr/vice/etc/CellServDB
#		are mounted (and old cells unmounted).
# HISTORY	Based on Anton Knaus <awk@ece.cmu.edu>'s update_cells script.
# USAGE		This script should be run by the AFS administrator in your
#		cell when the CellServDB has had new cells added (or old cells
#		removed).
#		Optionally, define shell variable "mk_afs_base" as the
#		directory where your mk-afs-* scripts reside (this is
#		normally: /usr/local/sbin)

nocfcheck=true	; export nocfcheck	# we don't need the afs_install cf file

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

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

# source global config for AFS installation

. ${X}

# main

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

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

CSDB_list=/tmp/masm_csdb_list.$$
AFS_list=/tmp/masm_afs_list.$$
new_cells=/tmp/masm_new_cells.$$
deleted_cells=/tmp/masm_deleted_cells.$$
deleted_links=/tmp/masm_deleted_links.$$
failed_mounts=/tmp/masm_failed_mounts.$$
ok_mounts=/tmp/masm_ok_mounts.$$

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

this_cell_file=/usr/vice/etc/ThisCell
this_cell=$(cat ${this_cell_file})
ROOT=/afs/.${this_cell}/temp_$$

doit "> ${new_cells}"
doit "> ${deleted_cells}"
doit "> ${deleted_links}"
doit "> ${failed_mounts}"
doit "> ${ok_mounts}"

doit "< ${CSDB} sed  -n -e \"s/^>\([^ 	]*\).*$/\1/p\" | sort > ${CSDB_list}"

doit "fs mkmount -dir ${ROOT} -vol root.afs"
if [ ${retcode} != 0 ]; then
	fatal "Unable to mount root.afs - check permissions - AFS admin."
fi

tstamp "Get a list of existing mounted cells."
tstamp "Use \"ls -l\" to get dirs and exclude symbolic links."
warning "This can take some time to complete."

doit "ls -l ${ROOT} | grep \"^d\" | awk '{print \$9}' > ${AFS_list}"

tstamp "Check for new cells"
doit "comm -23 ${CSDB_list} ${AFS_list} > ${new_cells}"

for cell in $(cat ${new_cells}); do
	doit "fs mkm -dir ${ROOT}/${cell} -vol root.cell -cell ${cell}"
	if [ ${retcode} = 0 ]; then
		doit "echo ${cell} >> ${ok_mounts}"
	else
		doit "echo ${cell} >> ${failed_mounts}"
	fi
done

tstamp "Check for cells no longer in ${CSDB}"
(cd ${ROOT}
	for cell in * ; do
		egrep -s "^$cell" ${CSDB_list} 
		if [ $? = 1 ]; then
			if [ -L $cell ]; then
				if [ $cell != "@cell" ]; then
					tstamp "remove symbolic link: ${cell}"
					doit "rm ${cell}"
					doit "echo ${cell} >> ${deleted_links}"
				fi
			else
				tstamp "remove mountpoint: ${cell}"
				doit "fs rmmount -dir ${ROOT}/${cell}"
				doit "egrep \"^>$cell\" $CSDB >> $deleted_cells"
			fi
		fi
	done   
)

doit "fs rmmount -dir ${ROOT}"
doit "vos release root.afs"
doit "fs checkv"

if [ -s "${new_cells}" ]; then
	tstamp "Summary of new cells:"
	doit "cat ${new_cells}"
	if [ -s "${ok_mounts}" ]; then
		tstamp "Summary of successfully foreign cell mounts:"
		doit "cat ${ok_mounts}"
	else
		tstamp "No successfull foreign cell mounts"
	fi
	if [ -s "${failed_mounts}" ]; then
		tstamp "Summary of failed foreign cell mounts:"
		doit "cat ${failed_mounts}"
	else
		tstamp "No failed foreign cell mounts"
	fi
else
	tstamp "No new cells"
fi

if [ -s "${deleted_cells}" ]; then
	tstamp "Summary of old cells removed:"
	doit "cat ${deleted_cells}"
else
	tstamp "No deleted cells"
fi

if [ -s "${deleted_links}" ]; then
	tstamp "Summary of old symbolic links removed:"
	doit "cat ${deleted_links}"
else
	tstamp "No deleted symbolic links"
fi

doit "rm $CSDB_list $AFS_list $new_cells $deleted_cells $deleted_links"
doit "rm $ok_mounts $failed_mounts"

T=/tmp/${CMD}.mail.$$
cat <<eeooff >${T}
Greetings,
AFS foreign cell mount synchronisation has completed.
You will find a log of this process on ${HOST} in:

        ${LOG}
--
Sincerely,
${CMD} program
eeooff
doit "mail -s \"${HOST}: ${CMD} completed\" ${NOTIFY} <${T}"
cat ${T}
rm ${T}

tstamp "completed"
