#! perl -w

##
## Name:
##
## cfx5params
##
## Purpose:
##
## Run CFX-5 Parameters Editor
## 
## NOTE: This program is intended to be used only under the supervision of 
##       the CFX support staff. Other use can have unintended consequences.
##
## Synopsis:
##
#S cfx5params -read|-write -definition <file> -text <file> 
#S            [-help] [-verbose]
##
## []  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.
##
## Arguments:
##
## -read
##     Read parameters from a CFX-5 Definition file and put into a text file.
##
## -write
##     Write parameters contained in the text file to the CFX-5 
##     Solver Input File.
##
## -definition <file> 
## -def <file> 
##     Use <file> as the CFX-5 Solver Input File.
##
## -text <file> 
##     Use <file> as the parameters text file.
##
## -help
## -h
##     Print this message.
##
## -verbose
## -v
##     Specifying this option may result in additional output being sent to 
##     the UNIX standard output file (normally the screen).
##

# 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);
sub BEGIN {
  $CVSVersion='@(#) $Id$\n';
}

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::FileTests;

use CFX5::PathManip qw(join_file);
use CFX5::Commands qw(do_cmd handle_rv do_cmd_quiet);

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

#
# Parse the command line
#
use vars qw($deffile $textfile $pexec);

parse_cmdline
  (\@ARGV,
   'INCLUDE_REQ',
   '-def+'        => \$deffile,
   '-definition+' => '-def',
   '-text+'       => \$textfile,
   -read          => sub { $pexec='readparams'; },
   -write         => sub { $pexec='writeparams'; },
  );

exit_if_error();

# 
# Check we got a read/write action and relevant files
# 
unless(defined $deffile or defined $textfile or defined $pexec) {
  # user didn't pass any arguments, don't give errors about all three!
  _error("No arguments specified. Type $CFX5::Output::Prog -help for help.");
} else {
  _error("Please specify -read or -write")
    unless defined $pexec;
  _error("Please specify a text file")
    unless defined $textfile;
  _error("Please specify a CFX-5 Solver Input File")
    unless defined $deffile;
}

$pexec .= '.exe';

# now check these are reasonable files to use
if ($pexec =~ m/read/i) {
  CFX5::FileTests::datafile($deffile) if defined $deffile;
  CFX5::FileTests::datafile_w($textfile) if defined $textfile;
} else {
  CFX5::FileTests::datafile_w($deffile) if defined $deffile;
  CFX5::FileTests::datafile($textfile) if defined $textfile;
}

exit_if_error();

CFX5::PrintConfig::show_vars_by_verbosity();

use vars qw($rv);
$rv=handle_rv(do_cmd(join_file($Config{CFX5EXECDIR}, $pexec), $deffile, $textfile));

if ($rv == 17) {
  _warn("The parameters editor could not locate the expert parameters in the",
	"\nsupplied definition file.  Please note that the parameters editor",
	"\nwill only work with CFX-5.3 definition files.\n");
  exit(2);
}

_warn("The parameters editor exited with return code $rv")
  if ($rv);

exit(defined $rv ? $rv : 2);
