########################################################################
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
#
# Module name   : jats_rm_play28.pl
# Module type   : Makefile system
# Compiler(s)   : n/a
# Environment(s): jats
#
# Description   : Extract packages with text in comment
#
#
#......................................................................#

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

use DBI;

my $VERSION = "1.2.3";                      # Update this
my $opt_verbose = 1;
my $opt_help = 0;
my $opt_manual;
my $RM_DB;

#-------------------------------------------------------------------------------
# Function        : Main Entry
#
# Description     :
#
# Inputs          :
#
# Returns         :
#
my $result = GetOptions (
                "help+"         => \$opt_help,          # flag, multiple use allowed
                "manual"        => \$opt_manual,        # flag
                "verbose+"      => \$opt_verbose,       # flag
                );

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


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

#$ENV{GBE_RM_LOCATION} = 'jdbc:oracle:thin:@auperaora07:1521:RELMANU1';
#$ENV{GBE_RM_USERNAME} = 'release_manager'; 
#$ENV{GBE_RM_PASSWORD} =  'release_manager';

getTextInComment( @ARGV );

sub getTextInComment
{
    my (@args ) = @_;
    my $found = 0;
    my @rows;

    connectRM(\$RM_DB) unless $RM_DB;

    #
    #   Create an arg list
    #   ie: like '%txt%'
    #   
    my @search;
    foreach my $arg (@args)
    {
        push @search, " pv.comments like '%$arg%' ";
    }
    

    #
    #   Now extract the package with matching comments
    #
    my $m_sqlstr = "SELECT UNIQUE pv.PV_ID, pkg.PKG_NAME,pv.PKG_VERSION " .
                   " FROM package_versions pv,packages pkg" .
                   " WHERE pv.pkg_id = pkg.pkg_id AND ( " . join(' or ', @search) . ") " .
                   " order by upper(pkg.PKG_NAME), pv.PKG_VERSION"
                   ;
#$m_sqlstr =~ s~\s+~ ~g;
#print "\n\n$m_sqlstr\n";
    my $sth = $RM_DB->prepare($m_sqlstr);
    if ( defined($sth) )
    {
        if ( $sth->execute( ) )
        {
            if ( $sth->rows )
            {
                while ( my @row = $sth->fetchrow_array )
                {
                    print "@row\n";
                    $found = 1;
                }
            }
            $sth->finish();
        }
        else
        {
            Error("GetDepends:Execute failure: $m_sqlstr", $sth->errstr() );
        }
    }
    else
    {
        Error("GetDepends:Prepare failure" );
    }

    unless ( $found )
    {
        Warning("No Packages found with comment containing: @args");
    }
}

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

=pod

=head1 NAME

scanComments - Scan Release Manager commants for text

=head1 SYNOPSIS

 Usage: jats [opts] scanComments [cmd-options] text [text...]

 Where opts:
    -help                       - brief help message
    -help -help                 - Detailed help message
    -man                        - Full documentation

=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 ARGUMENTS

=head2 Basic Commands

Scans the 'Reason For Release' field of all packages for matching text.

The '%' character can be used as a wild card character

=head1 EXAMPLES

jats scanComments DEVI%066675

Will locate all packages that contain 'DEVI-066675' or 'DEVI 066675' or the like.

=cut

