use alienfile;
use Config;

configure { requires 'Alien::Build::Plugin::Build::Make' => '0.01' };

plugin 'PkgConfig' => (
    pkg_name => 'libuv',
    minimum_version => '1.0.0',
);

share {
    # note on apple weirdisms: https://github.com/joyent/libuv/issues/1200
    meta->prop->{env}->{LIBTOOLIZE} = 'libtoolize' if $^O eq 'darwin';

    plugin Download => (
        url     => 'https://dist.libuv.org/dist/v1.28.0',
        version => qr/^libuv-v([0-9\.]+)\.tar\.gz$/,
    );

    plugin Extract => 'tar.gz';

    if($^O eq 'MSWin32') {
        my $bits = $Config{archname} =~ /^MSWin32-x64/ ? 64 : 32;
        requires 'Path::Tiny';
        plugin 'Build::Make' => 'gmake';
        plugin 'Build::CMake';

        my @args = (
            -G => '%{cmake_generator}',
            '-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true',
            '-DCMAKE_INSTALL_PREFIX:PATH=%{.install.prefix}',
            '-DCMAKE_MAKE_PROGRAM:PATH=%{make}',
        );

        meta->before_hook(build => sub {
            my($build) = @_;
            my $prefix = $build->install_prop->{prefix};
            $prefix =~ s{/}{\\}g;
            meta->interpolator->add_helper(prefix_win => sub { $prefix });
        });

        meta->after_hook(gather_share => sub {
            my $build= shift;
            my $ver = '0x0601'; # win7+ compat
            my $flags = '-DWIN32 -D_WIN32';
            $flags .= ' -DWIN64 -D_WIN64' if $bits == 64;
            $flags .= " -D_WIN32_WINNT=$ver -DWINVER=$ver";
            $build->runtime_prop->{$_} .= " $flags" for qw( cflags cflags_static );
            # on windows, we need the following libraries to be included. MinGW can't pull these
            # from source on windows, so we add the equivalent to these pragma comments
            # to our libs/libs_static area:
            #pragma comment(lib, "Advapi32.lib")
            #pragma comment(lib, "IPHLPAPI.lib")
            #pragma comment(lib, "kernel32.lib")
            #pragma comment(lib, "Psapi.lib")
            #pragma comment(lib, "Shell32.lib")
            #pragma comment(lib, "User32.lib")
            #pragma comment(lib, "Userenv.lib")
            #pragma comment(lib, "Ws2_32.lib")
            $build->runtime_prop->{$_} .= ' -msse2 -ladvapi32 -lIphlpapi -lkernel32 -lpsapi -lshell32 -luser32 -luserenv -lws2_32' for qw( libs libs_static );
        });

        build [
            ['%{cmake}', @args, '%{.install.extract}' ],
            ['%{make}' ],
            'mkdir %{prefix_win}\\lib',
            'mkdir %{prefix_win}\\lib\\pkgconfig',
            'copy libuv_a.a %{prefix_win}\\lib\\libuv.a',
            'copy libuv.dll %{prefix_win}\\lib',
            'copy libuv.dll.a %{prefix_win}\\lib',
            'mkdir %{prefix_win}\\include',
            'mkdir %{prefix_win}\\include\\uv',
            'copy include\\*.h %{prefix_win}\\include',
            'copy include\\uv\\*.h %{prefix_win}\\include\\uv',
            sub {
                my($build) = @_;
                my($pc) = Path::Tiny->new('libuv.pc.in')->slurp;

                my $prefix = $build->runtime_prop->{prefix};
                my $version = $build->runtime_prop->{version};

                $pc =~ s{\@prefix\@}{$prefix}g;
                $pc =~ s{\@libdir\@}{$prefix/lib}g;
                $pc =~ s{\@includedir\@}{$prefix/include}g;
                $pc =~ s{\@PACKAGE_NAME\@}{libuv}g;
                $pc =~ s{\@PACKAGE_VERSION\@}{$version}g;
                $pc =~ s{\@LIBS\@}{}g;

                Path::Tiny->new($build->install_prop->{prefix})->child('lib/pkgconfig/libuv.pc')->spew($pc);
            },
        ];
    }
    else {
        requires 'Alien::Autotools';
        plugin 'Build::Autoconf' => ();

        build [
            'sh autogen.sh',
            '%{configure}',
            '%{make}',
            '%{make} test',
            '%{make} install',
        ];
    }

    plugin 'Gather::IsolateDynamic' => ();
};