#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,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:  cfgct                                                       */
#*                                                                           */
#* Description:                                                              */
#*      Script to configure the RSCT subsystems at installation              */
#*                                                                           */
#* N.B. This command is intended to be executed by the RMC subsystem only !! */
#*                                                                           */
#*===========================================================================*/
# sccsid = "@(#)10   1.19   src/rsct/rmc/mcdaemon/cfgct.sh, mcdaemon, rsct_rfos, rfos0838a 7/23/08 16:05:50"

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

CTROOTDIR=/var/ct		# root directory for all RSCT run-time data
rc=0									# return code

#============================================================================
#
# Test for debug environment variable. If its value is 1, exit immediately
# with a 0 exit value. If it's value is 2, set INUBOSTYPE for testing BOS
# installation. If it's value is 3, set INUCLIENTS for NIM testing
#
#============================================================================

if [[ ${CFGCT_DEBUG:-0} -eq 1 ]]
then
	exit 0
fi

if [[ ${CFGCT_DEBUG:-0} -eq 2 ]]
then
	export INUBOSTYPE=1
fi

if [[ ${CFGCT_DEBUG:-0} -eq 3 ]]
then
	export INUCLIENTS=1
fi

umask 022


#============================================================================
#
# Step 0: parse cmdline options
#
# Interix: the following are optionally passed
#     -G AdminGroup
#     -U AdminUser
#============================================================================

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

CfgCtInstall=0
Argcount=0
SetNodeID=0

if [[ $OS = "Interix" ]]
then
    # Interix
    CTADMINS=/var/ct/cfg/ctadmins.cfg
    if [[ -f $CTADMINS ]]; then
	# /var/ct/cfg/ctadmins.cfg contains the following entries
	# 	GROUP=<group>		# no space before/after "="
	# 	USER=<user>

	AdminGroup=$(grep -w GROUP $CTADMINS | sed -e "s/GROUP=//")
	AdminUser=$(grep -w USER $CTADMINS | sed -e "s/USER=//")
    fi

    if [[ -z $AdminGroup ]]; then
	AdminGroup="+Administrators"	# default Admin Group
    fi
    if [[ -z $AdminUser ]]; then
	AdminUser="Administrator"	# default Admin User
    fi

    while getopts ":IG:U:i:" opt
    do
	case $opt in
		G ) AdminGroup=$OPTARG;;
		U ) AdminUser=$OPTARG;;
		I ) CfgCtInstall=1; Argcount=$((Argcount + 1));;
		i ) SetNodeID=1; NID=$OPTARG;;
		? ) exit 0;;
	esac
    done

else
 # Non-Interix
 while getopts ":Ii:" opt
 do
    case $opt in
        I ) CfgCtInstall=1; Argcount=$((Argcount + 1));;
        i ) SetNodeID=1; NID=$OPTARG;;
        ? ) exit 0;;
    esac
 done
fi

#============================================================================
#
# Step 1: make the node id
#
#============================================================================

if [[ $SetNodeID -ne 0 ]]
then
	MKNODEID_ARGS="-f -i $NID"
else
	MKNODEID_ARGS="-k"
fi

if ! mknodeid $MKNODEID_ARGS
then
	exit 1
fi

#============================================================================
#
# Step 2: check if /var/ct/IW exists. If so, then configuration has already
# been done. Set-up the ctposti script to run on AIX if called during install,
# otherwise exit.
#
#============================================================================

if [[ -a $CTROOTDIR/IW ]]
then
	if [ $CfgCtInstall -eq 1 ]
	then
		if [[ $OS = "AIX" ]]
		then
			/usr/sbin/rsct/install/bin/ctinstall -t I -f rsct.core.rmc -g RSCT_ALL
			rc=$?
		fi
	fi
	exit 0
fi

#============================================================================
#
# Step 3: create required directories and configuration files in /var
#
#============================================================================

if [[ $OS = "AIX" ]]
then
	LC_ALL=C uname -x | read r1 r2 MID junk
elif [[ $OS = "Interix" ]]; then
        MID=$(lsnodeid)
elif [[ $OS = "SunOS" ]]; then
        MID=$(makeuuid)
else
	MID=$(uuidgen)
fi

# create directories, using the machine ID as the cluster ID

for name in                        \
	$CTROOTDIR/$MID                \
	$CTROOTDIR/$MID/cfg            \
	$CTROOTDIR/$MID/lck            \
	$CTROOTDIR/$MID/log            \
	$CTROOTDIR/$MID/registry       \
	$CTROOTDIR/$MID/run            \
	$CTROOTDIR/$MID/soc
do
	if [[ ! -a $name ]]
	then
		# Make "mkdir" successful using "-p" option if it already exists
		if ! mkdir -p $name
		then
			exit 1
		fi
		if [[ $OS = "Interix" ]]; then
			chown "$AdminUser" $name
			chgrp "$AdminGroup" $name
		else
			chown bin $name
			chgrp bin $name
		fi
		chmod 750 $name
	fi
done

# allow "other" necessary access to use CLI

chmod 755 $CTROOTDIR/$MID
chmod 755 $CTROOTDIR/$MID/cfg
chmod 755 $CTROOTDIR/$MID/soc

# link the cluster name

ln -s $CTROOTDIR/$MID $CTROOTDIR/IW

# make config files

print "$MID IW" > $CTROOTDIR/cfg/clusters
print "$MID" > $CTROOTDIR/cfg/default_cluster
print "$MID" > $CTROOTDIR/cfg/current_cluster

print "1 $MID localhost $MID IW 1" > $CTROOTDIR/IW/cfg/nodedef.cfg

# create the "first start" file

touch $CTROOTDIR/first_start

#============================================================================
#
# Step 4: add the RMC subsystem and start it, unless INUCLIENTS is set
# or INUBOSTYPE is set
#
#============================================================================

# add security subsystem to SRC first

ctcasctrl -a 

if rmcctrl -a
then
	if [[ -z "$INUCLIENTS" && -z "$INUBOSTYPE" ]]
	then
		if ! rmcctrl -s
		then
			exit 1
		fi
	fi
else
	exit 1
fi

exit 0
