use ExtUtils::MakeMaker;
use Env qw(XERCES_LIB XERCES_INCLUDE XERCESCROOT XERCES_DEVEL SWIG);
use strict;
use vars qw($INCLUDES);

if (defined $XERCES_LIB || defined $XERCES_INCLUDE) {
  # this is the default, so do nothing
} elsif (defined $XERCESCROOT) {
  # they are instructing us to use the build directories
  $XERCES_INCLUDE = "$XERCESCROOT/include\n";
  $XERCES_LIB = "$XERCESCROOT/lib\n";
} 

my $LIBS = '';
$INCLUDES = '-I. -IHandler';
if (defined $XERCES_LIB) {
  print STDERR "Using XERCES_LIB = $XERCES_LIB";
  $LIBS = "-L$XERCES_LIB ";
}

if (defined $XERCES_INCLUDE) {
  print STDERR "Using XERCES_INCLUDE = $XERCES_INCLUDE";
  $INCLUDES .= " -I$XERCES_INCLUDE";
}

if (!defined $XERCES_LIB && !defined $XERCES_INCLUDE) {
  print STDERR <<EOW;

   WARNING

You have not defined any of the following environment variables:
   XERCESCROOT
   XERCES_LIB
   XERCES_INCLUDE

These instruct me how to locate the Xerces header files, and the
Xerces dynamic library. If they are installed in a standard system
directory, I will located them without those variables.

However, if they have been installed in a non-standard location
(e.g. '/usr/include/xerces'), then I will need help. See the README
for more info.

Proceeding ...
EOW
}

# We need to know which version of libxerces we are to use
my $XERCES_MAJOR_VERSION = '1.5';
# this substitution will yield '1_4' from '1.4' to be used by 'LIBS'
my $XERCES_LIB_VERSION = $XERCES_MAJOR_VERSION;
$XERCES_LIB_VERSION =~ s/\./_/;
my $LIBXERCES = "-lxerces-c$XERCES_LIB_VERSION";
$LIBS .= " $LIBXERCES";

# now we ensure that libxerces is in the library path
# if not we die()
print STDERR "Checking to see if libxerces is in your library path...";
my $lib;
{
  # suppress any messages from Liblist::ext()
  local *STDOUT;
  local *STDERR;
#  open(STDERR, ">/dev/null");
#  open(STDOUT, ">/dev/null");
  ($lib) = MM->new->ExtUtils::Liblist::ext($LIBS);
}
die <<EOE unless $lib;


    !!!WHOA!!!

I couldn\'t find $LIBXERCES anywhere in your library path. Begging to
differ with perl, this is most assuredly *NOT* harmless. This is a
critical error that will prevent you from running Xerces.pm.

Check your settings of \$XERCES_INCLUDE and \$XERCES_LIB, and rerun
'perl Makefile.PL'

EOE

print STDERR "Success!!\n";

# give some nice feedback for the user
print STDERR "Using Xerces-C version: $XERCES_MAJOR_VERSION\n";

my $HANDLER_LIB = '$(INST_ARCHLIB)/auto/Handler/Handler$(LIB_EXT)';

sub MY_postamble {
qq[

$HANDLER_LIB:
	cd Handler && make static
];

}
*MY::postamble = \&MY_postamble;

# we only consider using SWIG if we are a Xerces Developer
my @MACRO;
my $CFLAGS = '-D_REENTRANT -DDEBIAN';
if ($XERCES_DEVEL) {
  # replace this with the path to your Unix compatible find application
  my $FIND = 'find';
  my @handler_headers;
  chomp(@handler_headers = `$FIND Handler -name "*.swig.hpp"`);

  sub Xerces_postamble {
    chomp(my @header_files = `$FIND Xerces_headers -name "*.hpp"`);
    push(@header_files,@handler_headers);
    local $" = ' ';
    <<TERMINUS;
Xerces.C Xerces.pm: Xerces.i @header_files postSource.pl postModule.pl
	\$(SWIG) -I. -ILib -ILib/perl5 \$(INC) -D\$(SWIG_VERSION) -package XML::Xerces -perl5 -c++ -shadow -o Xerces.C Xerces.i
	perl postSource.pl Xerces.C
	perl postModule.pl Xerces.pm


$HANDLER_LIB:
	cd Handler && make static
TERMINUS
  }

  print STDERR "Welcome Xerces Developer!\n";
  # if we're to use SWIG, we need to know which version is available
  my $swig = $SWIG || 'swig';
  my ($sv_maj,$sv_min) = qx[$swig -version 2>&1] =~ /(\d+)\.(\d+)/;
  @MACRO = ('macro'       => {
    'SWIG'         => "$swig",
    'SWIG_VERSION' => "SWIG_$ {sv_maj}_$ {sv_min}",
  });
  $CFLAGS .= ' -D$(SWIG_VERSION)';
  print STDERR "Using SWIG version: SWIG_$ {sv_maj}_$ {sv_min}\n";
  *MY::postamble = \&Xerces_postamble;
}

# set up the list of object files to include in Xerces\$(OBJ_EXT)
my $OBJS = 'Xerces$(OBJ_EXT)';

# write the makefile
WriteMakefile(
  'AUTHOR'      => 'The Xerces-P developers',
  'ABSTRACT'    => 'Perl Interface for Xerces XML API',
  'CCFLAGS'     => $CFLAGS,
  'PM'          => {
    'Xerces.pm' => '$(INST_LIB)/XML/Xerces.pm',
    'DOMParse/DOMParse.pm' => '$(INST_LIB)/XML/Xerces/DOMParse.pm',
  },
  'INC'         => $INCLUDES,
  'MYEXTLIB'    => $HANDLER_LIB,
  'LIBS'        => [$LIBS],
  'NAME'        => 'XML::Xerces',
  'OBJECT'      => $OBJS,
  'DIR'         => ['DOMParse'],
  'VERSION'     => "$XERCES_MAJOR_VERSION.3",
  @MACRO
);




jas.

