#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/lib/ksh93/db2/KLIB_DB2_get_instance_databases.sh 1.9 
#  
# 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 
# @(#)84	1.9 src/43haes/lib/ksh93/db2/KLIB_DB2_get_instance_databases.sh, hacmp, 61haes_r720, 1539B_hacmp720 9/11/15 14:16:42
#
#=head1 NAME
#
# KLIB_DB2_get_instance_databases - List all of the databases accessible
#                                   for the specified DB2 instance
#
#=head1 SYNOPSIS
#
# . /usr/es/sbin/cluster/sa/db2/etc/db2.disc
# databases=$(KLIB_DB2_get_instance_databases "db2inst1")
#
#=head1 DESCRIPTION
#
# This function connects to the DB2 instance and queries for a list of all
# configured DB2 instances by running "db2 list database directory"
#
#=head1 ARGUMENTS
#
# 	1: [scalar] name of the DB2 instance
#
#=head1 RETURN
#
# None
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_get_instance_databases
{
	. /usr/es/lib/ksh93/func_include

        instance_owner=$1

	DB2_SHELL_ENV=$(lsuser -a shell $instance_owner | 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"
	}
	[[ "$DB2_SHELL_ENV" == "ksh" ]] && {
        	/usr/bin/su - $instance_owner -c "LC_ALL=C db2 list database directory" | while IFS='=' read name value; do
                	name=$(eval echo $name)
                	value=$(eval echo $value)
                	if [[ "$name" == "Database name" ]]; then
                        	databases="$value $databases"
                	fi
        	done
	}
	[[ "$DB2_SHELL_ENV" == "csh" ]] && {
		/usr/bin/su - $instance_owner -c "set LC_ALL=C; db2 list database directory" | while IFS='=' read name value; do
			name=$(eval echo $name)
                        value=$(eval echo $value)
                        if [[ "$name" == "Database name" ]]; then
                                databases="$value $databases"
                        fi
		done
	}
        echo $databases
}
