#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/clchres.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009 
# 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 
# @(#)42  1.1  src/43haes/usr/sbin/cluster/utilities/clchres.sh, hacmp.utils, 61haes_r714 4/15/09 12:10:19

###############################################################################
#
#  Name:  clchres
#
#	Utilitiy for updating resources in a resource group.
#
#  Arguments
#	-a | -d - add or delete flag - one is required
#
#	-g resource group name (required)
#
#	$* - one or more "type=value" pairs in the format used for the
#		HACMPresource odm, e.g. SERVICE_LABEL=fake_svc_1
#
#  Returns
#	-1	Missing required parameter(s)
#	0	Success
#	1	Failure updating the configuration
#
#
###############################################################################

###############################################################################
#
#  Name:  usage
#
#  This routine displays the usage message for the clchres utility,
#  then exits.
#
#  Arguments:    none
#  Usage:        usage
#  Returns:      -1 - exit with invalid input
#  Environment:
#
###############################################################################
usage()
{
    typeset PS4_FUNC="usage"
    [[ "${VERBOSE_LOGGING:-}" == "high" ]] && set -x
    dspmsg scripts.cat 4670 "Usage: \n\
        $PROGNAME -a | -d "add" or "delete" the specified resource\n\
        $PROGNAME -g <resource group name> \n\
        $PROGNAME [NAME=VALUE] one or more pairs of names and values to add or delete \n\
\n" $PROGNAME $PROGNAME $PROGNAME 
    exit -1

}

###############################################################################
#
#  Name:  config_changed
#
#  This routine clears the handle field in HACMPcluster to indicate a
#  configuration change has been made then exits.
#
#  Arguments:    none
#  
#  Returns:      0
#  Environment:
#
###############################################################################
config_changed()
{
    typeset PS4_FUNC="config_changed"
    [[ "${VERBOSE_LOGGING:-}" == "high" ]] && set -x

    echo "HACMPcluster:" >/tmp/clchres.$$
    echo "	handle = 0" >>/tmp/clchres.$$
    odmchange -o HACMPcluster /tmp/clchres.$$ >/tmp/clchres.error.$$ 2>&1
    RC=$?
    if [[ $RC != 0 ]]
    then
	    echo "$PROGNAME: odmchange failed with code $RC"
	    echo "Error information from odmchange command:"
	    cat /tmp/clchres.error.$$
	    rm -f /tmp/clchres.error.$$
	    rm -f /tmp/clchres.$$
	    exit 1
    fi
}


###############################################################################
#
# main starts here
#

###############################################################################
#
# main starts here
#
###############################################################################

PROGNAME=$(basename ${0})
export PATH="$($(dirname ${0})/cl_get_path all)"
[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
[[ "$VERBOSE_LOGGING" == "high" ]] && version='1.1'
HA_DIR="$(cl_get_path)"

# Initialize variables
ADD_DELETE="NEITHER"
GROUP="NOGROUP"
CONFIG_CHANGED="FALSE"

# at least 5 params required
if [ -z "$4" ]
then
    usage 
fi

# set the platform
UNAME=$(uname -m)
case $UNAME in
    ppc* | i* )
        export PLATFORM="__LINUX__"
        ;;
    * )
        export PLATFORM="__AIX__"
        ;;
esac

# Parse the command line.
while [ $# != 0 ]; do
    case $1 in

	-a)	ADD_DELETE="ADD"
		shift;
		;;

	-d)	ADD_DELETE="DELETE"
		shift;
		;;

	-g)	GROUP=$2
		shift;
		shift;
		;;

	-*)	# these are the only recognized flags - if the input has
		# "-" its not valid
		usage
		;;

	* )	# done parsing flags - break
		break
		;;

    esac
done

#
# Do we have all the required inputs ?
#
if [[ $ADD_DELETE == "NEITHER" || $GROUP == "NOGROUP" || $# < 1 ]]
then
    usage
fi

case $ADD_DELETE in
    ADD )
	#
	# Create a file to odmadd all stanzas
	#
	while [ $# != 0 ]; do
	    #
	    # Note there could be checking added here to make sure
	    # the name is a valid keyword for HACMPresource
	    #
	    NAME=$(echo $1 | cut -f1 -d\=)
	    VALUE=$(echo $1 | cut -f2 -d\=)
	    if [[ -z "$NAME" || -z "$VALUE" ]]
	    then
		[[ $CONFIG_CHANGED == "TRUE" ]] && config_changed
		usage
	    fi

	    echo "# Temporary work file created by $PROGNAME on $(date)" >/tmp/clchres.$$
	    echo "HACMPresource:" >>/tmp/clchres.$$
	    echo "	group = $GROUP" >>/tmp/clchres.$$
	    echo "	name = $NAME" >>/tmp/clchres.$$
	    echo "	value = $VALUE" >>/tmp/clchres.$$
	    echo "	id = 0" >>/tmp/clchres.$$
	    echo "	monitor_method = \"\"" >>/tmp/clchres.$$
	    
	    odmadd /tmp/clchres.$$ >/tmp/clchres.error.$$ 2>&1
	    RC=$?
	    if [[ $RC != 0 ]]
	    then
	        echo "$PROGNAME: odmadd failed with code $RC"
	        echo "Error information from odmadd command:"
	        cat /tmp/clchres.error.$$
	        rm -f /tmp/clchres.error.$$
	        rm -f /tmp/clchres.$$
		[[ $CONFIG_CHANGED == "TRUE" ]] && config_changed
	        exit 1
	    fi
	    CONFIG_CHANGED="TRUE"
	    shift
	done
	rm -f /tmp/clchres.$$
	;;
    DELETE )
	#
	# odmdelete each entry in turn
	#
	while [ $# != 0 ]; do
	    #
	    # Note that odmdelete does not spit an error if the stanza
	    # does not exist - could add checking here to error out if the
	    # caller specified an entry that does not exit
	    #
	    NAME=$(echo $1 | cut -f1 -d\=)
	    VALUE=$(echo $1 | cut -f2 -d\=)
	    if [[ -z "$NAME" || -z "$VALUE" ]]
	    then
		[[ $CONFIG_CHANGED == "TRUE" ]] && config_changed
		usage
	    fi

	    odmdelete -o HACMPresource -q"group = $GROUP AND name = $NAME and value = $VALUE" >/tmp/clchres.error.$$ 2>&1
            RC=$?
            if [[ $RC != 0 ]]
            then
                echo "$PROGNAME: odmdelete failed with code $RC"
                echo "Error information from odmdelete command:"
                cat /tmp/clchres.error.$$
                rm -f /tmp/clchres.error.$$
		[[ $CONFIG_CHANGED == "TRUE" ]] && config_changed
            	exit 1
            fi
	    CONFIG_CHANGED="TRUE"
	    shift
	done
	;;
esac

#
# If any of the above commands was sucesful, mark the config as having changed
#
[[ $CONFIG_CHANGED == "TRUE" ]] && config_changed

exit 0
