use alienfile;
use Capture::Tiny qw( capture );
use Config;

configure { requires 'Capture::Tiny' };

plugin 'Probe::CommandLine' => (
  command => 'nasm',
  args    => ['-v'],
  match   => qr/NASM version/,
);

plugin 'Probe::CommandLine' => (
  command      => 'ndisasm',
  args         => ['-v'],
  match_stderr => qr/NDISASM version/,
  secondary    => 1,
);

share {

  requires 'Path::Tiny';
  plugin 'Fetch::HTTPTiny' => 'http://www.nasm.us/pub/nasm/releasebuilds';
  plugin 'Decode::Mojo' => ();
  plugin 'Prefer::SortVersions' => ();

  download sub {
    my($build) = @_;
    
    my $res = $build->decode($build->fetch('http://www.nasm.us/pub/nasm/releasebuilds'));

    $res->{list} = [grep { $_->{filename} =~ /^[0-9\.]+$/ } @{ $res->{list} }];
    
    $res = $build->prefer($res);
    
    die "unable to find first level candidate"
      unless @{ $res->{list} } > 0;

    my $url;
    my $regex;

    if($^O eq 'MSWin32')
    {
      my $bits = $Config{ptrsize} == 4 ? '32' : '64';
      $url   = $res->{list}->[0]->{url} . "/win$bits";
      $regex = qr/^nasm-[0-9\.]+-win$bits\.zip$/;
    }
    else
    {
      $url = $res->{list}->[0]->{url};
      $regex = qr/^nasm-[0-9\.]+\.tar\.gz$/;
    }
    
    $res = $build->decode($build->fetch($url));
    
    $res->{list} = [grep { $_->{filename} =~ $regex } @{ $res->{list} }];

    $res = $build->prefer($res);

    die "unable to find second level candidate"
      unless @{ $res->{list} } > 0;
    
    $res = $build->fetch($res->{list}->[0]->{url});
    Path::Tiny->new($res->{filename})->spew_raw($res->{content});
    $build->runtime_prop->{command} = 'nasm';
  };

  if($^O eq 'MSWin32')
  {
    requires 'Path::Tiny';
    plugin 'Extract' => 'zip';
    build sub {
      my($build) = @_;
      my $dest = Path::Tiny->new($build->install_prop->{prefix})->child('bin');
      $dest->mkpath;
      foreach my $exe (qw( nasm.exe ndisasm.exe ))
      {
        Path::Tiny->new($exe)->copy($dest->child($exe));
      }
    };
  }
  else
  {
    plugin 'Extract' => 'tar.gz';  
    plugin 'Build::MSYS' => ();

    build [
      'sh configure --prefix=%{alien.install.prefix}',
      '%{gmake}',
      '%{gmake} install',
    ];
  }
};

gather sub {
  my($build) = @_;
  my($stdout) = capture {
    system($build->runtime_prop->{command}, '-v');
  };
  my($version) = $stdout =~ /NASM version ([0-9\.]+)/;
  $build->runtime_prop->{version} = $version || 'unknown';
};

