use alienfile;
use Config;

sub WINVER() {
    return undef unless $^O eq 'MSWin32';
    my $ver;
    my $err = do {
        local $@;
        eval { require Win32; $ver = Win32::GetOSName(); 1; };
        $@;
    };
    $ver = 'Win10' unless $ver; # just pick one if we couldn't find it
    if ($err) {
        warn "We had an error grabbing the Win32 OS Name: $err";
    }

    if ($ver =~ /^Win(?:Win32s|95|98|Me|NT4|NT3\.51|HomeSvr)/) {
        warn "This version of Windows is really old and we can't install here";
        exit(1);
    }
    return '0x0500' if $ver =~ /^Win2000/;
    return '0x0501' if $ver =~ /^WinXP\/\.Net/;
    return '0x0502' if $ver =~ /^Win2003/;
    return '0x0600' if $ver =~ /^Win(?:Vista|2008)/;
    return '0x0601' if $ver =~ /^Win7/;
    return '0x0603' if $ver =~ /^Win8\.1/;
    return '0x0602' if $ver =~ /^Win8/;
    return '0x0A00' if $ver =~ /^Win10/;
    return '0x0A00' if $ver =~ /^Win(?:2012|2014|2016)/; # not sure here

    warn "We couldn't determine the version of Windows you're on.";
    return undef;
}

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.20.3',
    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';

    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 $flags = '-DWIN32';
        $flags .= ' -DWIN64' if $bits == 64;
        if (my $ver = WINVER()) {
            $flags .= " -D_WINVER=$ver -D_WIN32_WINNT=$ver"
        }
        $build->runtime_prop->{$_} .= " $flags" for qw( cflags cflags_static );
        $build->runtime_prop->{$_} .= ' -lpsapi -luserenv -lIphlpapi' for qw( libs libs_static );
    });

    build [
      '%{make} -f Makefile.mingw CC=%{perl.config.cc}',
      'mkdir %{prefix_win}\\lib',
      'mkdir %{prefix_win}\\lib\\pkgconfig',
      'copy libuv.a %{prefix_win}\\lib',
      'mkdir %{prefix_win}\\include',
      'copy include\\*.h %{prefix_win}\\include',
      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' => ();
};