#
# $Header: rdbms/admin/catctl.pm
#
# catctl.pm
#
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      catctl.pm - Shared routines with autoupgrade.
#
#    DESCRIPTION
#
#      Shared routines that are needed by both catctl.pl and
#      the autoupgrade project.
#
#    NOTES
#    
#
#    MODIFIED   (MM/DD/YY)
#    jerrede     04/19/18 - Creation
#

package catctl;

use strict;
use warnings;
use English;

require Exporter;

#
# Export shared routines
#
our @ISA = qw( Exporter );
our @EXPORT = qw(catctlInsertSummaryRpt);


######################################################################
# 
# catctlInsertSummaryRpt - Insert summary report into the catupgrd0.log.
#
# Description:  Insert Summary report into the catupgrd0.log file and
#               calculate the times.
#
# Parameters:
#
#   INPUT -
#    $LogFile - Log file to insert into
#    $ReportName - Summary report name
#    $LineTag - Line Deliminator
#    $bCdbDatabase - True is a CDB or False is not a CDB
#    TotalTag - Total tag identifier
#    TotalTime - Total Upgrade Time in seconds
#    DisplayTag - Display tag
#
# Returns: 
#
######################################################################
sub catctlInsertSummaryRpt
{
    my $LogFile      = $_[0];       # Log File
    my $ReportName   = $_[1];       # Report Name
    my $LineTag      = $_[2];       # Line Tag
    my $bCdbDatabase = $_[3];       # Is Cdb Database
    my $TotalTag     = $_[4];       # Total tag
    my $TotalTime    = $_[5];       # Total Time in seconds
    my $DisplayTag   = $_[6];       # Display tag
    my $Days  = int($TotalTime/(24*60*60));
    my $Hours = ($TotalTime/(60*60))%24;
    my $Mins  = ($TotalTime/60)%60;
    my $Sec   = $TotalTime%60;
    my $DisplayTime;
    my $idx1 = 0;
    my $counter = 0;
    my @GrandTotals;
    my @SortedTotals;
    my $Line = "";

    #
    # Put together the display for the user
    #
    $DisplayTime = $DisplayTag.$Days."d:".
        $Hours."h:".$Mins."m:".$Sec."s]";
    #
    # Display Total Time in human readable format
    #
    print STDERR "\n$DisplayTime\n";

    open (FileOut, '>>', $LogFile);
    open (FileIn, '<', $ReportName);
    print FileOut "\nStart of Summary Report\n$LineTag\n";
    while(<FileIn>)
    {
        #
        # Store the line
        #
        $Line = $_;
        chomp($Line);

        #
        # Combine Grand Totals for a CDB database
        #
        if ($bCdbDatabase)
        {
            $idx1 = index($Line, $TotalTag);
            if ($idx1 != -1)
            {
                $counter++;
                push(@GrandTotals, $Line."\n");
            }
        }

        #
        # Write the line to log file
        #
        print FileOut "$Line\n";

    }

    #
    # Write combined grand totals at the end catupgrd0.log
    #
    if (($bCdbDatabase) && ($counter > 1))
    {
        # sort by time at position 21 (note the 20) for length of 8
        # sort in reverse (descending) order, hence the b before a in
        # sort{ cmp } statement
        @SortedTotals = map{$_->[0]}
        sort {$b->[1] cmp $a->[1]}
        map {[$_,substr($_,20,8)]} @GrandTotals;

        print FileOut "\nUpgrade Times Sorted In Descending Order\n\n";
        print FileOut @SortedTotals;
    }

    #
    # Write Total Time to catupgrd*0 log file
    #
    print FileOut "$DisplayTime\n";
    print FileOut "\nEnd of Summary Report\n$LineTag\n";

    #
    # Close Files
    #
    close (FileIn);
    close (FileOut);

    #
    # Open Summary Report file
    #
    open (FileOut, '>>', $ReportName);

    #
    # Write combined grand totals at the end of summary report
    #
    if (($bCdbDatabase) && ($counter > 1))
    {
        print FileOut "\nUpgrade Times Sorted In Descending Order\n\n";
        print FileOut @SortedTotals;
    }

    #
    # Write Total Time to upgrade summary report file
    #
    print FileOut "$DisplayTime\n";
    close(FileOut);

} # End sub catctlInsertSummaryRpt

1;
