
use File::Find ;
use Cwd ;

$win32 = ($^O eq 'MSWin32') ;


if (1) # !$win32)
    {
    $path = $ENV{XALANPATH} || GetString ("Enter path of XALAN base directory or single dot for not using XALAN", $XALANPATH) ;

    if (!$path || $path eq '.')
        {
        print "Xalan will not be linked into Embperl\n" ;
        return undef ;
        }

    if (!-f "$path/src/XalanTransformer/XalanTransformer.hpp")
        {
        if (!-f "$path/c/src/XalanTransformer/XalanTransformer.hpp")
            {
            if (!-f "$path/xml-xalan/c/src/XalanTransformer/XalanTransformer.hpp")
                {
                print "Xalan sources not found\n" ;
                return undef ;
                }
            else
                {
                $path = "$path/xml-xalan/c/" ;
                }
            }
        else
            {
            $path = "$path/c/" ;
            }
        }

    my $currdir = Cwd::fastcwd ;

    eval { find (\&xalanlib, $win32?"$path/Build":"$path/lib") ; } ;
    $@ = '' ;
    chdir ($currdir);
    if (!$xalanlib)
        {
        print "Xalan library not found under $path\n" ;
        return undef ;
        }

    my $xpath = Cwd::fast_abs_path ("$path/../..") ;
    eval { find (\&xerceslib, $xpath) ; } ;
    $@ = '' ;
    chdir ($currdir);
    if (!$xerceslib)
        {
        print "Xerces library not found under $xpath/../..\n" ;
        return undef ;
        }


    print "Found Xalan library $xalanlib at $xalanlibpath\n" ;
    print "Found Xerces library $xerceslib at $xerceslibpath\n" ;

    my $platform  ;
    my $cpp       ;
    my $cppflags  ;
    my $makefile = "$path/src/Makefile" ;
    my $extralibs = '' ;

    if (-f $makefile)
        {
        $platform = search_config ('PLATFORM', $makefile) ;
        $cpp      = search_config ('CXX', $makefile) ;
        $cppflags = search_config ('CXXFLAGS', $makefile) ;
 
        $extralibs = '-lCrun -lCstd' if ($platform eq 'SOLARIS' && $cpp eq 'CC') ;

        print "Found XALAN was build for '$platform', C++ Compiler is $cpp, with flags $cppflags\n" ;
        }
    else
        {
        print "Makefile for XALAN not found. Assuming prebuild version using defaults\n" ;
        print "!!! NOTE: If you get compiler errors when build epxalan.cpp, try to build\n" ;
        print "!!!       XALAN from the sources. See INSTALL.pod\n\n" ;
        }


    "$xalanlib $xerceslib" =~ /^lib(.*?)\.so lib(.*?)\.so$/ ;

    return { cflags => "-I$path/src -I$xerceslibpath/../include -D_REENTRANT "  , 
             cpp    => $cpp,
             cppflags  => $cppflags,
             libs => "-L$xalanlibpath -L$xerceslibpath -l$1 -l$2 $extralibs", 
             defines => '-DXALAN' . ($platform?" -D$platform":'') ,
             objects => 'driver/epxalan$(OBJ_EXT)',
             save    => { '$XALANPATH' => $path }} ;
    }

print "Xalan is currently not supported on Win32\n" ;
return undef ;


sub xalanlib

    {
    if ($File::Find::dir =~ m#/\.#)
        {
        $File::Find::prune = 1 ;
        return ;
        } 

    if ((!$win32 && (/^libxalan.*\.so/)) || 
        ($win32  && (/^xalantransformer.*\.lib/i)) )
        {
        $xalanlibpath = $File::Find::dir ;
        die $xalanlib = $_ ;
        }
    }


sub xerceslib

    {
    if ($File::Find::dir =~ m#/\.#)
        {
        $File::Find::prune = 1 ;
        return ;
        } 

    if ((!$win32 && (/^libxerces.*\.so/)) || 
        ($win32  && (/^xerces.*\.lib/i)) )
        {
        $xerceslibpath = $File::Find::dir ;
        die $xerceslib = $_ ;
        }
    }

