#!/bin/sh
# BEGIN_ICS_COPYRIGHT8 ****************************************
# 
# Copyright (c) 2015, Intel Corporation
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#     * Redistributions of source code must retain the above copyright notice,
#       this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Intel Corporation nor the names of its contributors
#       may be used to endorse or promote products derived from this software
#       without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# END_ICS_COPYRIGHT8   ****************************************

# [ICS VERSION STRING: @(#) ./fm_cmdall 10_0_0_0_696 [10/08/16 10:52]

CONFIG_DIR=/etc/sysconfig
CONFIG_FILE=$CONFIG_DIR/opafm.xml
IFS_FM_BASE=/opt/opafm # default
if [ -s $CONFIG_DIR/opa/opafm.info ]
then
	# get IFS_FM_BASE
	. $CONFIG_DIR/opa/opafm.info
else
	echo "Error: $CONFIG_DIR/opa/opafm.info not found: using $IFS_FM_BASE" >&2
fi


PROGNAME="$0"

Usage() {
	echo "Usage: $PROGNAME [-i fm_instance] cmd [args]" >&2
	echo "    fm_instance - instance number (0-7) to act on" >&2
	echo "                  specify multiple -i options as needed" >&2
	echo "                  default is all instances" >&2
	exit 2
}

die () {
	echo "$@" > /dev/stderr

	echo "removing $dumpLocation"
	rm -rf $dumpLocation
	exit 1
}

INSTANCES=
while getopts i: param
do
	case $param in
	i)	INSTANCES="$INSTANCES $OPTARG";;
	?) Usage;;
	esac
done
shift $((OPTIND -1))
if [ $# -le 0 ]
then
	echo "$0: Error: Must specify a command" >&2
	Usage
fi
if [ -z "$INSTANCES" ]
then
	INSTANCES="0 1 2 3 4 5 6 7"
	default=y
else
	default=n
fi

running_manager()
# $1 = instance number
{
	pgrep -f "(fe|sm) (-D )?-e (fe|sm)_$1" > /dev/null 2>&1
}

found_one=n
for i in $INSTANCES
do
	if running_manager $i
	then
		found_one=y
		$IFS_FM_BASE/bin/fm_cmd -i $i "$@"
		if [ $? -eq 255 ]
		then
			break	# 255 is used as a Usage return, no use iterating
		fi
	else
		if [ "$default" != "y" ]
		then
			echo "Skipping FM instance $i: Not Running"
		fi
	fi
done

if [ "$found_one" = n -a "$default" = y ]
then
	echo "No running FM instances found"
fi
