#!/usr/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72L src/bos/usr/sbin/C2admin/chCCadmin.sh 1.1.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2018 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)92	1.1.1.1  src/bos/usr/sbin/C2admin/chCCadmin.sh, cmdsadm, bos72L, l2018_12A5 3/19/18 06:23:58
#
# chCCadmin
#
# Change the name of the CC administrative host
#
# Usage:
#   chCCadmin [ -a address ] hostname
#

umask 027
unset PATH
export PATH=/usr/bin:/etc:/usr/sbin:/sbin:/usr/ucb

# ignore signals to prevent inconsistent state
trap "" INT HUP TERM QUIT EXIT

isCChost -i 2>/dev/null
case $? in
	0):
		# 2. check if currently in CC mode
		[[ ! -d /etc/data.shared ]] && {
			dspmsg CC.cat 3 "%s: This system was not configured to operate in CC mode.\n" ${0##*/} >&2
			exit 2
		}
		;;
	1):
		# CC installed but not initialized
		dspmsg CC.cat 3 "%s: This system was not configured to operate in CC mode.\n" ${0##*/} >&2
		exit 1
		;;
	2):
		# CC is not installed
		dspmsg CC.cat 2  "%s: This system was not installed using the CC option.\n" ${0##*/} >&2
		exit 1
		;;
	*):
		dspmsg CC.cat 6 "%s: An error occured while initializing the administrative host.\n" ${0##*/} >&2
		exit 2
		;;
esac

while getopts a: var
do
	case $var in
	a)	address="$OPTARG";;
	*)	dspmsg CC.cat 12 "Usage: chCCadmin [ -a address ] hostname\n" >&2
		exit 2
		;;
	esac
done

shift $(( $OPTIND - 1 ))

[[ -z $1 ]] && {
	dspmsg CC.cat 12 "Usage: chCCadmin [ -a address ] hostname\n" >&2
	exit 2
}

[[ $PWD = /etc/data.shared ]] && {
	dspmsg CC.cat 15 "%s: Cannot unmount %s.  Ensure that it is not your current directory.\n" ${0##*/} /etc/data.shared >&2
	exit 2
}

dspmsg CC.cat  14 "%s: The system may be left in an inconsistent state if this this script is interrupted.\n" ${0##*/}

hostname=$1

# verify the supplied hostname
if ! host "$hostname" >/dev/null 2>&1
then
	if [[ -z $address ]]
	then
		dspmsg CC.cat 5 "%s: Administrative host \"%s\" is unknown.\n" ${0##*/} >&2
		exit 2
	else
		# add to /etc/hosts
		hostent -a $address -h $hostname || {
			dspmsg CC.cat 6 "%s: An error occured while initializing the administrative host.\n" ${0##*/} >&2
			exit 2;
		}
	fi
fi

# update admin_host file
echo "$hostname" > /etc/security/admin_host || {
	dspmsg CC.cat 6 "%s: An error occured while initializing the administrative host.\n" ${0##*/} >&2
	exit 2;
}

# update local copy of hosts file
umount /etc/data.shared || {
	dspmsg CC.cat 13 "%s: An error occurred changing the administrative host.\n" ${0##*/} >&2
	exit 2
}
hostent -a $address -h $hostname 2>/dev/null
mount /etc/data.shared || {
	dspmsg CC.cat 13 "%s: An error occurred changing the administrative host.\n" ${0##*/} >&2
	exit 2
}

# update NFS mount stanzas
chnfsmnt -f /etc/data.shared -d /etc/data.master -h $hostname || {
	dspmsg CC.cat 13 "%s: An error occurred changing the administrative host.\n" ${0##*/} >&2
	exit 2
}

dspmsg CC.cat 11 "%s: The system must be rebooted for this change to take effect.\n" ${0##*/}

exit 0;