use alienfile;

probe sub { 'share' };

share {
  requires 'Alien::autoconf' => '0.02';
  requires 'Alien::m4'       => '0.11';
  requires 'Path::Tiny'      => '0.077';

  plugin Download => (
    url => 'ftp://ftp.gnu.org/gnu/automake',
    filter => qr/^automake-.*\.tar\.gz$/,
    version => qr/([0-9\.]+)/,
  );
  plugin Extract => 'tar.gz',
  
  patch sub {
    my($build) = @_;

    # https://debbugs.gnu.org/cgi/bugreport.cgi?bug=20903
    # bug in automake: it puts the @datadir@ in double quotes
    # in one place in bin/aclocal, which causes Perl to barf
    # if prefix has an at sign (@) in it.  This is commonly
    # the case when using perlbrew.    
    Path::Tiny->new('bin/aclocal.in')->edit_lines(sub {
      s/"\@datadir\@/'\@datadir\@'."/g;
    });
  };
  
  plugin 'Build::Autoconf' => ();
  
  requires 'IPC::Cmd';
  requires 'Capture::Tiny';
  
  meta->before_hook(build => sub {
    my $autoconf_bin = IPC::Cmd::can_run('autoconf');
    print "***************************************************\n";
    print "autoconf path = @{[ defined $autoconf_bin ? $autoconf_bin : 'undef' ]}\n";
    if($autoconf_bin)
    {
      my($version) = Capture::Tiny::capture_merged(sub {
        system $autoconf_bin, '--version';
      });
      print "[version]\n";
      print "$version\n";
    }
    print "***************************************************\n";
  });
};
