=head1 NAME

  CFX5::PLAPI - (trivial) interface to the plapi from Perl

=head1 SYNOPSIS

  use CFX5::PLAPI;
  my $plapi=CFX5::PLAPI->new();

  $plapi->read_help_map('iproductHelpMap.txt');
  $plapi->show_help('cfx5help:Master Contents');

=head1 DESCRIPTION

This is a thin wrapper around the plapi, to enable access to it from
Perl scripts.

  $Id$

=cut

package CFX5::PLAPI;

use strict;

use CFX5::Config qw(%Config export_conf);
use CFX5::Output qw(:all);

use CFX5::PathManip qw(is_absolute join_file);
use CFX5::FileTests;

use CFX5::Commands qw(do_cmd_quiet parse_rv);

=head3 new

=cut

sub new {
  my $this=shift;
  my $class = ref($this) || $this;
  my $self={};
  bless $self, $class;
  return $self;
}

=head3 check_setup

  # CFX5::PLAPI->setup() or _error_;
  # or
  # $plapi->setup() or _error_;

  exit_if_error();

Checks that all required parts of the infrastructure are available for
the plapi to work, and exports the required variables etc.  When using
the object interface you usually do not need to call this function as
it is done automatically for you, but it is also available for use on
its own to set things up for another application.

=cut

use CFX5::Config::def CFX_PLAPI_EXEC => sub
{
    return(join_file($Config{ANSYS_COMMON_ROOT},'help', 'HelpViewer', 'ANSYSHelpViewer.exe'));
 };

sub setup {
  my $this=shift;
  my $fatal=shift;

  # check we've got everything we need
  # export_conf(qw(CFX_PLAPI_EXEC));

  1;
}

=head3 read_help_map

  # $plapi->read_help_map('iproductHelpMap.txt');

Sets the help map to use.  If a relative path is specified, the map is
located in the CFX_DATA_DIR directory.

Currently, this I<does not> read the help map, it just marks it for
passing onto the plapi.exe, but it may if we come up with a more
closely-tied interface later.  The name is kept for compatibility with
cue::HelpProvider.

=cut

sub read_help_map {
  my $self=shift;
  my $map=shift;

  unless (is_absolute($map)) {
    $map = join_file($Config{CFX_DATA_DIR}, $map);
  }

  CFX5::FileTests::datafile($map, "Help map file $map")
      or return 0;

  $self->{map} = $map;

  1;
}

=head3 show_help

  $plapi->show_help('cfx5help:Master Contents');

Given a link name, uses the plapi to bring up the help text associated
with that link.

=cut

sub show_help {
  my $self=shift;
  my $tag=shift;
  my $helpLanguage;
  my $env_language = $ENV{AWP_LOCALE150};
  if ($env_language) {
    $helpLanguage = $env_language;}
  else {
    $helpLanguage = "en-us";
  }
  
  $self->setup() or return 0;
  my @exec;
  if( $Config{windows_nt} ){
  
      @exec =($Config{CFX_PLAPI_EXEC}, "--language",$helpLanguage);
   }
   else{
      @exec =($ENV{CFX5MONO},$Config{CFX_PLAPI_EXEC}, "--language",$helpLanguage);
   }
   
    my $rv=parse_rv(do_cmd_quiet(@exec));


  unless ($rv->is_success()) {
   _error($rv->errstr("Opening help"));
    return 0;
  }

  1;
}

1;
