#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2019 
# 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 
#
# sccsid = "@(#)12   1.11   src/rsct/cfg_access/scaffold/ct_init_node.sh, cfg.access, rsct_rady, rady2035a 11/12/15 16:44:00"
#
# Temporary command file to enable RSCT HA subsystems 
# with the manually edited 'subsys.conf' and 'machines.lst'
#
# Usage: 
#   ct_init_node [-c clname] [-n nodenum] machines.lst [subsys.conf [nodelist]]
#

PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin
RSCTBIN=/opt/rsct/bin
CTROOT=/var/ct

#
basecmd=$(basename $0)
#
function usage
{
CMDLINE="\
${basecmd} [-c clstname] [-n nodenumber] \
machines.lst [subsys.conf [nodelist]]"
    print -u2 "Usage: $CMDLINE"
}

# process the command line
CLSTNAME=
NODENUM=
while getopts ":c:n:" opt; do
    case $opt in
	n ) NODENUM=${OPTARG} ;;
        c ) CLSTNAME=${OPTARG} ;;
        * )
            print -u2 "${basecmd}: Not a recognized flag: -${OPTARG}"
            usage
            exit 1
            ;;
    esac
done

shift $(($OPTIND - 1))

if [[ -z $CLSTNAME ]]; then
   # use 'scaffold' as the cluster name
   CLSTNAME=scaffold
fi

if [[ -z $1  || -z $CLSTNAME ]]; then
   # no two arguments
   usage
   exit 1
fi

MACHINESLST=$1
SUBSYSCONF=$2
NODELIST=$3

if [[ ! -a $MACHINESLST ]]; then
    print -u2 "File $MACHINESLST not found"
    exit 1;
fi

DEL_SUBSYSCONF=
if [[ ! -a $SUBSYSCONF ]]; then
	SUBSYSCONF=/tmp/.tmp_ha_subsys.conf
	echo "cthats 12347" > $SUBSYSCONF
	echo "cthags 12348" >> $SUBSYSCONF
	DEL_SUBSYSCONF=1
fi

if [[ -z $NODENUM ]]; then
   # no node number is defined...find it from machines.lst
   # search this from $MACHINESLST
   ipaddr=$(/bin/hostname -i)
   # first check whether it is in the new format
   NODENUM=$(grep -i ADAPTER $MACHINESLST | grep -w $ipaddr | awk '{print $4}')
   if [[ -z $NODENUM ]]; then
   	# get the only entry starting with an integer
   	NODENUM=$(grep -w $ipaddr $MACHINESLST | \
		  grep '^[ \t]*[0-9]' | awk '{print $1}')
   fi
   if [[ -z $NODENUM  && -a ${NODELIST} ]]; then
        # not found in the machines.lst...search in the /var/ct/NODELIST
        NODENUM=$(grep -w $ipaddr $NODELIST | \
		  grep '^[ \t]*[0-9]'| awk '{print $1}')
        if [[ -z $NODENUM ]]; then
                hostname=$(/bin/hostname -f)
                NODENUM=$(grep $hostname $NODELIST | \
			  grep '^[ \t]*[0-9]'| awk '{print $1}')
        fi
   fi
   if [[ -z $NODENUM ]]; then
	if [[ -z ${NODELIST} ]]; then
           print -u2 "Node number not found in $MACHINESLST"
 	else
           print -u2 "Node number not found in $MACHINESLST or $NODELIST"
	fi
        exit 1
   fi

fi

# Only supported CLSTNAME is currently "IW"
CLSTDIR=/var/ct/${CLSTNAME}

clstdir_exist=0		# assume /var/ct/<CLST> not exist
if [[ -d $CLSTDIR ]]; then
    # clstdir already exist
    clstdir_exist=1

    mkdir -p ${CLSTDIR}/cfg/
    # copy SUBSYSCONF
    cp -f $SUBSYSCONF ${CLSTDIR}/cfg/subsys.conf

    # also copy machines.lst
    cp -f $MACHINESLST ${CLSTDIR}/cfg/topology.conf
    cp -f $MACHINESLST ${CLSTDIR}/run/cthats/machines.lst
fi

# SCAFFOLD:
# The followings will be used by 'ct_{hags|hats}_info' and 'ct_topology_info'
export CT_TOPOLOGY_CONF_FILE=$MACHINESLST
export CT_SUBSYS_CONF_FILE=$SUBSYSCONF

# initialize the cluster
$RSCTBIN/cthactrl -i -c $CLSTNAME -n $NODENUM -e scaffold
rc=$?

if [[ $clstdir_exist -eq 0 && -d $CLSTDIR ]]; then
   #clstdir was created by 'cthactrl -i' 
   mkdir -p ${CLSTDIR}/cfg/
   # copy SUBSYSCONF
   cp -f $SUBSYSCONF ${CLSTDIR}/cfg/subsys.conf

   # also copy machines.lst 
   cp -f $MACHINESLST ${CLSTDIR}/cfg/topology.conf
   cp -f $MACHINESLST ${CLSTDIR}/run/cthats/machines.lst
fi

if [[ ! -z $DEL_SUBSYSCONF ]]
then
	rm -f $SUBSYSCONF
fi

exit $rc
