#!/bin/ksh -
#
# @(#)mk-afs-ssc	1.37 (hursley) 11/1/96
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-ssc/SCCS/s.mk-afs-ssc
#
# NAME		mk-afs-ssc
# AUTHOR	Paul Blackburn <mpb@acm.org>
# PURPOSE	Unless it already exists, generate the AFS install config
#		file ($AFSICF) defined in:
#			${afs_install_base}/usr/local/sbin/mk-afs-cf
#		either from qhost data or by prompting user.
#
#		Later, $AFSICF is used by other afs install scripts
#		(mk-afs-srv-[1234]of4, mk-afs-cli).
# USAGE		Normally invoked as part on the system installation
#		(via mkcf and installscript)

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}

yesno() {
	if [ ! -z "${1}" ]; then
		prompt="${1} "
	fi

	ANSWER="x"
	while [ ${ANSWER} = "x" ]; do
		echo "${prompt}Enter: y=yes or n=no [default is yes]: \c"
		read ANSWER

		case "${ANSWER}" in
			"" | y | Y | yes | YES )
				ANSWER="Y"
				;;
			n | N | no | NO )
				ANSWER="N"
				;;
			* )
				ANSWER="x"
				;;
		esac
	done
	case ${ANSWER} in
		Y ) return 0;;
		N ) return 1;;
	esac
}

getqhostdata() {

# Get AFS parameters from qhost-db...

afsserver=`qhost -f name:${HOST} -f class:afsserver -o '%[name]'`
if [ -z "${afsserver}" ]; then
	warning "${HOST} is not defined as an AFS server in qhost-db"
fi

afsclient=`qhost -f name:${HOST} -f class:afsclient -o '%[name]'`
afsmemcache=`qhost -f name:${HOST} -f class:afsmemcache -o '%[name]'`

X=`qhost -f name:${HOST} -o '%[afscachevg]'`

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

X=`qhost -f name:${HOST} -o '%[afscachename]'`

if [ -z "${X}" ]; then
	warning "afscachename not defined in qhost for ${HOST}"
	afscachename="/usr/vice/cache"
	warning "defaulting to: ${afscachename}"
else
	afscachename=${X}
fi
X=`qhost -f name:${HOST} -o '%[afscachesize]'`

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

X=`qhost -f name:${HOST} -o '%[afsdoptions]'`

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

X=`qhost -f name:${HOST} -o '%[afscellname]'`

if [ -z "${X}" ]; then
	echo "afscellname not defined in qhost for ${HOST}"
	afscellname=aixssc.uk.ibm.com
	warning "defaulting to: ${afscellname}"
else
	afscellname=${X}
fi

X=`qhost -f name:${HOST} -o '%[afscellcomment]'`

if [ -z "${X}" ]; then
	warning "afscellcomment not defined in qhost for ${HOST}"
	afscellcomment="comment to be provided"
else
	afscellcomment=${X}
fi

X=`qhost -f name:${HOST} -o '%[afspartitions]'`

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

X=`qhost -f name:${HOST} -o '%[afstimeservers]'`

if [ -z "${X}" ]; then
	warning "afstimeservers not defined in qhost for ${HOST}"
	afstimeservers="9.66.4.41 129.33.10.13"
	warning "defaulting to: ${afstimeservers}"
else
	afstimeservers=${X}
fi

X=`qhost -f name:${HOST} -o '%[afsimageserver]'`

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

X=`qhost -f name:${HOST} -o '%[afsimagedir]'`

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

X=`qhost -f name:${HOST} -o '%[afsimagefs]'`

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

X=`qhost -f name:${HOST} -o '%[afsimagefssize]'`

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

X=`qhost -f name:${HOST} -o '%[afsimagevg]'`

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

X=`qhost -f name:${HOST} -o '%[afsusrbumpsize]'`

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

X=`qhost -f name:${HOST} -o '%[afslicense]'`

if [ -z "${X}" ]; then
	warning "afslicense not defined in qhost for ${HOST}"
else
	afslicense=${X}
fi

X=`qhost -f name:${HOST} -o '%[afsfileserver]'`

if [ -z "${X}" ]; then
	echo "afsfileserver not defined in qhost for ${HOST}"
	afsfileserver=ariel.aixssc.uk.ibm.com
	warning "defaulting to ${afsfileserver}"
else
	afsfileserver=${X}
fi

X=`qhost -f name:${HOST} -o '%[afssystemcontrolmachine]'`

if [ -z "${X}" ]; then
	warning "afssystemcontrolmachine not defined in qhost for ${HOST}"
else
	afssystemcontrolmachine=${X}
fi

X=`qhost -f name:${HOST} -o '%[afswsfs]'`

if [ -z "${X}" ]; then
	warning "afswsfs not defined in qhost for ${HOST}"
	afswsfs="/vol/afsws"
	warning "defaulting to: ${afswsfs}"
else
	afswsfs=${X}
fi

X=`qhost -f name:${HOST} -o '%[afswsfssize]'`

if [ -z "${X}" ]; then
	warning "afswsfssize not defined in qhost for ${HOST}"
	afswsfssize="8"
	warning "defaulting to: ${afswsfssize} PP"
else
	afswsfssize=${X}
fi

X=`qhost -f name:${HOST} -o '%[afswsvg]'`

if [ -z "${X}" ]; then
	warning "afswsvg not defined in qhost for ${HOST}"
	afswsvg="rootvg"
	warning "defaulting to: ${afswsvg}"
else
	afswsvg=${X}
fi

X=`qhost -f name:${HOST} -o '%[afspostinstall]'`

if [ -z "${X}" ]; then
	warning "afspostinstall not defined in qhost for ${HOST}"
else
	afspostinstall=${X}
fi

}	# end getqhostdata

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

	if yesno "Is ${HOST} an AFS fileserver?"
	then
		afsserver=${HOST}
	else
		afsserver=""
	fi

	if yesno "Is ${HOST} an AFS client?"
	then
		afsclient=${HOST}
		if yesno "Will ${HOST} use an AFS disk cache?"
		then
			afsmemcache=""
		else
			afsmemcache=${HOST}
		fi
		deflt="rootvg"
		echo "Enter AIX volume group for AFS disk cache (${deflt}): \c"
		read afscachevg
		[ -z "${afscachevg}" ] && afscachevg=${deflt}

		deflt="/usr/vice/cache"
		echo "Enter filesystem name for AFS cache (${deflt}): \c"
		read afscachename
		[ -z "${afscachename}" ] && afscachename=${deflt}

		default=4
		echo "Enter size of AFS cache in 4MB PP (${default}): \c"
		read afscachesize
		[ -z "${afscachesize}" ] && afscachesize=${default}

		deflt="-stat 2000 -dcache 800 -daemons 3 -volumes 70"
		echo "AFS Installation Guide page 2-51"
		echo "Enter afsd options (${deflt}): \c"
		read afsdoptions
		[ -z "${afsdoptions}" ] && afsdoptions=${deflt}
	else
		afsclient=""
		afscachevg=""
		afscachename=""
		afscachesize=""
		afsdoptions=""
	fi

	echo "Enter name of system to tftp AFS tape images from: \c"
	read afsimageserver
	
	default=`hostname | cut -d. -f2-`
	echo "Enter AFS cell name (${default}: \c"
	read afscellname
	[ -z "${afscellname}" ] && afscellname=${default}

	default="none"
	echo "Enter AFS cell name comment (${default}: \c"
	read afscellcomment
	[ -z "${afscellcomment}" ] && afscellcomment=${default}

	echo "AFS partion data is expressed as a triple:"
	echo "<volumegroup>:<afspartitionname>:<sizeinPP>"
	default="rootvg:/vicepa:20"
	echo "Enter AFS partion data (${default}): \c"
	read afspartitions
	[ -z ${afspartitions} ] && afspartitions="${default}"

	echo "AFS timeservers are expressed in IP addresses"

	default="9.66.4.41 129.33.10.13"
	echo "Enter AFS timeservers list (${default}): \c"
	read afstimeservers
	[ -z ${afstimeservers} ] && afstimeservers="${default}"

	echo "stdout and stderr now being written to ${LOG}"
	exec 4>&2
	exec 3>&1
	exec 1>>${LOG}
	exec 2>&1
}

# main

echo "stdout and stderr now being written to ${LOG}"
exec 4>&2
exec 3>&1
exec 1>${LOG}
exec 2>&1
tstamp "version 1.37 commenced on `date '+%a %d %h %y'`"

# Use first parameter as file name for AFSICF if given on command line

if [ ! -z "${1}" ]; then
	AFSICF=${1}
	tstamp "writing AFS install configuration to ${AFSICF}"
fi

if [ -s "${AFSICF}" ]; then
	fatal "${AFSICF} exists and will not be overwritten"
fi

# Use 2nd parameter as HOST if given on command line

if [ ! -z "${2}" ]; then
        HOST=${2}
        tstamp "computing AFS install parameters for host ${HOST}"
	HD=`host ${HOST} | awk '{print $1}'`
fi

# qhost command available?

type qhost >/dev/null 2>&1

if [ $? != 0 ]; then
	tstamp "Prompting user for AFS configuration data"
	promptdata
else
	tstamp "Extracting AFS configuration data from qhost-db"
	getqhostdata
fi

# Initial AFS passwords (if defined)

if [ ! -z "${afspasswd}" ]; then
	echo "${afspasswd}" >${afsafspwfile}
	chmod 600 ${afsafspwfile}
fi

if [ ! -z "${adminpasswd}" ]; then
	echo "${adminpasswd}" >${afsadminpwfile}
	chmod 600 ${afsadminpwfile}
fi

# Create ${AFSICF} containing AFS installation configuration data

STAMP=`date '+''%H''%M'':%S on %a %d %h %y'`

echo "# NAME\t\t${AFSICF}" >${AFSICF}
echo "# PURPOSE\tAFS installation configuration data" >>${AFSICF}
echo "# created by ${LOGNAME} on `hostname`" >>${AFSICF}
echo "# using ${CMD} at ${STAMP}" >>${AFSICF}
echo >>${AFSICF}

echo "${HD}:afscachename=\"${afscachename}\"" >>${AFSICF}
echo "${HD}:afscachesize=\"${afscachesize}\"" >>${AFSICF}
echo "${HD}:afscachevg=\"${afscachevg}\"" >>${AFSICF}
echo "${HD}:afscellcomment=\"${afscellcomment}\"" >>${AFSICF}
echo "${HD}:afscellname=\"${afscellname}\"" >>${AFSICF}
echo "${HD}:afsclient=\"${afsclient}\"" >>${AFSICF}
echo "${HD}:afsdoptions=\"${afsdoptions}\"" >>${AFSICF}
echo "${HD}:afsfileserver=\"${afsfileserver}\"" >>${AFSICF}
echo "${HD}:afsimagedir=\"${afsimagedir}\"" >>${AFSICF}
echo "${HD}:afsimagefs=\"${afsimagefs}\"" >>${AFSICF}
echo "${HD}:afsimagefssize=\"${afsimagefssize}\"" >>${AFSICF}
echo "${HD}:afsimageserver=\"${afsimageserver}\"" >>${AFSICF}
echo "${HD}:afsimagevg=\"${afsimagevg}\"" >>${AFSICF}
echo "${HD}:afslicense=\"${afslicense}\"" >>${AFSICF}
echo "${HD}:afsmemcache=\"${afsmemcache}\"" >>${AFSICF}
echo "${HD}:afspartitions=\"${afspartitions}\"" >>${AFSICF}
echo "${HD}:afspostinstall=\"${afspostinstall}\"" >>${AFSICF}
echo "${HD}:afsserver=\"${afsserver}\"" >>${AFSICF}
echo "${HD}:afssystemcontrolmachine=\"${afssystemcontrolmachine}\"" >>${AFSICF}
echo "${HD}:afstimeservers=\"${afstimeservers}\"" >>${AFSICF}
echo "${HD}:afsusrbumpsize=\"${afsusrbumpsize}\"" >>${AFSICF}
echo "${HD}:afswsfs=\"${afswsfs}\"" >>${AFSICF}
echo "${HD}:afswsfssize=\"${afswsfssize}\"" >>${AFSICF}
echo "${HD}:afswsvg=\"${afswsvg}\"" >>${AFSICF}

echo >>${AFSICF}

doit "cat ${AFSICF}"
tstamp "completed"
