#! perl -w

=head1 NAME

cfx5viewer - run CFX-Viewer

=cut

#!/bin/sh
#==============================================================================
# @(#) $Id$
#-------------------------------------------------------------------------------
##
#H Name:
##
## cfx5viewer
##
#H Purpose:
##
## Run the CFX Post Processor
##
#H Synopsis:
##
## cfx5viewer [-graphics ogl|mesa] [-display <display>] [-help] [-v] 
##            [viewer_state_file]
##
## []  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 -display <display>
#A -d <display>
##     Display the graphical user interface on the X11 server <display>
##     instead of using the X11 server defined by the DISPLAY environment
##     variable.
##
#A -graphics ogl|mesa
#A -gr ogl|mesa
##     Set the graphics mode to use for the GUI to the OpenGL (hardware)
##     or MESA (software)-based version.
##
#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).
##
#A -t
##     Load viewer.dmp file or test?.dat file
##
#U
#U -e
#U     Load an event file
#U
#U -n
#U     Start with no window
#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 vars qw($CVSVersion);
sub BEGIN {
  $CVSVersion='@(#) $Id$\n';
}

use strict;

use CFX5::Output qw(:all);
use CFX5::Config qw(%Config export_conf append_to_libpath prepend_to_libpath append_to_execpath prepend_to_execpath setconf check_gui_support);
use CFX5::PrintConfig;

use CFX5::CmdLine qw(:util parse_cmdline);

use CFX5::Help;

use CFX5::PathManip qw(join_file split_file fullpath basename dirname);
use CFX5::FileTests;

use CFX5::ExitRoutines qw(:tidyup);

use CFX5::System qw(assert_can_window);

use CFX5::FrameServ;

use CFX5::Commands qw(do_cmd do_cmd_quiet handle_rv parse_rv get_cmd_output);

use CFX5::CCLI qw(export_parser_conf);
use CFX5::CUEI;
use CFX5::LIST;

CFX5::Config::read_config();

use vars qw($mode $local $nowindow $stateFile);

$mode='gui';
$local=0;
$nowindow=0;
$stateFile='';

my @nonopt = parse_cmdline
  (
   \@ARGV,
   'ALLOW_NON_OPTION',
   'INCLUDE_STD',
   -e => sub { $mode='event'; },
   -n => \$nowindow,
   '-graphics+' => \$Config{CUE_GRAPHICS},
   '-gr+' => '-graphics',
   -local => \$local,
   -t => sub { $mode='test'; },
    # the first non-option argument is the state file name
    '*' => sub { $stateFile=$_[1]; },
  );

exit_if_error();

if (@nonopt) {
  _error("Unexpected arguments", @nonopt, "without an associated argument were found on the command line.");
}

exit_if_error();

_error("GUIs are not supported on this platform") unless check_gui_support();
exit_if_error();

#
# On DECs the _RLD_ARGS environemnt variable will be set if CFX-Viewer is
# started from CFX-Build. However the setting used by CFX-Build will
# cause CFX-Visualise to crash, and might affect viewer too. So we'll unset
# it.
#
delete $ENV{_RLD_ARGS};

CFX5::CUEI::export_post_conf();

export_conf(qw(ANSYS_COMMON_IMAGES_DIR));

# check for DISPLAY
assert_can_window();

exit_if_error();

CFX5::PrintConfig::show_vars_by_verbosity();

my @args;
my $viewerDir;

if ($mode eq 'test') {
  push @args, '-t';
} elsif ($mode eq 'event') {
  push @args, '-e';
}
if ($nowindow) {
  push @args, '-n';
}
if ($stateFile ne '') {
    push @args, $stateFile
}
if ($local) {
    $viewerDir="./bin/$Config{\"CFX5OS\"}/";
  CFX5::CUEI::export_pre_conf();
} else {
    $viewerDir="$Config{\"CUE_VIEWER_DIR\"}/";
}

CFX5::FileTests::execfile("$viewerDir$Config{\"CUE_VIEWER_EXE\"}",
                          "CFX-Viewer executable ".$Config{CUE_VIEWER_EXE});

exit_if_error();

my @runwith;

my $rv=handle_rv(do_cmd(@runwith, "$viewerDir$Config{\"CUE_VIEWER_EXE\"}", @args));
_warn("CFX-Viewer exited with return code $rv") if ($rv);
exit($rv);
