########################################################################
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
#
# Module name   : cc2svn_updaterm.pl
# Module type   : Makefile system
# Compiler(s)   : Perl
# Environment(s): jats
#
# Description   : Show dirs that have been relabled
#
# Usage:
#
# Version   Who      Date        Description
#
#......................................................................#

require 5.008_002;
use strict;
use warnings;

use Pod::Usage;
use Getopt::Long;

use JatsError;

#
#   Globals
#
my $VERSION = "1.0.0";                      # Update this
my %pkgInfo;
#
#   Options
#
my $opt_verbose = 0;
my $opt_help = 0;
my $opt_package;

#-------------------------------------------------------------------------------
# Function        : Main Entry
#
# Description     :
#
# Inputs          :
#
# Returns         :
#
my $result = GetOptions (
                "help+"         => \$opt_help,          # flag, multiple use allowed
                "manual:3"      => \$opt_help,
                "verbose:+"     => \$opt_verbose,       # flag
                'package:s'     => \$opt_package,
                );

#
#   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_help > 2);

ErrorConfig( 'name'    =>'CC2SVN_SHOWL',
             'verbose' => $opt_verbose,
              );

Error ("Must specify a package name") unless ( defined $opt_package || $#ARGV >= 0 );


foreach my $fileName ( $opt_package, @ARGV )
{
    next unless ( defined $fileName );
    next if ( $fileName eq 'Dataman' );

    $fileName =~ s~\.data$~~;
    $fileName =~ s~\.svg$~~;
    $fileName =~ s~\.importlog$~~;
    readPackageDataFile($fileName);

    foreach my $packageName( sort keys %pkgInfo )
    {
        processPackage($packageName);
    }
}
exit 0;

#-------------------------------------------------------------------------------
# Function        : readPackageDataFile
#
# Description     : Read in the data file. It may contain data for more
#                   than one package - but this is rare
#
# Inputs          : 
#
# Returns         : Fills in %pkgInfo
#
#
our %ScmVersions;
sub readPackageDataFile
{
    %pkgInfo = ();
    my ($pname) = @_;
    my $fname = $pname . '.data';
    Verbose2 ('Reading Package Data: ' . $fname);
return unless ( -f $fname );
    Error "Cannot locate $fname" unless ( -f $fname );
    %ScmVersions = ();
    require $fname;

    Error "Data in $fname is not valid\n"
        unless ( keys(%ScmVersions) >= 0 );

    foreach (keys %ScmVersions)
    {
        my $entry = $ScmVersions{$_};
        $pkgInfo{$entry->{name}}{$_} = $entry;
    }

    %ScmVersions = ();
}

#-------------------------------------------------------------------------------
# Function        : processPackage
#
# Description     : Process data for one package
#
# Inputs          : Name of the package
#
# Returns         : 
#
sub processPackage
{
    my ($pname) = @_;
    Error ("Internal: Hash data not found")
        unless ( exists $pkgInfo{$pname});
    my $pkgData = $pkgInfo{$pname};

    foreach (sort {$a <=> $b}  keys(%{$pkgInfo{$pname}} ) )
    {
        my $pkgEntry = $pkgInfo{$pname}{$_};

        if ( exists $pkgEntry->{data}{DirsLabled} )
        {
            next if ( $pkgEntry->{data}{DirsLabled} == 100 );
            #print "$pname: ",$pkgEntry->{vname},", ",$pkgEntry->{data}{DirsLabled},", ", $pkgEntry->{data}{ViewRoot},"\n";
            print $pkgEntry->{data}{ViewRoot},"\n";
        }

    }
}


#-------------------------------------------------------------------------------
#   Documentation
#

=pod

=for htmltoc    SYSUTIL::cc2svn::

=head1 NAME

cc2svn_show_relabled - Show packages that were relabled

=head1 SYNOPSIS

  jats cc2svn_updaterm [options] [PackageName]*

 Options:
    -help              - brief help message
    -help -help        - Detailed help message
    -man               - Full documentation
    -verbose           - Enable verbosity
    -package=name      - Specify single package to be processed

=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.

=item B<-[no]test>

Invoke the program in test mode. This is the default.

In test mode the program will examine each packages 'data' results file
and report status and errors.

In 'notest' the program will update the Release Manager database.

=item B<-[no]live>

Invoke the program in live-database mode. This is the NOT the default.

In live mode the program will access the Live Release Manager database.

In non-live mode the program will access the test (RELMANU1) database

=item B<-package=name>

This option will name one package to be processed. Packages to be
processed can just be named on the command line.

=item B<-database=name>

This option specifies the target database. The default value is 'RELMANU1'.
This is a test database.

The production database is RELEASEM.

The user must specifically specify the database to update the production system.

=item B<-[no]force>

This option will force the Release Manager entries to be updated - even if the
current entry matches the desired result.

Useful in testing.

=item B<-check=string>

This option will pass a string directly to the Release Manager updating proceedure.

With this option no packages will be examined or processed.

It is only useful for testing.

=back

=head1 DESCRIPTION

This program is a tool used in the conversion of ClearCase VOBS to subversion.
It will:

=over 8

=item *

Process all packages named on the command line or with the -package option.

=item *

Examine the packages '.data' file, whcih is created as the package is inserted
into Subversion.

=item *

Report the status of the package import and highlight issues.

=item *

Read the Release Manager entry and ensure that the entry is not the same.
If the entry is the same then it will not be updated, unless the '-force'
option has been used.

=item *

Insert the new Version Control information into the Release Manager entry.

=back

The default operation of this utility is to test the import process. The
user needs to provide specific options in order to update the production
database. This is intentional.

=cut

