#! perl
########################################################################
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
#
# Module name   : jats.sh
# Module type   : Makefile system
# Compiler(s)   : n/a
# Environment(s): jats
#
# Description   : Display the currently released version of ALL packages
#                 that are a part of a given release
#
#                 Currently hard coded to Sydney Release-1
#                 Currently will extract information on COTS packages
#
# Usage:
#
# Version   Who      Date        Description
#
#......................................................................#

require 5.006_001;
use strict;
use warnings;
use JatsError;
use JatsRmApi;

#use Data::Dumper;
use DBI;
use Cwd;

my $GBE_PERL     = $ENV{'GBE_PERL'};        # Essential ENV variables
my $GBE_CORE     = $ENV{'GBE_CORE'};
my $opt_verbose = 1;
my $RM_DB;
my @GDATA;

sub getPkgDetailsByRTAG_ID
{
    my ($RTAG_ID) = @_;
    my $foundDetails = 0;
    my (@row);
    my %dnr;

    # if we are not or cannot connect then return 0 as we have not found anything
    connectRM(\$RM_DB) unless ( $RM_DB );

    #
    #   Get do not ripple info
    #
    my $m_sqlstr1 = "SELECT dnr.PV_ID" .
                    " FROM DO_NOT_RIPPLE dnr" .
                    " WHERE dnr.RTAG_ID = $RTAG_ID";
    my $sth1 = $RM_DB->prepare($m_sqlstr1);
    if ( defined($sth1) )
    {
        if ( $sth1->execute( ) )
        {
            if ( $sth1->rows )
            {
                while ( @row = $sth1->fetchrow_array )
                {
                    $dnr{ $row[0] } = 1;
#print "Do not ripple: " . $row[0] . "\n";
                }
            }
            $sth1->finish();
        }
    }
    else
    {
        Error("Prepare failure" );
    }



    # First get details from pv_id

    my $m_sqlstr = "SELECT pv.PV_ID, pkg.PKG_NAME, pv.PKG_VERSION, pv.PKG_LABEL, pv.SRC_PATH, pv.BUILD_TYPE, pv.BS_ID, pbi.BSA_ID" .
                    " FROM RELEASE_CONTENT rc, PACKAGE_VERSIONS pv, PACKAGES pkg, PACKAGE_BUILD_INFO pbi" .
                    " WHERE rc.RTAG_ID = $RTAG_ID AND rc.PV_ID = pv.PV_ID AND pv.PKG_ID = pkg.PKG_ID AND pv.PV_ID = pbi.PV_ID(+)"
                    ;
    my $sth = $RM_DB->prepare($m_sqlstr);
    if ( defined($sth) )
    {
        if ( $sth->execute( ) )
        {
            if ( $sth->rows )
            {
                while ( @row = $sth->fetchrow_array )
                {
                    my %DATA;
                    $DATA{pv_id}            = $row[0];
                    my $name = $DATA{name}             = $row[1];
                    my $ver = $DATA{version}          = $row[2];
                    my $label =                     $row[3] || '';
                    my $path = $row[4] || '';
                    my $bt = $row[5] || '?';
                    my $bs = $row[6] || '?';
                    my $be = $row[7] || '?';

                    my $ripple = 'Y';
                    $ripple = 'N' if ( exists  $dnr{ $row[0] } );

#                    next if ( $ver =~ /syd$/i );
#                    next if ( $ver =~ /cr$/i );
#                    next if ( $ver =~ /mas$/i );
#                    next unless ( $ver =~ /cots$/i );

                    $path =~ tr~\\/~/~s;
#                    next if ( $path =~ m~^/~  );

                    my $buildable = 'Y';
                    $buildable = 'N' unless ( $label );
                    $buildable = 'N' unless ( $path =~ m~^/~);
                    $buildable = 'N' if ( $path =~ m~deployarchive~);
                    $buildable = 'N' if ( $path =~ m~dpkg_archive~);
                    $buildable = 'N' if ( $path =~ m~deploy_archive~);
                    $buildable = 'N' if ( $bs eq '?' || $bs == 3);

#print "Ripple:$ripple,";
#print "BuildType:$row[5],";
print "Buildable:$buildable --";
print "BS:$bs,$bt,$be --";
printf ( "%40s %15s %50s %s\n",  $name, $ver, $label, $path);
#printf ( "copy e:\\%s\\%s .\n",  $name, $ver, $label, $path);

                    push @GDATA, (\%DATA);
                }
            }
            $sth->finish();
        }
    }
    else
    {
        Error("Prepare failure" );
    }

    $RM_DB->disconnect() || Error ("Disconnect failed");


}

#-------------------------------------------------------------------------------
# Function        : getRtagId
#
# Description     : Given a release name, determine the RTAG_ID
#
# Inputs          :
#
# Returns         :
#
sub getRtagId
{
    my ($RTAG_ID) = @_;
    my $foundDetails = 0;
    my (@row);

    # if we are not or cannot connect then return 0 as we have not found anything
    connectRM(\$RM_DB) unless ( $RM_DB );

    # First get details from pv_id

    my $m_sqlstr = "SELECT rt.RTAG_ID, rt.RTAG_NAME, rt.DESCRIPTION, pj.PROJ_ID, pj.PROJ_NAME, rt.OFFICIAL FROM RELEASE_TAGS rt, PROJECTS pj WHERE rt.PROJ_ID = pj.PROJ_ID ORDER BY pj.PROJ_NAME";
    my $sth = $RM_DB->prepare($m_sqlstr);
    if ( defined($sth) )
    {
        if ( $sth->execute( ) )
        {
            if ( $sth->rows )
            {
                while ( @row = $sth->fetchrow_array )
                {
                    printf "%20s, %8s(%s), %40s\n", $row[4], $row[0], $row[5], $row[1];
                }
            }
            $sth->finish();
        }
    }
    else
    {
        Error("Prepare failure" );
    }

    $RM_DB->disconnect() || Error ("Disconnect failed");

    exit;
}

#-------------------------------------------------------------------------------
# Function        : Main
#
# Description     :
#
# Inputs          :
#
# Returns         :
#

ErrorConfig( 'name'    =>'PLAY7' );


#getRtagId();

#getPkgDetailsByRTAG_ID(5143);           # 2301 : Seattle I8
#getPkgDetailsByRTAG_ID(2301);           # 2301 : Seattle I7
getPkgDetailsByRTAG_ID(2362);           # 2362 : Syd Release 1
#getPkgDetailsByRTAG_ID(1861);           # 1861 : Syd Release Legacy
#getPkgDetailsByRTAG_ID(3462);           # 3462 : Beijing Release 1
#getPkgDetailsByRTAG_ID(2641);           # 2641 : NZS Phase-1

#DebugDumpData("GDATA", \@GDATA);
exit;


