#! 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   : Locate ALL build.pl files in a given release
#                 Convert the build.pl files to a new project
#                 Create a new branch for the build file
#                 Check the new file in
#
# Usage:          Convert 'sydney' build files to 'beijing' build files
#
# Version   Who      Date        Description
#
#......................................................................#

require 5.006_001;
use strict;
use warnings;
use JatsError;
use Pod::Usage;                             # required for help support
use Getopt::Long;

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

my $GBE_PERL     = $ENV{'GBE_PERL'};        # Essential ENV variables
my $GBE_CORE     = $ENV{'GBE_CORE'};
my $VERSION = '1.0.0';
my $opt_help = 0;
my $opt_manual = 0;
my $opt_verbose = 1;
my $opt_rtagid;
my $opt_branch;
my $opt_old_project;
my $opt_newproject;
my $RM_DB;


my @GDATA;
sub getPkgDetailsByRTAG_ID
{
    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 pv.PV_ID, pkg.PKG_NAME, pv.PKG_VERSION, pv.PKG_LABEL, pv.SRC_PATH, pv.BUILD_TYPE" .
                    " FROM RELEASE_CONTENT rc, PACKAGE_VERSIONS pv, PACKAGES pkg" .
                    " WHERE rc.RTAG_ID = $RTAG_ID AND rc.PV_ID = pv.PV_ID AND pv.PKG_ID = pkg.PKG_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] || '';

#                    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~^/~  );
print "$row[5] --";
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'    =>'ConvertBuild' );


#
#   Extract options fromthe user command line
#
my $result = GetOptions (
                "help+"         => \$opt_help,              # flag, multiple use allowed
                "manual"        => \$opt_manual,            # flag, multiple use allowed
                "verbose+"      => \$opt_verbose,           # flag, multiple use allowed
                "rtagid=s"      => \$opt_rtagid,
                "branch=s"      => \$opt_branch,
                "oldproject=s"  => \$opt_old_project,
                "newproject=s"  => \$opt_newproject,
                );

                #
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
                #

#
#   Process help and manual options
#
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
pod2usage(-verbose => 1)  if ($opt_help == 2 );
pod2usage(-verbose => 2)  if ($opt_manual || ($opt_help > 2));

#
#   Validate the user options
#
Error ("RTAGID must be provided (ie 2362)" ) unless ( $opt_rtagid );

getPkgDetailsByRTAG_ID($opt_rtagid);           # 2362 : Syd Release 1
#DebugDumpData("GDATA", \@GDATA);
exit;



#-------------------------------------------------------------------------------
#   Documentation
#

=pod

=head1 NAME

jats_convert_build - Convert a Releases build.pl files

=head1 SYNOPSIS

  jats jats_convert_build [options]

 Options:
    -help              - brief help message
    -help -help        - Detailed help message
    -man               - Full documentation
    -rtagid=tag        - RTAG ID of the view defining packages to process
    -branch=xxx        - Branch for new build.pl files
    -oldproject=xx     - Project name of base projects (syd)
    -newproject=xx     - Project name of new base ( bej )

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-help -help>

Print a detailed help message with an explanation for each option.

=item B<-man>

Prints the manual page and exits.

=back

=head1 DESCRIPTION

This program does stuff.


=cut

