#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/sa/KLIB_SA_get_metadata.sh 1.7 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2015 
# 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 
# @(#)20	1.7 src/43haes/lib/ksh93/sa/KLIB_SA_get_metadata.sh, hacmp, 61haes_r714, 1502A_hacmp714 1/6/15 05:06:30
#
#=head1 NAME
#
# KLIB_SA_get_metadata - Get the value for the specified SA application_id
#
#=head1 SYNOPSIS
#
# typeset application_id="SA_DB2Instance"
# typeset sa_name="FILESYSTEM"
# typeset sa_key="db2inst1"
# value=$(KLIB_SA_get_metadata $sa_type $sa_name $sa_value)
#
#=head1 DESCRIPTION
#
# Get metadata from the smart assistant metadata ODM, this function
# will get the value field with the same application_id and name
#
#=head1 ARGUMENTS
#
#   1: [scalar] application_id, the unique key to identify the smart assistant amongst its peer type
#   2: [scalar] sa_name, the smart assistant name, as part of the name / value pair
#
#=head1 RETURN
#
#       0 - success
#       1 - failed (not found)
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_SA_get_metadata
{
	CL=$LINENO . /usr/es/lib/ksh93/func_include

	application_id=$1
	shift
	name=$1

	if [[ -z $name || -z $application_id ]]; then
		return 1
	fi
	odmget -q "application_id = $application_id and name = $name" HACMPsa_metadata | while IFS='=' read f_name f_value; do
		f_name=$(eval echo $f_name)
		if [[ "$f_name" == "value" ]]; then
			f_value=$(echo $f_value | sed -e "s/\"//g")
			echo $f_value
			return 0
		fi
	done 2>/dev/null
	return 1
}