#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/lib/ksh93/db2/KLIB_DB2_get_tablespace_paths.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 
# @(#)92	1.6 src/43haes/lib/ksh93/db2/KLIB_DB2_get_tablespace_paths.sh, hacmp, 61haes_r720, 1539B_hacmp720 9/11/15 14:28:00
#
#=head1 NAME
#
# KLIB_DB2_get_tablespace_paths - Get the list of paths for the specified
#                                 DB2 database
#
#=head1 SYNOPSIS
#
# . /usr/es/sbin/cluster/sa/db2/etc/db2.disc
# paths=$(KLIB_DB2_get_tablespace_paths "db2inst1" "SAMPLE")
# for data in $paths; do
#	id=${data/:*/}
#	path=${data/*:/}
# 	echo "id=$id, path=$path"
# done
#
#=head1 DESCRIPTION
#
# Get the tablespace identifiers and paths associated with each of those
# identifiers
#
#=head1 ARGUMENTS
#
# 	1: [scalar] user name for db2 instance owner
#	2: [scalar] DB2 database name
#
#=head1 RETURN
#
# echo to stdout the id:path of each of the tablespaces associated
# with the database
#
#=head1 COPYRIGHT
#
#(C) COPYRIGHT International Business Machines Corp. 2005
#All Rights Reserved
#
#=cut
#
function KLIB_DB2_get_tablespace_paths
{
	. /usr/es/lib/ksh93/func_include

	typeset user=$1
	typeset db=$2
	typeset tmpfile="/tmp/db2_""$db""_$$"
	typeset ts_path
	typeset name
	typeset value

	typeset userhome=$(lsuser -a home $user)
	userhome=${userhome/*=/}
		
	echo "CONNECT TO $db" >> $tmpfile

	for tablespace_id in $(KLIB_DB2_get_tablespace_ids $user $db); do
		echo "LIST TABLESPACE CONTAINERS FOR $tablespace_id SHOW DETAIL" >> $tmpfile
	done
	echo "TERMINATE" >> $tmpfile
	chmod gou+r $tmpfile
	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 -f $tmpfile" | while IFS='=' read name value; do
                        name=${name//[[:space:]]/}
                        if [[ "$name" == "Name" ]]; then
                                value=${value//[[:space:]]/}
                                ts_path=$value
                        fi
                        if [[ "$name" == "Type" ]]; then
                                value=${value//[[:space:]]/}
                                echo $value:$ts_path
                                ts_path=
                        fi
                done
        elif [[ "$DB2_SHELL_ENV" == "csh" ]]; then
                /usr/bin/su $user -c "source $userhome/sqllib/db2cshrc && db2 -f $tmpfile" | while IFS='=' read name value; do
                        name=${name//[[:space:]]/}
                        if [[ "$name" == "Name" ]]; then
                                value=${value//[[:space:]]/}
                                ts_path=$value
                        fi
                        if [[ "$name" == "Type" ]]; then
                                value=${value//[[:space:]]/}
                                echo $value:$ts_path
                                ts_path=
                        fi
                done
        fi

        rm -f $tmpfile
}
