#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/lib/ksh93/db2/KLIB_DB2_get_db_cfg.sh 1.6 
#  
# 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 
# @(#)77	1.6 src/43haes/lib/ksh93/db2/KLIB_DB2_get_db_cfg.sh, hacmp, 61haes_r720, 1539B_hacmp720 9/11/15 13:59:25
#
#=head1 NAME
#
# KLIB_DB2_get_db_cfg - Get the database configuration for the specified instance/db
#
#=head1 SYNOPSIS
#
# . /usr/es/sbin/cluster/sa/db2/etc/db2.disc
# typeset -A DB_CFG
# KLIB_DB2_get_db_cfg "SAMPLE" "db2inst1" DB_CFG || {
#	echo "FAILED to get SAMPLE database configuration."
# }
#
#=head1 DESCRIPTION
#
# Get the database configuration for the specified instance / database by
# running db2 get db cfg while connected to the database.  The instance must be
# running, and the database must be accessible at the time this command runs.
# If neither of the above are true, then the DB configuration will not be
# obtained for this instance.
#
# This function will su to the db2 instance owner to run the db2 command
# and will connect to the DB2 instance to obtain the db configuration.
#
#=head1 ARGUMENTS
#
# 	1: [scalar] name of the database to obtain configuration info on
#	2: [scalar] name of the DB2 instance owner (user) to su to in order
#	            to connect to the database
#	3: [scalar] pass by reference, reference to hash table that will
#               contain the name / value pairs returned from running
#               db2 db get cfg
#
#=head1 RETURN
#
# None
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_get_db_cfg
{
	. /usr/es/lib/ksh93/func_include

        typeset db=$1
        typeset user=$2
        typeset -n config=$3
        typeset userhome=$(lsuser -a home $user)
        userhome=${userhome/*=/}
	DB2_SHELL_ENV=$(lsuser -a shell $user | awk -F = '{print $2}')
	(echo $DB2_SHELL_ENV | grep csh > /dev/null) &&
		DB2_SHELL_ENV="csh"
	(echo $DB2_SHELL_ENV | grep ksh > /dev/null) &&
		DB2_SHELL_ENV="ksh"
	if [[ "$DB2_SHELL_ENV" == "ksh" ]]; then
		/usr/bin/su $user -c ". $userhome/sqllib/db2profile && db2 connect to $db && db2 get db cfg && db2 terminate 2>/dev/null" | \
		while IFS='=' read name value
		do
			name=${name//[[:space:]]/}
			value=${value//[[:space:]]/}
			if [[ "$name" == "Pathtologfiles" ]]; then
				name=" (LOGPATH) "
			fi
			[[ -n "$name" && -n "$value" ]] && {
				if [[ $name == *\([[:alnum:]]*\)* ]]; then
					short_name=${name/*\(/}
					short_name=${short_name/\)/}
					short_name=${short_name// /}
					[[ "$short_name" ]] && config["$short_name"]="$value"
				fi
			}
		done
	elif [[ "$DB2_SHELL_ENV" == "csh" ]]; then
		/usr/bin/su $user -c "source $userhome/sqllib/db2cshrc && db2 connect to $db && db2 get db cfg && db2 terminate 2>/dev/null" | \
		while IFS='=' read name value
		do
			name=${name//[[:space:]]/}
			value=${value//[[:space:]]/}
			if [[ "$name" == "Pathtologfiles" ]]; then
				name=" (LOGPATH) "
			fi
			[[ -n "$name" && -n "$value" ]] && {
				if [[ $name == *\([[:alnum:]]*\)* ]]; then
					short_name=${name/*\(/}
					short_name=${short_name/\)/}
					short_name=${short_name// /}
					[[ "$short_name" ]] && config["$short_name"]="$value"
				fi
			}
		done
	fi
}
