#! perl -w
##
## Name:
##
## cfx5dmerge
##
## Purpose:
##
## Merge multiple definition files to create a GGI definition file
##
## Synopsis:
##
#S cfx5dmerge -help
#S cfx5dmerge -write <dest.def> [-no-conflict] [-no-merge]
##           [-glue <file.ccl>] <file.def> [<file.def> [...]]
##
## []  denotes an optional argument.
## |   separates mutually exclusive arguments.
## ... following an argument indicates that it may be repeated.
## <>  and # denote that substitution of a suitable value is required. All 
##     other arguments are keywords, some of which may have a short form.
##
## Arguments:
##
#A <file.def> [...]
##    One or more CFX-5 definition files to merge.
##
#A -glue <file.ccl>
#A -add <file.ccl>
#A -a <file.ccl>
##    Specifies the name of a file containing additional Command
##    Language to include.
##
#A -no-conflict
##    Switches off name conflict resolution.
##
#A -no-merge
##    Switches off merging of Command Language data from the
##    definition files.
##
#A -verbose
#A -v
##    Show additional output information.
##
#A -write <dest.def>
##    Gives the name of the definition file to write.
##

# 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 vars qw($CVSVersion);
$CVSVersion= '@(#) $Id$ ';

use strict;
use CFX5::Output qw(:all);
use CFX5::Config qw(%Config export_conf);
use CFX5::PrintConfig;
use CFX5::CmdLine qw(:util parse_cmdline);
use CFX5::PathManip qw(join_file fullpath);
use CFX5::Commands qw(do_cmd handle_rv parse_rv);
use CFX5::CCLI qw(export_parser_conf);
use CFX5::ListManip qw(check_unique_list);

# define custom env vars
use CFX5::Config::def
  ( CFX5_DMERGE_EXEC => sub { join_file($Config{CFX5EXECDIR},'dmerge.exe'); } );

# read the configuration
CFX5::Config::read_config();

# parse the command line
use vars qw(%args @nonopt @deffiles $gluefile $noconflict $nomerge $outfile);

%args=(
     '-a+'          => sub{ conf_datafile(\$gluefile, undef, @_); },
     '-add+'        => '-a',
     '-glue+'       => '-a',
     '-h'           => '-help',      
     '-no-conflict' => \$noconflict,
     '-no-conf'     => '-no-conflict',
     '-no-merge'    => \$nomerge,
     '-verbose'     => '-v',
     '-write+'      => sub{ conf_datafile_w(\$outfile, undef, @_); },
     '-out+'        => '-write',
     '-outfile+'    => '-write',
);

my @nonopt=parse_cmdline(\@ARGV, 'INCLUDE_STD', 'ALLOW_NON_OPTION', %args);
exit_if_error();

if (@nonopt) {
  for my $deffile (@nonopt){
    CFX5::FileTests::datafile($deffile);
    $deffile=fullpath($deffile);
    push(@deffiles, $deffile); 
  }
} else {
  _error("You must specify a CFX-5 Definition or Results file to use.", CFX5::Help::usage_msg_once());
}

my @args;
push(@args, '-read', @deffiles);

#must have only one output definition file
unless (defined $outfile){
  _error("You must specify a file to write to.", CFX5::Help::usage_msg_once());
}
push(@args, '-write', $outfile);

#must have only one glue file
unless (defined $gluefile){
  _error("You must specify a glue file.", CFX5::Help::usage_msg_once());
}
push(@args, '-a', $gluefile);
exit_if_error();

#check that input, output and glue files are not the same files
my @checklist = (@deffiles,$outfile,$gluefile);
my $description = "file";
CFX5::ListManip::check_unique_list($description, @checklist);
exit_if_error();

unless ($noconflict){
  push(@args, '-c');
}

unless ($nomerge){
  push(@args, '-m');
}

CFX5::CCLI::export_parser_conf();
CFX5::PrintConfig::show_vars_by_verbosity();
exit_if_error();

my $rv;
CFX5::FileTests::execfile($Config{CFX5_DMERGE_EXEC}, "dmerge executable $Config{CFX5_DMERGE_EXEC}");
exit_if_error();
$rv=handle_rv(do_cmd($Config{CFX5_DMERGE_EXEC}, @args));
exit defined $rv ? $rv : 2;
