#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2008 
# 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 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  recfgct                                                     */
#*                                                                           */
#* Description:                                                              */
#*      Script to reconfigure RSCT subsystems including reassigning node id  */
#*                                                                           */
#*===========================================================================*/
# sccsid = "@(#)63   1.16   src/rsct/rmc/mcdaemon/recfgct.sh, mcdaemon, rsct_rfos, rfos0838a 7/24/08 10:49:41"

# Set path to known value
PATH=/usr/sbin/rsct/install/bin:/usr/sbin/rsct/bin:/usr/bin:/bin:/usr/sbin
export PATH

# set command to display help message
CTMSG=ctdspmsg
CMD=$(basename $0)

# Get option arguments; 
#   -n          means clean up the node id file (default option)
#   -s          save the node id file.
#   -i <NID>    make NID the node ID, where NID is a string of no more than
#               sixteen hex digits (no leading 0x or 0X)
#
# N.B. these arguments are mutally exclusive

# get machine ID
OS=$(uname -s)

if [[ $OS = "Interix" ]]
then
	# Save "/var/ct/cfg/ctadmins.cfg" to "/var/ctadmins.cfg"
	if [[ -f /var/ctadmins.cfg ]]; then
		rm -f /var/ctadmins.cfg
	fi

	if [[ -f /var/ct/cfg/ctadmins.cfg ]]
	then
		mv  /var/ct/cfg/ctadmins.cfg /var/ctadmins.cfg
	fi
fi

#
Argcount=0
cleannodeid=1
setnodeid=0
while getopts :i:nsh opt
do
	case $opt in
                i) setnodeid=1; NID=$OPTARG
                                    Argcount=$((Argcount + 1));;
                n) cleannodeid=1;   Argcount=$((Argcount + 1));;
                s) cleannodeid=0;   Argcount=$((Argcount + 1));;
                h) $CTMSG recfgct ct_rmc.cat INFOMsg_recfgct_Usage_B $CMD
                     exit 0;;
                ?) $CTMSG recfgct ct_rmc.cat INFOMsg_recfgct_Usage_B $CMD
                     exit 1;;
        esac
done

if ((Argcount > 1))
then
	$CTMSG recfgct ct_rmc.cat INFOMsg_recfgct_Usage_B $CMD
	exit 1
fi

if [[ $setnodeid -ne 0 ]]; then

	# validate the Node ID before doing anything destructive

	if ! mknodeid -v $NID
	then
		exit 1
	fi
fi

#check the existence of /var/ct/cfg/*.ckpt
CKPTS=$(LC_ALL=C ls /var/ct/cfg/*.ckpt 2> /dev/null)

#check the existence of START_PORT_FILE
START_PORT_FILE=/var/ct/cfg/ctrmc.rpe   # rpe == remote port enable
if [[ -a $START_PORT_FILE ]]; then
    CTRMCRPE_EXIST=1
else
    CTRMCRPE_EXIST=0
fi

#check the existence of MSG_POLICY_FILE
MSG_POLICY_FILE=/var/ct/cfg/ctrmc.msgp
if [[ -s $MSG_POLICY_FILE ]]
then
	POLICY=$(< $MSG_POLICY_FILE)
else
	POLICY=
fi

#check the existence of DMSG_POLICY_FILE
DMSG_POLICY_FILE=/var/ct/cfg/ctrmc.dmsgp
if [[ -s $DMSG_POLICY_FILE ]]
then
	DPOLICY=$(< $DMSG_POLICY_FILE)
else
	DPOLICY=
fi

#reconfigure RSCT
if [[ $cleannodeid -ne 0 ]]; then
	uncfgct -n
else
	uncfgct
fi


if [[ $OS = "Interix" ]]
then
	# Interix
	# Restore "/var/ct/cfg/ctadmins.cfg" from "/var/ctadmins.cfg"
	if [[ -f /var/ctadmins.cfg ]]
	then
		mv /var/ctadmins.cfg /var/ct/cfg/ctadmins.cfg 
	fi
fi

if [[ $CTRMCRPE_EXIST -ne 0 ]]; then
    # start the remote port if it was previously enabled (on next start of ctrmc)
    touch $START_PORT_FILE 
    chmod 440 $START_PORT_FILE
fi

#re-establish message policies
case $POLICY in
	R | E | D ) print $POLICY > $MSG_POLICY_FILE
				chmod 440 $MSG_POLICY_FILE
				;;
esac

case $DPOLICY in
	R | E | D ) print $DPOLICY > $DMSG_POLICY_FILE
				chmod 440 $DMSG_POLICY_FILE
				;;
esac

# invoke cfgct
if [[ $setnodeid -ne 0 ]]; then
	cfgct -i $NID
else
	cfgct 
fi

if [[ -n $CKPTS ]]; then
	# create this again for the ptf-reject
	touch $CKPTS
fi

exit 0
