# Copyright (C) 2007, Parrot Foundation.
# $Id: parrot_embed_pl.in 39045 2009-05-22 16:17:19Z Infinoid $
#

use strict;
use warnings;

use Cwd;
use Config;
use File::Copy;
use ExtUtils::MakeMaker;

copy( 'lib/Parrot/Embed.xs', 'Embed.xs' );

# cross-platform commands and paths
my %config;
$config{CC}              = '@cc@';
$config{PARROTDIR}       = '../..';
$config{PARROT}          = '..@slash@..@slash@parrot@exe@';
$config{C_LIBS}          = '@libs@';
$config{INCLUDE}         = "$config{PARROTDIR}/include";
#UNLESS(win32):$config{ALL_PARROT_LIBS} = "@libparrot_linkflags@ $config{C_LIBS} @linkflags@";
$config{ABS_PARROTDIR}   = Cwd::realpath(
                               File::Spec->rel2abs( $config{PARROTDIR} ) );
$config{LDDLFLAGS}       = $Config{lddlflags};

if ($config{CC} eq 'cl')
{
    $config{LDDLFLAGS} .= qq| -libpath:"$config{ABS_PARROTDIR} "|
        .  File::Spec->catfile( $config{ABS_PARROTDIR}, 'libparrot.lib' );
}

WriteMakefile(
    'NAME'         => 'Parrot::Embed',
    'VERSION_FROM' => 'lib/Parrot/Embed.pm',
    'PREREQ_PM'    => { 'ExtUtils::CBuilder' => 0 },
#IF(win32):    'LIBS'         => [ $config{C_LIBS} ],
#ELSE:    'LIBS'         => [ $config{ALL_PARROT_LIBS} ],
#IF(win32):    'OBJECT'       => q|@libparrot_linkflags@ Embed@o@|,
    'INC'          => "-I$config{INCLUDE}",
    'PM'           => { map { $_ => "blib/$_" } <lib/Parrot/*pm> },
    'clean'        => { FILES => '*.xs t/greet.pbc' },
    'LDDLFLAGS'    => $config{LDDLFLAGS},
);

package MY;

sub postamble
{
    "t/greet.pbc:\n\t$config{PARROT} -o t/greet.pbc t/greet.pir\n";
}

sub test
{
    my $inherited   = shift->SUPER::test(@_);
    return $inherited unless $config{cc} eq 'cl';

    my $dynlib_path = "\tset PATH=%PATH%:$config{ABS_PARROTDIR}";
    $inherited      =~ s{^(test.*ic ::.*)$}{$1\n$dynlib_path}mg;

    return $inherited;
}

sub dynamic_lib
{
    my $inherited  = shift->SUPER::dynamic_lib(@_);
    my $sub_target = quotemeta( ': $(OBJECT)' );
    $inherited     =~ s{($sub_target)}{$1 t/greet.pbc};
    $inherited;
}

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
