########################################################################
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
#
# Module name   : jats.sh
# Module type   : Makefile system
# Compiler(s)   : n/a
# Environment(s): jats
#
# Description   : Determine packages with mismatched dpkg_archive names
#                 ie: Case differences
#
#......................................................................#

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;

#
#   Package information
#
my $total;
my %Names;
my %NamesLc;
my %PkgNames;

#-------------------------------------------------------------------------------
# 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);
pod2usage(-verbose => 1)  if ($opt_help == 2 );
pod2usage(-verbose => 2)  if ($opt_manual || ($opt_help > 2));


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

getDpkgArchiveNames();
getPkgDetailsByRTAG_ID();
#DebugDumpData("Names", \%Names );
#DebugDumpData("Names", \%PkgNames );

foreach my $dir ( sort keys %PkgNames) 
{
    if ( exists $Names{$dir})
    {
        next;
    }
    if ( exists $NamesLc{lc $dir} )
    {
        Warning("$dir does not exist - but " . $NamesLc{lc $dir} . " does");
    }
}



print "Package Names: $total\n";
exit;

sub getDpkgArchiveNames
{
    my $dir = $ENV{GBE_DPKG};
    Error ("DPKG_ARCHIVE is not a dir") 
        unless(-d $dir);

    opendir (my $dh, $dir) or Error ("Coundn't read DPKG_ARHIVE");
    while (readdir $dh)
    {
        $PkgNames{$_} = 1;
    }
    close $dh;
}


sub getPkgDetailsByRTAG_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);

    # First get all packages that are referenced in a Release

    my $m_sqlstr = "SELECT DISTINCT pkg.PKG_NAME" .
                   " FROM RELEASE_MANAGER.RELEASE_CONTENT rc, RELEASE_MANAGER.PACKAGE_VERSIONS pv, RELEASE_MANAGER.PACKAGES pkg" .
                   " WHERE rc.PV_ID = pv.PV_ID AND pv.PKG_ID = pkg.PKG_ID" .
                   " order by pkg.PKG_NAME";
    my $sth = $RM_DB->prepare($m_sqlstr);
    if ( defined($sth) )
    {
        if ( $sth->execute( ) )
        {
            print "--- Execute\n";
            if ( $sth->rows )
            {
                print "--- Execute ROWS\n";
                while ( @row = $sth->fetchrow_array )
                {
                    print "Data: @row\n";
                    my %data;
                    my $name = $row[0];
                    $Names{$name} = 1;
                    $NamesLc{lc $name} = $name;
                    $total++;
                }
            }
            print "--- Finish\n";
            $sth->finish();
        }
        else
        {
            Error("Execute failure: $m_sqlstr" );
        }
    }
    else
    {
        Error("Prepare failure" );
    }
}

