use alienfile;
use Config;

# NOTE: you can set ALIEN_MESON_FROM_SOURCE to force a build from source.

plugin 'Probe::CommandLine' => (
  command => $_,
  args    => [ '--version' ],
  match   => qr/([0-9\.]+)/,
  version => qr/([0-9\.]+)/,
) for qw( meson.py meson );

share {
  requires 'Path::Tiny';
  requires 'File::Which';
  requires 'Capture::Tiny';
  requires 'Alien::Build::CommandSequence';
  requires 'File::Copy::Recursive';

  # same URL used for both source and binary releases
  start_url 'https://github.com/mesonbuild/meson/releases';

  my ($binary_release_name_re, $binary_release_format);
  if( $^O eq 'MSWin32' && $Config{ptrsize} == 8 ) {
    # Windows 64-bit
    $binary_release_name_re = qr/meson-.*-64\.msi/;
    $binary_release_format = '.msi';
  }

  if($binary_release_format && !$ENV{ALIEN_MESON_FROM_SOURCE}) {
    plugin Download => (
      filter  => $binary_release_name_re,
      version => qr/([0-9\.]+)/,
    );

    if( $binary_release_format eq '.msi' ) {
      extract sub {
        my ($build) = @_;

        my $msi = Path::Tiny::path($build->install_prop->{download})->canonpath;
        my $cwd = Path::Tiny->cwd->canonpath;

        Alien::Build::CommandSequence->new([
          qw(msiexec /a),
          $msi,
          "TARGETDIR=$cwd",
          '/qn'
        ])->execute($build);
      };

      patch sub {
        my $cwd = Path::Tiny->cwd;
        $_->remove for $cwd->children( qr/\.msi$/ );
        my $Meson = $cwd->child('Meson');
        $Meson->child('ninja.exe')->remove;
        File::Copy::Recursive::rmove( "$Meson/*", $cwd );
        $Meson->remove_tree;
      };

      plugin 'Build::Copy';

      after build => sub {
        my($build) = @_;
        $build->runtime_prop->{'style'} = 'binary';
        $build->runtime_prop->{command} = 'meson';
      };
    }
  } else {
    # Python source
    plugin Download => (
      filter  => qr/^meson-.*\.tar\.gz$/,
      version => qr/([0-9\.]+)/,
    );
    plugin 'Extract::CommandLine' => 'tar.gz';
    my $python_bin;
    PYTHON_BIN:
    for my $test_python_bin ( qw(python3 python) ) {
      if(
        File::Which::which($test_python_bin)
        &&
        Capture::Tiny::capture(sub {
              system( $test_python_bin, qw( --version ) )
        }) =~ /^Python 3/
      ) {
        $python_bin = $test_python_bin;
        last PYTHON_BIN;
      }
    }
    die "No Python3 found" unless defined $python_bin;
    patch sub {
      my @tests = Path::Tiny::path('.')->children( qr/.*test.*/ );
      $_->remove_tree for @tests;
      Path::Tiny::path('graphics')->remove_tree;
      Path::Tiny::path('setup.py')->remove_tree;
    };
    plugin 'Build::Copy';
    after build => sub {
      my($build) = @_;
      $build->runtime_prop->{'style'} = 'source';
      $build->runtime_prop->{'python-source'} = 1;
      $build->runtime_prop->{command} = 'meson.py';
      $build->runtime_prop->{python_bin} = $python_bin;
    };
  }

};

sys {
  meta->after_hook( probe => sub {
    my($build) = @_;
    $build->runtime_prop->{'style'} = 'system';
  });
};
