#! perl -w

##
## Name:
##
## cfx5licenseman
##
## Purpose:
##
## Start the CFX License Manager
##
## Synopsis:
#S
#S cfx5licenseman [-verbose] [-help] [-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.
##
## The cfx5licenseman command starts the CFX License Manager, as
## distributed with the CFX Licensing Tools.
##
## Arguments:
##
## -display <display>
##     Display the graphical user interface on the X11 server <display>
##     instead of using the server defined by the DISPLAY environment variable.
##
## -help
## -h
##     Print this message.
##
## -verbose
## -v
##     Specifying this option may result in additional output being
##     sent to the standard output file (normally the screen).
##

=head1 NAME

cfx5llicenseman - Run the CFX License Manager

=cut

# 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::System qw(assert_can_window);
use CFX5::FrameServ;

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

use CFX5::PathManip qw(join_file);

use CFX5::LIST;

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

#
# parse the command line
#

use vars qw($wish);
$wish=0;

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

exit_if_error();

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

unless (defined CFX5::LIST::license_dir()) {
  _abort("The CFX License Manager installation directory could not be found.  Please ensure that both CFX-5 and the CFX License Manager were installed correctly.");
}

my $exec=$Config{CFX_LICMAN_EXEC};
CFX5::FileTests::execfile($exec, "CFX License Manager executable $exec");

exit_if_error();

#
# next define environment variables needed by the launcher
#
my @exportvars=qw( CFX5EDITOR LM_LICENSE_FILE CFX_LICENSE_ROOT
                  CFX_WIN32_DRIVE_LIST);

#
# on NT, force everything to use forward-slashes, else Tcl gets confused.
# This is a shame.
#
if ($Config{windows_nt}) {
  for my $rec (@exportvars) {
    $Config{$rec} =~ s{\\}{/}g
      if defined $Config{$rec};
  }
}

export_conf(@exportvars);

CFX5::PrintConfig::show_vars_by_verbosity();

exit_if_error();

use vars qw($rv);
{
  my @args=($Config{CFXBUILDID});
  unless ($Config{windows_nt} and !$Config{CFX_MOTIF_LOOK}) {
    push @args, '-aea';
  } else {
    push @args, '-win';
  }

  $rv=handle_rv(do_cmd($Config{CFX_LICMAN_EXEC}, @args));
}

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

