#!/bin/sh
#	ident	"@(#)rename_host.sh	1.6	96/09/18 CRS"
PATH="/usr/sbin:/bin:/usr/ucb:."
export PATH

warn()
{
	echo "${0}: $*" | fmt >&2
}

abort()
{
	warn "$*"
	exit 1
}

ec()
{
	echo "# $*"
	eval $*
}

###############################################################################
#
# Make sure we are really the super user.
#
###############################################################################
IDSTR="`id`"
UID=`expr "$IDSTR" : 'uid=\([0-9][0-9]*\)(.*'`
if [ "$UID" != "0" ]
then
        abort "must be super user"
fi

###############################################################################
#
# Basic initializations.
#
###############################################################################
sspname=`uname -n`
MACHINE="CS6400"
PKG=CYRSssp
CRAY_SSPHOME=/export/home/ssp
if [ -f /etc/opt/${PKG}/cray_ssp_env.sh ]; then
	. /etc/opt/${PKG}/cray_ssp_env.sh
else
	abort "couldn't find /etc/opt/${PKG}/cray_ssp_env.sh!"
fi
SSPHOSTS=${CRAY_SSPETC}/hosts

###############################################################################
#
# Other checks.
#
###############################################################################
if [ ! -s ${SSPHOSTS} ]; then
	abort "No SSP host is currently defined in ${SSPHOSTS}."
fi

###############################################################################
#
# This shell function formats output to 72 characters per line via the fmt
# program.  It also removes the newline from the last line.  It is meant
# to be used for queries.
#
###############################################################################
echoq () {
	echo $* | fmt > /tmp/request.ssp.$$
	n=`wc -l /tmp/request.ssp.$$ | awk '{print $1}'`
	p=`expr $n - 1`
	if [ $p -gt 0 ]; then
		head -$p /tmp/request.ssp.$$
	fi
	tail -1 /tmp/request.ssp.$$ | awk '{printf("%s ",$0)}'
	rm -f /tmp/request.ssp.$$
}

###############################################################################
#
###############################################################################
checkname()
{
	if grep "^$1 " ${SSPHOSTS}
	then
		return 0
	else
		return 1
	fi
}

echo ""

###############################################################################
#
# Parse arguments.
#
###############################################################################
if [ $# -eq 2 ]; then
	if [ $1 = $sspname ]; then
		abort "$1 is the same as the SSP's hostname."
	fi
	if [ $2 = $sspname ]; then
		abort "$2 is the same as the SSP's hostname."
	fi
	if checkname $1 ; then
		oldname=$1
	else
		warn "WARNING! $1 was not in ${SSPHOSTS}"
		echoq "Are you sure you want to rename ${1}? [n]:"
		read A
		A=`echo "${A}" | tr '[A-Z]' '[a-z]'`
		if [ "${A}" = "y" -o "${A}" = "yes" ]; then
			oldname=$1
		else
			abort "$0 aborted."
		fi
	fi
	if checkname $2 ; then
		warn "WARNING! $2 is already in ${SSPHOSTS}"
		echoq "Are you sure you want to rename ${oldname} to ${2}? [n]:"
		read A
		A=`echo "${A}" | tr '[A-Z]' '[a-z]'`
		if [ "${A}" = "y" -o "${A}" = "yes" ]; then
			newname=$1
		else
			abort "$0 aborted."
		fi
	else
		newname=$2
	fi
else
	abort "Usage: rename_host oldname newname"
fi

echo "Attempting to rename ${oldname} to ${newname}"

###############################################################################
#
#  Fix ${CRAY_SSPETC}/hosts and ${CRAY_SSPETC}/${oldname}/machine_config
#
###############################################################################
for FILE in ${SSPHOSTS} ${CRAY_SSPETC}/${oldname}/machine_config
do
	echo "Modifying ${FILE}"
	cp ${FILE} ${FILE}.save
	/usr/ucb/chown ssp.staff ${FILE}.save
	sed "s/^${oldname} /${newname} /" ${FILE}.save > ${FILE}
done

###############################################################################
#
#  Fix each loopback systems etc file "physical_host" contents.
#
###############################################################################


for h in `grep -v "^${newname} " ${SSPHOSTS} | awk '{print $1}'`
do
        # if renaming a domain, make sure you don't update
        # the main domain's physical_host because it is
        # not supposed to have one.
        if [ -f  ${CRAY_SSPETC}/${h}/physical_host ]; then
                 main_domain=`grep ${oldname} ${CRAY_SSPETC}/${h}/physical_host`
                 if [ -z "${main_domain}" ]; then
                         # the host to be renamed is not the main domain.
                         # no need to update the physical host files
                         break
                 else
                          if [ ${main_domain} = ${oldname} ]; then
                                # We are renaming the main domain
                                echo ${newname} > ${CRAY_SSPETC}/${h}/physical_host
                          fi
                 fi
        fi
done

###############################################################################
#
#  Fix ~ssp/.cray_ssp_env
#
###############################################################################
FILE=${CRAY_SSPHOME}/.cray_ssp_env
echo "Modifying ${FILE}"
cp ${FILE} ${FILE}.save
/usr/ucb/chown ssp.staff ${FILE}.save
sed "s/CRAY_HOSTNAME ${oldname}/CRAY_HOSTNAME ${newname}/" ${FILE}.save > ${FILE}

###############################################################################
#
# Create various hostname dependant directories, files and links.
#
###############################################################################
DIRS=" \
	${CRAY_SSPETC} \
	${CRAY_SSPADM} \
"

for i in ${DIRS}
do
	if [ -d ${i}/${oldname} ]; then
		if [ -d ${i}/${newname} ]; then
			warn "WARNING! ${i}/${newname} ALREADY EXISTS!"
		else
			ec mv ${i}/${oldname} ${i}/${newname}
		fi
	else
		echo "WARNING! ${i}/${oldname} DOES NOT EXIST!" >&2
	fi
done

for i in binary binary.bu
do
	old=${CRAY_SSPROOT}/data/${oldname}/${oldname}.${i}
	new=${CRAY_SSPROOT}/data/${oldname}/${newname}.${i}
	if [ -f ${old} ]; then
		ec mv ${old} ${new}
	fi
done

for i in CRAY_SD config/CRAY_SD.config
do
	src=${CRAY_SSPROOT}/data/$i
	old=`echo $src | sed "s/CRAY_SD/${oldname}/"`
	new=`echo $src | sed "s/CRAY_SD/${newname}/"`
	if [ -h ${old} -o -f ${old} ]; then
		if [ -h ${new} -o -f ${new} ]; then
			warn "WARNING! ${new} already exists!"
		else
			ec mv ${old} ${new}
		fi
	fi
done

old=${CRAY_SSPROOT}/data/common/${oldname}.signatures
new=${CRAY_SSPROOT}/data/common/${newname}.signatures
if [ -f ${old} ]; then
	ec mv ${old} ${new}
fi

###############################################################################
#
# /etc/remote nastyness.
#
###############################################################################
FILENAME=remote
FILE=/etc/${FILENAME}
echo "Modifying ${FILE}"
cp ${FILE} ${FILE}.save
SEDSC=/tmp/${FILENAME}.sed$$
cat > ${SEDSC} << EOF
/# ${PKG} start/,/# ${PKG} end/d
EOF
cat > ${FILE} << EOF
# ${PKG} start
${newname}|host|superdragon|CRAY_HOSTNAME:\\
        :dv=/dev/term/a:br#9600:el=^C^S^Q^U^D:ie=%\$:oe=^D:
# ${PKG} end
EOF
sed -f ${SEDSC} ${FILE}.save >> ${FILE}
rm -f ${SEDSC}

###############################################################################
#
#  Fix up some SSP user files.
#
###############################################################################
XFILES=".openwin-init .openwin-menu-ssp"
for xf in ${XFILES} ; do
	cp ${CRAY_SSPHOME}/${xf} ${CRAY_SSPHOME}/${xf}.save
	/usr/ucb/chown ssp.staff ${CRAY_SSPHOME}/${xf}.save
	echo "Modifying ${CRAY_SSPHOME}/${xf}"
	sed	-e "s/${oldname} Console\.\.\./${newname} Console.../g" \
		-e "s/${oldname} - Console/${newname} - Console/g" \
		-e "s/tip -v ${oldname}/cvc/g" \
		-e "s/cvc ${oldname}/cvc/g" \
		${CRAY_SSPHOME}/${xf}.save > ${CRAY_SSPHOME}/${xf}
done

