#!/bin/ksh -
#
# @(#)mk-afs-dbsm	1.7 (hursley) 10/31/96
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-dbsm/SCCS/s.mk-afs-dbsm
#
# NAME		mk-afs-dbsm
# AUTHOR	Paul Blackburn <mpb@acm.org>
# PURPOSE	Configure this host as AFS database server machine
#		eg: replicates kaserver ptserver vlserver and buserver
# USAGE		Normally invoked as part of system installation.
# ASSUMPTIONS   Make sure you have correctly defined AFS installation
#               parameters for your system. The file containing these
#               ($AFSICF) is defined in:
#			 ${afs_install_base}/usr/local/sbin/mk-afs-cf
#
#		The first AFS fileserver *must* have already been installed.

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}

tstamp "version 1.7 commenced on $(date '+%a %d %h %y')"

# If we are not running on an AFS server then error exit

if [[ -z "${afsserver}" ]]; then
	warning "Check: ${AFSICF}"
	fatal "${HOST} is not defined as an AFS server"
fi

# Check we have AFS admin password.

if [ -z "${adminpasswd}" ]; then

# Switch stdout and stderr back to tty to prompt for data:
	exec 1>&3
	exec 2>&4

	getpass "AFS password for admin:"
	adminpasswd=${PASSWORD}
	export adminpasswd

	if [ -z "${adminpasswd}" ]; then
		fatal "null adminpasswd"
	fi

	echo "stdout and stderr now being written to ${LOG}"

	exec 4>&2
	exec 3>&1
	exec 1>>${LOG}
	exec 2>&1
fi

# Check AFS installation parameters (already obtained from ${AFSICF})

if [ -z "${afslicense}" ]; then
	fatal "afslicense not defined for ${HOST}"
fi

if [ -z "${afscachevg}" ]; then
	warning "afscachevg not defined for ${HOST}"
	afscachevg="rootvg"
	warning "defaulting to: ${afscachevg}"
fi

if [ -z "${afscachename}" ]; then
	warning "afscachename not defined for ${HOST}"
	afscachename="/usr/vice/cache"
	warning "defaulting to: ${afscachename}"
fi

if [ -z "${afscachesize}" ]; then
	warning "afscachesize not defined for ${HOST}"
	afscache="12"
	warning "defaulting to: ${afscache} (4MB physical partitions)"
fi

if [ -z "${afsdoptions}" ]; then
	warning "afsdoptions not defined for ${HOST}"
	afsdoptions="-stat 2000 -dcache 800 -daemons 3 -volumes 70 -nosettime"
	warning "defaulting to: ${afsdoptions}"
fi

if [ -z "${afscellname}" ]; then
	warning "afscellname not defined for ${HOST}."
	X=`echo ${HOSTNAME} | cut -d. -f2-`
	if [ -z "${X}" ]; then
		fatal "failed to compute default afscellname"
	fi
	warning "defaulting to: ${X}"
	afscellname=${X}
fi

if [ -z "${afspartitions}" ]; then
	warning "afspartitions not defined for ${HOST}"
	afspartitions="rootvg:/vicepa:20"
	warning "defaulting to: ${afspartitions}"
fi

if [ -z "${afsimageserver}" ]; then
	warning "afsimageserver not defined for ${HOST}"
	afsimageserver="afsimageserver.aixssc.uk.ibm.com"
	warning "defaulting to: ${afsimageserver}"
fi

tstamp "Check we can reach afsfileserver=${afsfileserver}"
doit "ping ${afsfileserver} 56 1"
if [ ${retcode} != 0 ]; then
	fatal "Unable to ping ${afsfileserver}"
fi

if [ -z "${afsusrbumpsize}" ]; then
	warning "afsusrbumpsize not defined for ${HOST}"
	afsusrbumpsize="10"
	warning "defaulting to: ${afsusrbumpsize}"
fi

if [ -z "${afsimagedir}" ]; then
	warning "afsimagedir not defined for ${HOST}"
	afsimagedir="/vol/afs34/tapeimages/$sysname"
	warning "defaulting to: ${afsimagedir}"
fi

if [ -z "${afsimagefs}" ]; then
	warning "afsimagefs not defined for ${HOST}"
	afsimagefs="/vol/afs32"
	warning "defaulting to: ${afsimagefs}"
fi

if [ -z "${afsimagefssize}" ]; then
	warning "afsimagefssize not defined for ${HOST}"
	afsimagefssize="4"
	warning "defaulting to: ${afsimagefssize}"
fi

if [ -z "${afsimagevg}" ]; then
	warning "afsimagevg not defined for ${HOST}"
	afsimagevg="rootvg"
	warning "defaulting to: ${afsimagevg}"
fi

# Phew! Are we ready to start now? :-)

tstamp "klog -principal admin -password \${adminpasswd}"

klog -principal admin -password ${adminpasswd}

if [ $? != 0 ]; then
	fatal "failed to klog"
fi

doit "bos status ${HOST}"
doit "bos listhosts ${HOSTNAME}"

tstamp "AFS Installation Guide, page 3-32"
doit "bos create ${HOST} kaserver simple /usr/afs/bin/kaserver"
doit "bos create ${HOST} ptserver simple /usr/afs/bin/ptserver"
doit "bos create ${HOST} vlserver simple /usr/afs/bin/vlserver"
doit "bos create ${HOST} buserver simple /usr/afs/bin/buserver"

tstamp "Restart Authentication, Protection and Volume Location Servers"
tstamp "on every database server machine in ${afscellname}"

for i in `bos listhosts ${afsfileserver}| tail +2 | awk '{print $4}'`
do
	doit "bos restart ${i} kaserver ptserver vlserver buserver"
done

T=/tmp/${CMD}.mail.$$
cat <<eeooff >${T}
Greetings,
${HOST} has now been configured as an AFS database server machine
running: kaserver ptserver vlserver buserver

You will find a log of this process in:

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

doit "/usr/afsws/bin/unlog; # Do not leave tokens hanging around!"
tstamp "completed"
