#!/bin/ksh -
#
# @(#)mk-afs-client-install	1.2 (hursley) 2/21/97
# /afs/hursley.ibm.com/common/src/afs/@cell/rs_aix32/usr/local/sbin/mk-afs-client-install/SCCS/s.mk-afs-client-install
#
# NAME          mk-afs-client-install
# PURPOSE       Sample script to install AFS client with minimal intervention.
# AUTHOR        Paul Blackburn     http://acm.org/~mpb/homepage.html
# WRITTEN       January 1996
# SEE ALSO	
#
#   ftp://ftp.transarc.com/pub/afs-contrib/tools/afs_install/README.html
#
# HISTORY	09Nov96 mpb now uses ${afs_install_base}
#		07nov96 mpb now checks that hostname is FQDN
#		25nov96 mpb removed most site dependencies - it's generic.
#		21feb97 mpb bugfix consistency when hostname made FQDN

cmd=$(basename ${0})
cmdline="${cmd} $*"
logdir=/var/log/install
log=${logdir}/${cmd}

# site configurable variables follow ---------------------------------------

server=your.site.tftp.server
afs_install=/afs/@cell/afs/install/afs_install.tar
defaultcachesize=16
default_cellname=your.afs.cellname

# end of site configurable variables section -------------------------------

# functions

fatal() {
	echo "${cmd} fatal: ${1}" >&2
	exit 1
}

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

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

tstamp() {
	echo "$(date '+''%H''%M'':%S') ${cmd}: ${1}"
}

doit() {
	tstamp "${1}"
	eval ${1}

	retcode=$?
	if [[ ${retcode} != 0 ]]; then
		error "\$?=${retcode}"
	fi
}

# main

mkdir -p ${logdir} 2>/dev/null          # make sure we have a $logdir

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

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

tstamp "Check if we are root"
X=$(whoami)
if [[ "${X}" != 'root' ]]; then
	fatal "You must be logged in as root to run ${cmd}"
fi

tstamp "Check if cellname specified on command line"
if [[ "${1}" = "-cell" ]]; then
	cellname="${2}"
	if [[ -z "${cellname}" ]]; then
		error "incorrect specification of cell name"
		fatal "usage: ${cmd} -cell \$cellname"
	fi
fi

if [[ -z "${cellname}" ]]; then
	cellname=${default_cellname}
fi

tstamp "Check if we already have AFS (idempotency rules! :-)"
df | grep -q "AFS"
if [[ $? = 0 ]]; then
	tstamp "AFS is already installed"
	tstamp "completed"
	exit
fi

# Check if hostname is a fully qualified domain name
hostname=$(hostname)
host=$(echo ${hostname} | awk -F. '{print $1}')

if [[ ${hostname} = ${host} ]]; then
	warning "Hostname is incorrectly set. Should be fully qualified name."
	domain=$(awk '$1 == "domain" { print $2 }' /etc/resolv.conf)
	if [[ -z "${domain}" ]]; then
		fatal "unable to determine domain name from /etc/resolv.conf"
	else
		tstamp "Now setting fully qualified domain name..."
		doit "hostname ${host}.${domain}"
		doit "chdev -linet0 -a hostname=${host}.${domain}"
		hostname=${host}.${domain}
	fi
else
	tstamp "hostname is already correctly set to fully qualified name."
fi

tstamp "Install AFS"
f=/tmp/$(basename ${afs_install})
doit "rm ${f} 2>&1 > /dev/null"
tstamp "getting afs_install scripts from ${server}"
doit "tftp -go ${f} ${server} ${afs_install} image"
if [[ ! -s ${f} ]]; then
	fatal "unable to get afs_install scripts from ${server}"
fi

export afs_install_base=/tmp/afs_install
doit "mkdir -p ${afs_install_base} 2>&1 > /dev/null"
tstamp "installing afs_install scripts in ${afs_install_base}"
doit "cd ${afs_install_base} && tar -xpvf ${f}"

tstamp "looking for an afs_install configuration file"
local=${afs_install_base}/usr/local/etc/afs_install/cf
if [[ -s "${local}" ]]; then
	tstamp "using existing AFS install config file: ${local}"
else
	tstamp "checking for an afs_install config file on ${server}"
	remote=/afs/@cell/common/etc/host/${host}/afs_install_cf

	doit "rm ${local} 2>&1 > /dev/null"
	doit "tftp -g ${local} ${server} ${remote} image"

	if [[ -s "${local}" ]]; then
		tstamp "using server supplied afs_install cf file"
	else
		tstamp "checking we have room for an AFS disk cache"
		vg=rootvg
		tstamp "Check there is enough free space in volume group: ${vg}"
		FREE=$(lsvg ${vg} | awk '{if ($4 == "FREE") print $6}')
		WANT=${defaultcachesize}
		if [[ ${WANT} -gt ${FREE} ]]; then
			error "Not enough free space in volume group: ${vg}"
			error "We have ${FREE} PPs free but need ${WANT}."
			doit "lsvg ${vg}"
		else
			tstamp "OK: adequate free space in volume group: ${vg}"
			tstamp "We have ${FREE} PPs free and need ${WANT}"

# further site configurable variables section -------------------------------
			tstamp "creating a default afs_install cf file"
			cat <<eeooff > ${local}
# afs_install configuration file
# created by ${cmd} on $(date)
#
# For more information:
#   afs_install  ftp://ftp.transarc.com/pub/afs-contrib/tools/afs_install/
#   AFS FAQ      http://www.transarc.com/Product/AFS/FAQ/faq.html

${hostname}:afscachename="/usr/vice/cache"
${hostname}:afscachesize="${defaultcachesize}"
${hostname}:afscachevg="rootvg"
${hostname}:afscellcomment="Your Cell details here"
${hostname}:afscellname="${cellname}"
${hostname}:afsclient="${host}"
${hostname}:afsdoptions="-stat 2000 -daemons 3 -volumes 70"
${hostname}:afsfileserver="your.fileserver.name"
${hostname}:afsimagedir="/vol/afs34a/\$sysname"
${hostname}:afsimagefs="/vol/afs34a"
${hostname}:afsimagefssize="4"
${hostname}:afsimageserver="${server}"
${hostname}:afsimagevg="rootvg"
${hostname}:afslicense=""
${hostname}:afsmemcache=""
${hostname}:afspartitions=""
${hostname}:afspostinstall=""
${hostname}:afsserver=""
${hostname}:afssystemcontrolmachine="your.cell.scm"
${hostname}:afstimeservers="IPaddress of NTP server"
${hostname}:afsusrbumpsize="3"
${hostname}:afswsfs="/vol/afsws"
${hostname}:afswsfssize="8"
${hostname}:afswsvg="rootvg"
eeooff
		fi
	fi
fi

tstamp "configuring ${host} as an AFS client"
doit "${afs_install_base}/usr/local/sbin/mk-afs-cli"
tstamp "completed"
