#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/lib/ksh93/db2/KLIB_DB2_get_dbm_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 
# @(#)83	1.6 src/43haes/lib/ksh93/db2/KLIB_DB2_get_dbm_cfg.sh, hacmp, 61haes_r720, 1539B_hacmp720 9/11/15 14:08:33
#
#=head1 NAME
#
# KLIB_DB2_get_dbm_cfg - Get the database manager configuration,
#                        store in a hash table
#
#=head1 SYNOPSIS
#
# unset DBM_CFG
# typeset -A DBM_CFG
# KLIB_DB2_get_dbm_cfg "db2inst1" DBM_CFG
#
#=head1 DESCRIPTION
#
# This function obtains the DB2 configuration by running db2 get dbm cfg,
# which will obtain information regarding the instance configuration.
# This command must su to the instance owner user to obtain the needed
# information, and will source the db2profile environment variable script 
#
#=head1 ARGUMENTS
#
# 	1: [scalar] instance owner (user)
#	2: [by ref] hash array of DBM configuration
#
#=head1 RETURN
#
# None, output is provided by the pass by ref hash table 2nd argument 
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_get_dbm_cfg
{
	. /usr/es/lib/ksh93/func_include

        typeset user=$1
        typeset -n config=$2
	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 get dbm cfg 2>/dev/null" | \
                while IFS='=' read name value
                do
                        name=${name/ /}
                        value=${value/ /}
                        [[ -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 get dbm cfg 2>/dev/null" | \
                while IFS='=' read name value
                do
                        name=${name/ /}
                        value=${value/ /}
                        [[ -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
}
