########################################################################
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
#
# Module name   : jats_ccdelete_view.pl
# Module type   : Makefile system
# Compiler(s)   : Perl
# Environment(s): jats
#
# Description   : Delete a Clear Case view
#                 Need the view tag
#
# Usage         : See POD at the end of the file
#
#......................................................................#

require 5.008_002;
use strict;
use warnings;

use Cwd;
use JatsError;
use Time::Local;
use Pod::Usage;                             # required for help support
use Getopt::Long;

my $VERSION = "1.0.0";                      # Update this
my $opt_verbose = $ENV{'GBE_VERBOSE'};      # Allow global verbose
my $opt_help = 0;
my $opt_test;

my @vlist;

#-------------------------------------------------------------------------------
# Function        : Mainline Entry Point
#
# Description     :
#
# Inputs          :
#
my $result = GetOptions (
            "help|h:+"          => \$opt_help,
            "manual:3"          => \$opt_help,
            "verbose|v:+"       => \$opt_verbose,
            "test"              => \$opt_test,

            );

            #
            #   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_help > 2 );
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ( $#ARGV < 0 );

#
#   Configure the error reporting process now that we have the user options
#
ErrorConfig( 'name'    =>'CC_DELETE',
             'verbose' => $opt_verbose,
            );


#
#   Process views and determine information
#       Allow the use of '*' under Windows with a 'glob'
#
foreach my $view ( map { glob } @ARGV )
{
    my $uuid;

    next if ( $view eq 'sandbox_dpkg_archive' );
    if ( -d $view )
    {
        Message ("Delete view path: $view");
        ClearTool( "--Quiet", "rmview $view" ) || Warning('Problem deleting view');
#        $opt_verbose = 1;
#        ClearTool( "rmview $view" );
        next;
    }

    #
    #   If the view tag still exists then delete the view the hard way
    #   Use 'lsview' to locate the views uuid
    #
    Verbose("Look for View Tag");
    my $cmd = "cleartool lsview -long $view";
    open(CMD, "$cmd 2>&1 |") || Error( "can't run command: $!");
    while (<CMD>)
    {
        #
        #   Filter output from the user
        #
        chomp;
        Verbose("lsview: $_");
        $uuid = $1 if ( m~^View uuid:\s+(.*)~ );
        $uuid = $1 if ( m~^\s*View tag uuid:\s*(.*)~ );
    }
    close(CMD);

    if ( $uuid )
    {
        Verbose ("UUID: $uuid");
        unless ( $opt_test )
        {
            ClearTool( "--Quiet", "rmview -force -all -uuid $uuid" );
            ClearTool( "--Quiet", "unregister -view -uuid $uuid" );
            ClearTool( "--Quiet", "rmtag -view -all $view" );
        }
    }
}

#-------------------------------------------------------------------------------
# Function        : ClearTool
#
# Description     : Issue a cleartool command
#                   Filter out many of the stupid messages
#
# Inputs          : Options and Command line
#                   Options:
#                       --Quiet     - Supress all command output
#
# Returns         : Error code
#
sub ClearTool
{
    my $quiet;

    #
    #   Scan for initial options
    #       --Quiet
    #
    if ( $_[0] eq '--Quiet' )
    {
        $quiet = 1;
        shift;
    }

    my $cmd = "cleartool @_";

    Verbose ("ClearTool: $cmd");
    open(CMD, "$cmd 2>&1 |") || Error "can't run command: $!";
    while (<CMD>)
    {
        #
        #   Filter output from the user
        #
        next if ( $quiet );
        unless ( $opt_verbose )
        {
            next if ( m~Making dir~ );
            next if ( m~End dir~ );
            next if ( m~Processing dir~ );
            next if ( m~Error~ );
        }
        print $_;
    }
    close(CMD);

    Verbose2 "ClearTool Exit Status: $?";
    return $? / 256;
}

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

=pod

=pod

=for htmltoc    GENERAL::ClearCase::

=head1 NAME

jats ccdelete_view - Delete a clearcase views

=head1 SYNOPSIS

  jats etool ccdelete_view [options] [viewtag|viewpath]+

 Options:
    -help[=n]           - brief help message
    -help -help         - Detailed help message
    -man[=n]            - Full documentation
    -verbose[=n]        - Verbose operation
    -test               - Show what will be done

=head1 OPTIONS

=over 8

=item B<-help[=n]>

Print a brief help message and exits.

The verbosity of the help text can be controlled by setting the help level to a
number in the range of 1 to 3, or by invoking the option multiple times.

=item B<-man[=n]>

Without a numeric argument this is the same as -help=3. Full help will be
displayed.

With a numeric argument, this option is the same as -help=n.

=item B<-verbose[=n]>

This option will increase the level of verbosity of the utility.

If an argument is provided, then it will be used to set the level, otherwise the
existing level will be incremented. This option may be specified multiple times.

=item B<-test>

This option will not perform any deletion. It will only show what will be done.

=back

=head1 DESCRIPTION

This utility is used to delete a Clear Case views. If the user provides a valid
directory then the view store and view path will be removed, otherwise the tool
assumes that a viewtag has been provided. It will:

=over 4

=item 1

Remove the view from the core of ClearCase

=item 2

Remove the view from ClearCase's registry

=item 3

Remove the view tag

=back

The removal of the view will also:

=over 4

=item *

Remove checked out files, both reserved and unreserved

=item *

Delete the view from the users machine, if it can be located.

=back

The utility can be used to delete views that exist only in the Clear Case
database after they have been removed from a users machine. Access to the
view is not required.

The utility can be used to delete one or more views.

The cleartool 'lsview' command can be used to display views known to Clear Case.
Views created by JATS are prefixed by the user's name. This can be included
in the command. Thus

    cleartool lsview SomeUser*

will list all views, created by JATS, created by 'SomeUser'.

=cut

