#! perl -w

=head1 NAME

cfxlaunch - Run the CFX Launcher

=cut

#!/bin/sh
#==============================================================================
# @(#) $Id$
#------------------------------------------------------------------------------
##
## Name:
##
## cfxlaunch
##
## Purpose:
##
## Run the CFX Launcher
##
## Synopsis:
#S
#S cfxlaunch [-help] [-verbose] [-display <display>]
#S
## []  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:
##
#A -display <display>
##     Use the X11 server <display> instead of the X11 server
##     defined by the DISPLAY environment variable.
##
#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).
##
#------------------------------------------------------------------------------

# 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";
      push @INC, $FindBin::Bin;
    };
    warn $@ if $@;
  }
  if (defined $ENV{CFX_LOCAL_PERLLIB}) {
     unshift @INC, $ENV{CFX_LOCAL_PERLLIB};
  }

}

#
# Generate an unique session ID for the ANSYS help system.
#
if(! $ENV{ANSHELP_SESSION_ID})
{
	srand (time ^ $$ ^ unpack "%L*", rand());
	my $ANSHELP_SESSION_ID = "";
	for ($i = 1; $i <= 64; $i++)
	{
		$ANSHELP_SESSION_ID = $ANSHELP_SESSION_ID . sprintf("%x", int(rand(16)));
	}
	$ENV{ANSHELP_SESSION_ID} = $ANSHELP_SESSION_ID;
}

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 query_conf append_to_libpath prepend_to_libpath check_gui_support);
use CFX5::PrintConfig;

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

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

use CFX5::ExitRoutines qw(:tidyup);

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

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

use CFX5::LIST;

#
# before we do anything else which might use it, restore TMPDIR if it
# was changed by cfx5build.
#
if (exists $ENV{CFX_BUILD_ORIG_TMPDIR}) {
  if ($ENV{CFX_BUILD_ORIG_TMPDIR} eq "TMPDIR was not set") {
    delete $ENV{TMPDIR};
  } else {
    $ENV{TMPDIR}=$ENV{CFX_BUILD_ORIG_TMPDIR};
  }
  delete $ENV{CFX_BUILD_ORIG_TMPDIR};
}


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

parse_cmdline
  ( \@ARGV,
    'INCLUDE_STD',
  );

exit_if_error();

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

#------------------------------------------------------------------------------
# Start the Launcher


use CFX5::Config::def
  CFX_LAUNCH_EXEC => sub { join_file($Config{CFX5EXECDIR},'Launcher.exe'); },
  CFX_LAUNCH_SCRIPT_PATH => sub { join_file($Config{CFX5BINDIR},'cfx5launch'); },
  # these are the correct settings, I think
  CFX_LAUNCH_ETC_DIR => sub { $Config{CFX_DATA_DIR}; },
  CFX_LAUNCH_CFG_DIR => sub { join_file($Config{CFX_DATA_DIR},'launcher'); },
;

CFX5::CUEI::export_common_conf();

sub start_launcher {
    #
    # Check we've got somewhere to display the Launcher GUI.
    #
    CFX5::System::assert_can_window();

    prepend_to_libpath($Config{CFX5LIBDIR},$Config{CFX_QT_LIBDIR},$Config{CFX_QWT_LIBDIR},
                      $Config{CFX_STL_LIBDIR},$Config{CFX_STLPORT_LIBDIR});

    my @exportvars=qw(CFX_DATA_DIR CFX5EXECDIR CFX_CMD_DIR
                      CFX_ICON_DIR CFX_CONFIG_DIR CFX5ROOT
                      CFX5_HOSTS_CCL CFX_HOST CFX_FULLHOST
                      CFX_LAUNCH_SCRIPT_PATH
                      CFX_LAUNCH_ETC_DIR CFX_LAUNCH_CFG_DIR
                      CFX_LICMAN_EXEC CFX_LICENSING_ROOT
                      CFX5BINDIR CFX5OS
                      CUE_GLOBAL_DATA_DIR CFX_SITEFILE ANSYSLIC_DIR
                      CFXBUILDID CFX_PRODUCT_NAME CFX_PRODUCT_FULLNAME
                      AWP_COMMON_FILES_ROOT
                      ANSYS_COMMON_IMAGES_DIR
                     );

    export_conf(@exportvars);

    export_parser_conf();

    CFX5::PrintConfig::show_vars_by_verbosity();

    my @args;

    my $rv=parse_rv(do_cmd($Config{CFX_LAUNCH_EXEC}, @args));
    unless ($rv->is_success()) {
      _error("The CFX Launcher", $rv->errstr());
    }

    return $rv->shell_rv();
}

exit(start_launcher());
