#!/usr/bin/ksh
# whattablespacesegment ()
#------------------------------------------------------------------------------#
#    Module Name:  whattablespacesegment                                       #
#                                                                              #
#    Purpose:      Report on the Tablespace-Segments / Space-Used.             #
#                                                                              #
#    Maintenance                                                               #
#    Date          Author        Description                                   #
#    -----------   ------------  --------------------------------------------  #
#    10-Oct-2009   T.Robinette   Module design/creation                        #
#                  F.Bender      Module design/creation                        #
#                  A.Estrada     Module design/creation                        #
#                  M.Barone      Module design/creation                        #
#------------------------------------------------------------------------------#
{
        DIRNAM=$(dirname $0)
        ORASID=$(echo $CONTEXT_NAME | cut -f1 -d'_')
        HNAME=$(uname -n)
                
        clear
        echo
        echo  " #--------------------------------------------------------#"
        echo  " # whattablespacesegment:  Oracle Apps Database Segments  #"
        echo  " #--------------------------------------------------------#"
        echo

        #------------------------------------------------------------------#
        #  Trap:  Set CNTL-C CNTL-D CNTL-\ Before Password Protection      #
        #------------------------------------------------------------------#
        trap        "stty echo; return"     2
        trap        "stty echo; return"     3
        stty -echo

        read PASSWD?" Please Enter the Oracle APPS Passwd: " 
            
        stty echo
        echo
        #------------------------------------------------------------------#
        #  Trap:  Reset CNTL-C CNTL-D CNTL-\ After Password Protection     #
        #------------------------------------------------------------------#
        trap        2
        trap        3

        read TBSPCE?"        Enter Tablespace-Name Ex: APPS_UNDOTS1: "

        clear
        ORANAM=${ORASID}

#------------------------------------------------------------------------------#
#  SQL*Plus ("Here-Document")                                                  #
#------------------------------------------------------------------------------#
sqlplus  -s  /nolog  <<-END_FILE

        CONNECT  APPS/$PASSWD

        clear   BREAKS
        clear   BUFFER
        clear   COLUMNS
        clear   COMPUTES
        clear   SCREEN
        clear   SQL
        clear   TIMING

        set     feedback        off
        set     flush           on
        set     heading         on
        set     pause           off
        set     space           1
        set     termout         on
        set     verify          on
        set     linesize        80
        set     pagesize        999

        SPOOL /tmp/whattablespacesegment_$$.txt

        COLUMN  datevalue       NOPRINT NEW_VALUE       DATEVAR
        COLUMN  timevalue       NOPRINT NEW_VALUE       TIMEVAR

        COLUMN  c_stop       NOPRINT 
        COLUMN  c_time       NOPRINT
        BREAK   ON  REPORT
        COMPUTE SUM LABEL 'Total'  OF  bytes    ON segment_name
        COMPUTE SUM LABEL 'Total'  OF  bytes    ON REPORT

        COLUMN  tablespace_name         FORMAT A15
        COLUMN  owner                   FORMAT A8
        COLUMN  segment_name            FORMAT A26
        COLUMN  segment_type            FORMAT A12
        COLUMN  bytes                   FORMAT 99,999,999,999

        TTITLE  CENTER  'Oracle Tablespace Segment Details'             -
                LEFT    DATEVAR                                         -
                RIGHT   'Page:'         FORMAT 999 SQL.pno      skip 1  -
                CENTER  'whattablespacesegment -- Sorted by Owner'      -
                LEFT    TIMEVAR                                 skip 2 

        SELECT  TO_CHAR(sysdate, 'DD-MON-YY')   datevalue,
                TO_CHAR(sysdate, 'HH24:MI:SS')  timevalue,
                tablespace_name, owner,
                segment_name,    segment_type,
                bytes
         FROM   dba_segments
        WHERE   tablespace_name =  UPPER('${TBSPCE}')
        ORDER   BY tablespace_name, owner, segment_name, segment_type, bytes;

EXIT;

END_FILE
print
print "Please see /tmp/whattablespacesegment_$$.txt"
print
}