#!/bin/sh
#
# Licensed Materials - Property of IBM
#
# 5724O4800
#
# (C) Copyright IBM Corp. 2012. All Rights Reserved
#
# US Government Users Restricted Rights - Use, duplication
# or disclosure restricted by GSA ADP Schedule Contract
# with IBM Corp.
#
# Set a property in a probe using http interface.
#
# 5.50.78
#

#######################################
# Find nco_common
NCO_COMMON=`dirname $0`/../bin/nco_common

# Check for nco_common, and load if found
if [ ! -f "$NCO_COMMON" ]; then
	echo "Cannot find nco_common" 1>&2
	exit 1
fi

. $NCO_COMMON
		
# Set platform variables
f_SET_PLATFORM_VARIABLES

help() {
	echo "usage: `basename $1` [options]"
	echo
	echo "where options can be:"
	echo
	echo "	-help		Print this help text"
	echo "	-host		Hostname of probe"
	echo "	-messagelevel	Message logging level (default: INFO)"
	echo "	-name		Name of property to change or add"
	echo "	-password	Password to use for http authentication"
	echo "	-port		Port number of probe"
	echo "	-ssl		Use https rather than http"
	echo "	-timeout	Timeout for http response"
	echo "	-username	Username to use for http authentication"
	echo "	-value		New value of property"
	echo
	exit 0
}

OMNIUSERNAME=""
OMNIPASSWORD=""
OMNISSL=0
OMNIHOST=""
OMNIPORT=""
OMNINAME=""
OMNIVALUE=""
OMNILEVEL="info"
OMNITIMEOUT="20"

while [ $# -gt 0 ]; do
	case "$1" in
	-ssl)
		OMNISSL=1
		;;
	-messagelevel)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift	
		OMNILEVEL="$1"
		;;
	-username)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift	
		OMNIUSERNAME="$1"
		;;
	-password)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift	
		OMNIPASSWORD="$1"
		;;
	-host)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift
		OMNIHOST="$1"
		;;
	-port)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift
		OMNIPORT="$1"
		;;
	-name)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift
		OMNINAME="$1"
		;;
	-timeout)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift
		OMNITIMEOUT="$1"
		;;
	-value)
		if [ $# -lt 2 ]; then
			err "$1 option requires an argument"
		fi
		shift
		OMNIVALUE="$1"
		;;
	-he*|-H|-?)
		help $0 2>&1
		;;
	*)
		err "unknown option $1"
	esac
	shift
done

if [ "$OMNIHOST" = "" ]; then
	err "-host is required"
fi

if [ "$OMNINAME" = "" ]; then
	err "-name is required"
fi

if [ "$OMNISSL" -ne 0 ]; then
	PROTO=https
else
	PROTO=http
fi

if [ "$OMNIPORT" != "" ]; then
	PORT=":${OMNIPORT}"
fi

URI=${PROTO}://${OMNIHOST}${PORT}/probe/common
DATA="{ \"properties\": { \"${OMNINAME}\": \"${OMNIVALUE}\" } }"

if [ "${OMNIUSERNAME}" != "" ]; then
exec $NCHOME/omnibus/bin/nco_http -datatype application/json -data "${DATA}" \
	-method PATCH -uri "${URI}" -messagelevel "${OMNILEVEL}" \
	-username "${OMNIUSERNAME}" -password "${OMNIPASSWORD}" \
	-timeout "${OMNITIMEOUT}"
else
exec $NCHOME/omnibus/bin/nco_http -datatype application/json -data "${DATA}" \
	-method PATCH -uri "${URI}" -messagelevel "${OMNILEVEL}" \
	-timeout "${OMNITIMEOUT}"
fi
