=head1 NAME

CCL::Parser::Validation - Perl interface to cclapi's validation

=head1 SYNOPSIS

  use CCL::Parser::Validation;
  CCL::Parser::Validation::validateObject( $liteApi, $path );

=head1 DESCRIPTION

This is a Perl interface to the calls in cclValidation.h for running
the validation code against a lite API handle.

This is used internally by the heavy API, and is not intended for
direct use.

=head1 AUTHOR

Andy Mortimer

=cut

package CCL::Parser::Validation;

require 5.005_62;
use strict;
use warnings;
use Config;

#require DynaLoader;

#our @ISA = qw(DynaLoader);

our $VERSION = '0.01';

# if we are being called from a program linked against the cclApi
# files, we hope the interface will already be linked in; we try to be
# careful about having two copies, because of issues with memory
# allocated and freed in different DLLs on Windows.  So we implement
# our own bootstrapping, using dynaloader, in case we're being called
# from Perl.
eval {
  local $SIG{__DIE__};
  bootstrap CCL::Parser::Validation $VERSION;
};
if ($@ =~ m!Can't locate object method "bootstrap"!) {
  # OK, it's not linked in (or it went a bit wrong ...).  Do it
  # ourselves.  If you get errors here and you're not trying to access
  # the API from a standard Perl interpreter, check your xsinit.c.

  # first, check to see if it looks like we're in a standard Perl
  # exec, or one of the Windows cfx5*.exe wrapped scripts.  If not,
  # the stuff should already be linked in.
  if ($^X !~ m!perl[^\\/]*$!o and $^X !~ m!cfx5[^\\/]*$!) {
    warn "Attempting to dynamically load ", __PACKAGE__, 
      " from custom executable $^X.  This shouldn't happen.  ",
        "Check your xsinit.c.";
  }

  my $libdir=$ENV{CFX5_CCLAPI_LIBDIR} || $ENV{CFX5LIBDIR};
  die "CFX5LIBDIR unset, unable to auto-load ", __PACKAGE__
    unless (defined $libdir);
  die "CFX5LIBDIR directory $libdir does not exist, unable to autoload ", __PACKAGE__
    unless -d $libdir;

  my %exts=(
            'MSWin32-x86-perlio' => 'dll',
	    'MSWin32-AMD64-perlio' => 'dll',
	    'MSWin32-x64-perlio' => 'dll',
            'MSWin32-x86-multi-thread' => 'dll',
            'MSWin32-x64-multi-thread' => 'dll',
            MSWin32 => 'dll',
            'PA-RISC2.0-LP64' => 'sl',
            hpux => 'sl',
           );
  my $ext=$exts{$Config{archname}} || 'so';
  my $libfile="$libdir/CCL_Parser_Validation.$ext";
  die "$libfile missing, unable to autoload ", __PACKAGE__
    unless -f $libfile;

  require DynaLoader;
  my $lib=DynaLoader::dl_load_file($libfile, DynaLoader::dl_load_flags());
  die "Error loading library $libfile: ", DynaLoader::dl_error()
    unless $lib;

  my $sym=DynaLoader::dl_find_symbol($lib, 'boot_CCL__Parser__Validation');
  die "Error finding bootstrap routine boot_CCL__Parser__Validation in $libfile: ", DynaLoader::dl_error()
    unless $sym;

  DynaLoader::dl_install_xsub(__PACKAGE__ . "::bootstrap", $sym, __FILE__);

  eval {
    local $SIG{__DIE__};
    bootstrap CCL::Parser::Validation $VERSION;
  };
  if ($@) {
    die "Error bootstrapping ", __PACKAGE__, " even after loading the bootstrap routine: $@";
  }
}

1;
