use 5.008008;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.

my $lib;

# search system lib directories first
for ('/usr/lib', '/usr/lib64', '/usr/lib/x86_64-linux-gnu') {
    last if $lib;
    $lib = "-lJudy" if glob("$_/libJudy.*");
}
# and then some other likely places
for ('/usr/local/lib') {
    last if $lib;
    $lib = "-L$_ -lJudy" if glob("$_/libJudy.*");
}

die "cannot find libJudy" unless $lib;


WriteMakefile(
    NAME              => 'Bit::Vector::Judy',
    VERSION_FROM      => 'lib/Bit/Vector/Judy.pm', # finds $VERSION
    PREREQ_PM         => {}, # e.g., Module::Name => 1.1
    ($] >= 5.005 ?     ## Add these new keywords supported since 5.005
      (ABSTRACT_FROM  => 'lib/Bit/Vector/Judy.pm', # retrieve abstract from module
       AUTHOR         => 'Peter J. Holzer <hjp@hjp.at>') : ()),
    LIBS              => [$lib],
    DEFINE            => '', # e.g., '-DHAVE_SOMETHING'
    INC               => '-I.', # e.g., '-I. -I/usr/include/other'
	# Un-comment this if you add C files to link with later:
    # OBJECT            => '$(O_FILES)', # link all the C files too
);

# hack a workaround for a bug in xsubpp into the Makefile
open(my $fh, '<', 'Makefile') or die "cannot read Makefile: $!";
$/ = undef;
my $makefile = <$fh>;

$makefile =~ s{
    (\$\(XSUBPPRUN\) .*) \s && \s (\$\(MV\) \s \$\*.xsc \s \$\*.c)
}{$1 && perl -pe '\$\$_ = "\$\$1\\n\$\$2\\n" if /(.+)(#line \\d+ "Judy.xs")/' \$*.xsc > \$*.c}x;

open(my $fh, '>', 'Makefile') or die "cannot write Makefile: $!";
print $fh $makefile;
close($fh) or die "cannot write to Makefile: $!";
