#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# IBM CONFIDENTIAL 
#  
# OBJECT CODE ONLY SOURCE MATERIALS 
#  
# (C) COPYRIGHT International Business Machines Corp. 2006,2007 
# All Rights Reserved 
#  
# The source code for this program is not published or otherwise 
# divested of its trade secrets, irrespective of what has been 
# deposited with the U.S. Copyright Office. 
#  
# IBM_PROLOG_END_TAG 
######################################################################
#"@(#)16   1.4   src/rsct/rm/ConfigRM/cli/bin/forcerpoffline.sh, configrmcli, rsct_relgh, relghs001a 8/29/06 10:45:29"
######################################################################
#                                                                    #
# Module: forcerpoffline                                             #
#                                                                    #
# Purpose:                                                           #
#   forcerpoffline - forces an already defined RSCT peer domain      #
#                   offline.                                         #
#                                                                    #
# Syntax:                                                            #
#   forcerpoffline  [-h] PeerDomain                                  #
#                                                                    #
# Flags:                                                             #
#   -h      Help. Writes the command's usage statement to standard   #
#           output and exits.                                        #
#                                                                    #
# Operands:                                                          #
#   PeerDomain  The name of a previously defined peer domain to be   #
#               brought offline.                                     #
#                                                                    #
# Description:                                                       #
#   The forcerpoffline command brings a defined peer domain offline  #
#   by modifying the content of current_cluster and default_cluster  # 
#   files in /var/ct/cfg, and by restarting the IBM.ConfigRM and     #
#   ctrmc subsystems.                                                # 
#                                                                    #
#                                                                    #
# Exit Values:                                                       #
#   0  CRM_CLI_SUCCESS       Command completed successfully.         #
#   1  CRM_CLI_RMC_ERROR     Command terminated due to an underlying #
#                            RMC error.                              #
#   2  CRM_CLI_ERROR         Command terminated due to an underlying #
#                            error in the command script.            #
#   3  CRM_CLI_BAD_FLAG      Command terminated due to user          #
#                            specifying an invalid flag.             #
#   4  CRM_CLI_BAD_OPERAND   Command terminated due to user          #
#                            specifying a bad operand.               #
#   5  CRM_CLI_USER_ERROR    Command terminated due to a user error, #
#                            for example specifying a domain name    #
#                            that does not exist.                    #
#
SS_PATH=/usr/sbin/rsct/bin
MSGMAPPATH=/usr/sbin/rsct/msgmaps
CTMSG=${SS_PATH}/ctdspmsg
CMD_CAT=configrmcli.cat
export MSGMAPPATH
cmdName=$0
#####################################################################
# Function: run_cmds
#####################################################################
function run_cmds {
	IWid=`fgrep IW /var/ct/cfg/clusters | cut -f 1 -d' '`
	if test -z "$IWid"
	then 
		$CTMSG forcerpoffline $CMD_CAT EMsgforcerpofflineRMCError $cmdName
		exit 1
	fi

	$SS_PATH/rmcctrl -z
	stopsrc -cs cthats
	stopsrc -cs cthags

	for f in current_cluster default_cluster
	do
		printf "%s\n" $IWid > /var/ct/cfg/${f}
		if test "$IWid" != `cat /var/ct/cfg/${f}`
		then 
			$CTMSG forcerpoffline $CMD_CAT EMsgforcerpofflineBadData $cmdName /var/ct/cfg/${f}
			exit 2
		fi
	done
	$SS_PATH/rmcctrl -s
} #End of run_cmds
#####################################################################
# Bginning of main script
#####################################################################
if test $# -ne 1
then
	$CTMSG forcerpoffline $CMD_CAT IMsgforcerpofflineUsage 
	exit 5
fi
case $1 in
    -h)
	$CTMSG forcerpoffline $CMD_CAT IMsgforcerpofflineUsage 
	exit 0
	;;
    -*)
	$CTMSG forcerpoffline $CMD_CAT EMsgforcerpofflineBadFlag $cmdName $1
	exit 3
	;;
    *)
	;;
esac
domainName=$1

#Exit if domain name specified is IW
if [[ $domainName = "IW" ]]
then
       $CTMSG forcerpoffline $CMD_CAT EMsgforcerpofflineInvalidDomain $cmdName $1
       exit 4
fi

#Verify that the cluster configuration files exist
if test ! -f /var/ct/cfg/default_cluster -o ! -f /var/ct/cfg/current_cluster -o ! -f /var/ct/cfg/clusters
then
	$CTMSG forcerpoffline $CMD_CAT EMsgforcerpofflineConfigFileError $cmdName
	exit 5
fi

defaultDomain=`cat /var/ct/cfg/default_cluster`
currentDomain=`cat /var/ct/cfg/current_cluster`

#Check if the domain name specified is valid
domainId=`grep " ${domainName}$" /var/ct/cfg/clusters | cut -f 1 -d' '`

if test -z "$domainId"
then 
	$CTMSG forcerpoffline $CMD_CAT EMsgforcerpofflineInvalidDomain $cmdName $domainName
	exit 5
fi

#In an offline/online process, the /var/ct/cfg/default_cluster
#file is updated first which leaves the possibility that the
#/var/ct/cfg/current_cluster file may not be updated yet.
if test "$defaultDomain" = "$domainId"
then 
	run_cmds
	exit 0
elif test "$currentDomain" = "$domainId"
then 
	run_cmds
	exit 0
else
        $CTMSG forcerpoffline $CMD_CAT EMsgforcerpofflineNotDomain $cmdName $domainName
        exit 5
fi
