#!/bin/ksh -
#
# @(#)mk-afs-cf	1.39 (hursley) 3/6/98
# /afs/hursley.ibm.com/common/src/afs/@cell/asys/usr/local/sbin/mk-afs-cf/SCCS/s.mk-afs-cf
#
# NAME		mk-afs-cf
# AUTHOR	Paul Blackburn <mpb@acm.org>
# WRITTEN	27th July 1993
# PURPOSE	Common config file for mk-afs-srv* mk-afs-cli* install scripts.
#		Define global variables and functions, do global things.
#		Figure out the system type (rt_aix221, rs_aix32, rs_aix41 etc).
# USAGE		Normally invoked in mk-afs-srv* or mk-afs-cli by: ". mk-afs-cf"
#		Not intended to be run on its own.
#		This is an "include" file for other mk-afs* scripts
# HISTORY
#	1998-Mar-6 mpb Allow use of shell variable $sysname instead of
#		       computing the sysname with mk-afs-systype
#                      Added "rs_aix43" support

CMD=$(basename ${0})

# Commence being defensive...
#
# Set a miminal PATH to avoid any command name clashes like:
#     /bin/grep versus /usr/local/bin/grep

minPATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin
PATH=/usr/afs/bin:/usr/afs/etc:${minPATH}:${afs_install_base}/usr/local/sbin
export PATH

# Unset ${ENV} to avoid arbitary local .kshrc being run in sub-shells from here
unset ENV

# Conclude being defensive...

LOGDIR=/var/log/install
LOG=${LOGDIR}/${CMD}

# File containing AFS install parameters:
AFSICF=${afs_install_base}/usr/local/etc/afs_install/cf

[ -z "${NOTIFY}" ] && NOTIFY=${LOGNAME}
[ -z "${HOSTNAME}" ] && HOSTNAME=`hostname`
HD=${HOSTNAME}
if [[ -z "${HOST}" ]]; then
  HOST=$(localhost 2>/dev/null || (echo ${HOSTNAME} | awk -F. '{print $1}'))
fi

# Compute domain name

if [[ -f /etc/resolv.conf ]]; then
	domainname=$(awk '$1 == "domain" { print $2 }' /etc/resolv.conf)
else
	domainname=$(host ${HOST} | awk '{ print $1 }' | cut -d. -f2-)
fi

if [[ "${HOST}" = "${HOSTNAME}" ]]; then
	HOSTNAME=${HOST}.${domainname}
fi

partition=/vicepa
MKAFSCFTF=/tmp/${CMD}.tf.$$

# allow shell variable "sysname" to overide computation of sysname
if [[ -z "${sysname}" ]]; then
	sysname=$(mk-afs-systype)
fi

export domainname sysname partition
export HOST HOSTNAME HD NOTIFY AFSICF LOG LOGDIR CMD

CELLSERVDB=/usr/vice/etc/CellServDB
AFSCELLS=mk-afs-cells
FCELLS=mk-afs-fcells
SCRIPT=/tmp/${CMD}.script
TF=/tmp/${CMD}.tf.$$

export CELLSERVDB AFSCELLS SCRIPT TF FCELLS

# functions

fatal() {
	error "${1} - fatal error"
	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
}

getpass() {
	A="alpha"; B="beta"

	trap "echo; echo interrupted; stty echo" 1 2 3 15

	while [[ "${A}" != "${B}" ]]; do
		echo "${1} \c"
		stty -echo
		read A
		stty echo
		echo
		echo "Re-enter password: \c"
		stty -echo
		read B
		stty echo
		echo
		if [[ "${A}" != "${B}" ]]; then
			warning "passwords do not match, try again"
		else
			PASSWORD=${A}
		fi
	done
}

need_freespace()
{
# need_freespace $filesystem $kbytes
# returns 0 if we have the space (otherwise returns 1 if not)

	fs=$1
	need=$2
	space=$(df $fs | grep / | awk '{ print $3 }')
#	echo fs=$fs space=$space need=$need
	if [[ $space -ge $need ]]; then
		return 0
	else
		return 1
	fi
}

export fatal error warning tstamp doit getpass need_freespace

case ${sysname} in

	rs_aix31 | rs_aix32 | rs_aix41 | rs_aix42 | rs_aix43 )	X="whoami";;
	rs_aix221 )				X="who am i";;
	*)					fatal "unsupported system type"
						;;
esac

# Skip root ID check if $norootcheck=true

if [[ "${norootcheck}" != "true" ]]; then
	X=`${X}`
	if [[ "${X}" != 'root' ]]; then
		fatal "You must be logged in as root to run ${CMD}"
	fi
fi

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

# If we have the AFS Install Configuration File then extract data from it...
# skip this if $nocfcheck is "true"

if [[ "${nocfcheck}" != "true" ]]; then
	if [[ -s "${AFSICF}" ]]; then
		<${AFSICF} grep "${HOSTNAME}" | sed -e "/^#/d" -e "/^$/d" \
		| cut -d: -f2- >${MKAFSCFTF}

		if [[ -s ${MKAFSCFTF} ]]; then
			. ${MKAFSCFTF}
			rm ${MKAFSCFTF}
		else
			warning "check: ${AFSICF}"
			fatal "no AFS install config data for ${HOST}"
		fi
	else
		warning "${AFSICF} missing"
	fi
fi

unset MKAFSCFTF

# Initial AFS passwords: if not shell variables then check files.

afsafspwfile=/etc/security/afs.afs.pw

if [[ -z "${afspasswd}" ]]; then
	if [[ -s "${afsafspwfile}" ]]; then
		afspasswd=`cat ${afsafspwfile}`
		export afspasswd
	fi
fi

afsadminpwfile=/etc/security/afs.admin.pw

if [[ -z "${adminpasswd}" ]]; then
	if [[ -s "${afsadminpwfile}" ]]; then
		adminpasswd=`cat ${afsadminpwfile}`
		export adminpasswd
	fi
fi
