#! perl -w

=head1 NAME

cfx_helpclient - help client for use by Vis and the SM

This is just a simple -- not to say trivial -- wrapper around the
cfx_helpclient.exe utility which fires off the help viewer on Windows
NT. It is needed to read the configuration and ensure that variables
such as C<CFX_HELP_DIR> are set.

=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(exit_if_error _error internal_error _warn _abort _debug);
use CFX5::Config qw(%Config export_conf query_conf prepend_to_execpath);

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

use CFX5::FrameServ;		# used by DefaultConfig to find Frame if included

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

shift @ARGV if (defined $ARGV[0] and $ARGV[0] eq "-NoConsole");

_abort "Missing filename argument" unless @ARGV;
my $argfile=shift @ARGV;

if ($Config{CFX_NO_FRAME_VIEWER}) {
  # force PDF instead of .fm files
  $argfile =~ s!\.fm$!\.pdf!;
}

#
# what sort of file are we to display?
#
my ($rootdir,$typename);
if ($argfile =~ m!\.pdf$!i) {
  # it's a PDF file
  $rootdir=$Config{CFX_PDF_DIR};
  $typename='PDF help file';
} else {
  # assume it's a frame file
  $rootdir=$Config{CFX_HELP_DIR};
  $typename='online help file';
}

CFX5::FileTests::datadir($rootdir, "$typename directory $rootdir");
exit_if_error();

my $filename=join_file($rootdir, $argfile);

CFX5::FileTests::datafile($filename, "$typename file $filename");
exit_if_error();

#
# for PDF files, this is easy
#
if ($argfile =~ m!\.pdf$!i) {
  CFX5::FrameServ::view_pdf($filename);
  exit();
}

#
# we do different things on UNIX vs NT
#
if ($Config{windows_nt}) {
  #
  # Windows NT

  # make sure we can find the executable
  my $fmhome=$Config{FMHOME};
  if (defined $fmhome and -x join_file($fmhome,'FrameViewer.exe')) {
    _debug("Found FrameViewer.exe in $fmhome");
    prepend_to_execpath($fmhome);
  } elsif (findbin_by_path('FrameViewer.exe') eq '.') {
    # nope, no such luck
    _error("CFX-5 was unable to locate the Adobe Frame Viewer, which is required to view the CFX-5 online help text.  Please check your installation of the frame viewer and try again.");
    exit_if_error();
  }

  #
  # spawn the cfx_helpclient.exe proglet
  #
  export_conf(qw(CFX_HELP_DIR));

  {
    my $out=get_cmd_output(join_file($Config{CFX5EXECDIR},'cfx_helpclient.exe'), '-fullpath', $filename, @ARGV);
    if ($out !~ m/^[\n\r]*$/) {
      _error($out);
    }
  }
  exit_if_error();
} else {
  #
  # UNIX
  #
  # spawn the proglet supplied by Frame for just this purpose
  #
  _warn("CFX_HELP_ERR currently ignored") 
    if defined query_conf('CFX_HELP_ERR');
  _abort("Wrong number of arguments")
    if scalar(@ARGV) > 1;
  
  my $fmhome=$Config{FMHOME};

  _abort("Top-level FrameViewer directory $fmhome missing.\n",
	 "Is the Help Viewer installed correctly?")
    unless -d $fmhome;

  _abort("Frame viewer architecture string FMARCH unknown")
    unless defined $Config{FMARCH};

  _abort("Frame Viewer for $Config{arch_name} ($Config{FMARCH}) does not\n",
	 "appear to be installed.  Is the Help Viewer installed correctly?")
    unless -d join_file($fmhome,'bin',$Config{FMARCH});

  # allow FrameViewer to run on IBM without requiring the installation
  # of SLHS/6000 Development Tools
  if ($Config{CFX5OS} eq 'aix') {
    $Config{LIBPATH}="/usr/lib:/lib:$fmhome/bin/$Config{FMARCH}:$Config{LIBPATH}:";
    export_conf('LIBPATH');
  }

  $Config{CFX_HELP_BACKGROUND_SERVER}='yes';
  export_conf('CFX_HELP_BACKGROUND_SERVER');

  my $rpcProp=$Config{CFX_HELP_RPC_PROP} || '_Frame_RPC';

  my @cmd=join_file($Config{FMHOME},'bin','fmclient');
  push @cmd,'-rpcProp',$rpcProp;
  push @cmd,'-fpname',join_file($Config{CFX_CMD_DIR},'cfx_helpserver');
  push @cmd,'-f',$filename; # filename to open
  push @cmd, $ARGV[0] if defined $ARGV[0]; # link to visit

  my $rv=handle_rv(do_cmd(@cmd));
  _warn("Frame Client exited with return code $rv")
    if ($rv);
  exit(defined $rv ? $rv : 1);
}
