#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/lib/ksh93/db2/KLIB_DB2_get_tablespace_ids.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 
# @(#)91	1.6 src/43haes/lib/ksh93/db2/KLIB_DB2_get_tablespace_ids.sh, hacmp, 61haes_r720, 1539B_hacmp720 9/11/15 14:18:53
#
#=head1 NAME
#
# KLIB_DB2_get_tablespace_ids - Get a list of the tablespace IDs for the
#                               specified DB2 datbase
#
#=head1 SYNOPSIS
#
# . /usr/es/sbin/cluster/sa/db2/etc/db2.disc
# ids=$(KLIB_DB2_get_tablespace_ids "db2inst1" "SAMPLE")
#
#=head1 DESCRIPTION
#
# Obtains a list of the tablespace identifiers. This function runs the
# command db2 list tablespaces and records the list of ids that are
# accessible at the time this function is invoked.
#
#=head1 ARGUMENTS
#
# 	1: [scalar] DB2 instance owner (user)
#	2: [scalar] DB2 database name 
#
#=head1 RETURN
#
# 	echos the list of identifers to stdout
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_get_tablespace_ids
{
	. /usr/es/lib/ksh93/func_include

	typeset user=$1
	typeset db=$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 connect to $db && db2 list tablespaces && db2 terminate" |
                        while IFS='=' read name value; do
                                name=${name//[[:space:]]/}
                                if [[ "$name" == "TablespaceID" ]]; then
                                        echo $value
                                fi
                        done
        elif [[ "$DB2_SHELL_ENV" == "csh" ]]; then
                /usr/bin/su $user -c "source $userhome/sqllib/db2cshrc && db2 connect to $db && db2 list tablespaces && db2 terminate" |
                        while IFS='=' read name value; do
                                name=${name//[[:space:]]/}
                                if [[ "$name" == "TablespaceID" ]]; then
                                        echo $value
                                fi
                        done
        fi
}
