#! perl -w

=head1 NAME

cfx5cvfconvert - Convert CVF files to AVZ files using CFXViewer

=cut

#!/bin/sh
#==============================================================================
# @(#) $Id$
#-------------------------------------------------------------------------------
##
#H Name:
##
## cfx5cvfconvert
##
#H Purpose:
##
## Convert CVF files to AVZ files
##
#H Synopsis:
##
## cfx5cvfconvert [-help] [-v] cvf_file_1 [cvf_file_2] [...]
##
## []  denotes an optional argument.
## |   separates mutually exclusive arguments.
## ... following an argument indicates that it may be repeated.
## <>  denotes that substitution of a suitable value is required. All other
##     arguments are keywords, some of which have a short form.
##
#H Arguments:
##
#A -help
#A -h
##     Print this message.
##
#A -verbose
#A -v
##     Specifying this option may result in additional output being sent to
##     the UNIX standard output file (normally the screen).
##
#U
#U ****************************************************************************
#U *                                                                          *
#U *     The following features are supported only within CFX and are not     *
#U *     shown by the -help option when the command is run elsewhere.         *
#U *                                                                          *
#U ****************************************************************************
#U
#U -local
#U     Rather than using the central version of CFX-Viewer, assume that
#U     the current directory is the root of a locally-built version
#U     and run from that.
#U

# get the standard CFX-5 library directory
BEGIN {
    if ( $0 =~ m!^([a-z]:)?[\\/]!i ) {

        # it's an absolute path, use it as-is
        ( my $nd = $0 ) =~ s!(perllib[\\/])?[^\\/]*$!!i;
        unshift @INC, "$nd/perllib";
    }
    else {
        # wrap this in an eval, and if it fails, pray ... for example,
        # this can fail on Windows if we're running off a share.
        eval {
            require FindBin;
            FindBin->import();
            unshift @INC, "$FindBin::Bin/perllib";
            my $dummy = $FindBin::Bin;    # kill used only once warning
        };
        warn $@ if $@;
    }
    if ( defined $ENV{CFX_LOCAL_PERLLIB} ) {
        unshift @INC, $ENV{CFX_LOCAL_PERLLIB};
    }
}

use CFX5::Init;
CFX5::Init::init();

use strict;

use CFX5::Output qw(:all);
use CFX5::CmdLine qw(:util parse_cmdline);
use CFX5::Help;
use CFX5::CVFConverter qw(setup_viewer convert_cvf);

use vars qw($local @state_files @ignored_files);

$local         = 0;
@state_files   = ();
@ignored_files = ();

my @nonopt = parse_cmdline(
    \@ARGV,
    'ALLOW_NON_OPTION',
    'INCLUDE_STD',
    -local => \$local,
    '*'    => sub {
        my $fn = $_[1];
        if ( $fn =~ m/.*[.]cvf$/i ) {
            push @state_files, $fn;
        }
        else {
            push @ignored_files, $fn;
        }
    },
);

exit_if_error();

if (@ignored_files) {
    _warn("The following files do not have cvf file extension and will be ignored:\n @ignored_files");
}

my $rv = setup_viewer($local);

if ($rv) {
    exit($rv);
}

$rv = convert_cvf(@state_files);

if ($rv) {
    exit($rv);
}

__END__
