use alienfile;
#use File::Which qw( which );
#use Capture::Tiny qw( capture );
#use List::Util qw( all );
#use Sort::Versions qw( versioncmp );

#configure {
#  requires 'File::Which';
#  requires 'Capture::Tiny';
#  requires 'Sort::Versions';
#};

probe sub {
  my($build) = @_;

#  my %tools;
#  
#  foreach my $tool (qw( autoconf autoheader autom4te autoreconf autoscan autoupdate ifnames ))
#  {
#    $tools{$tool} = 0;
#    my $exe = which $tool;
#    if($exe)
#    {
#      my(undef, undef, $stdout) = capture {
#        `$tool --version`;
#      };
#      
#      if($stdout =~ /GNU Autoconf/ && $stdout =~ /([0-9\.]+)$/)
#      {
#        my $version = $1;
#        if(versioncmp($version, '2.69') >= 0)
#        {
#          $tools{$tool} = $version;
#        }
#      }
#    }
#  }
#
#  if(all { $_ != 0 } values %tools)
#  {
#    my($version) = values %tools;
#    if(all { $_ == $version } values %tools)
#    {
#      $build->install_prop->{my_version} = $version;
#      return 'system';
#    }
#  }

  'share';
};

share {
  requires 'Alien::m4'   => '0.11';
  requires 'Path::Tiny'  => 0;

  if($^O eq 'MSWin32')
  {
    meta->prop->{env}->{PERL} = '/usr/bin/perl';
  }
  else
  {
    meta->prop->{env}->{PERL} = $^X;
  }

  plugin Download => (
    url => 'http://ftp.gnu.org/gnu/autoconf',
    filter => qr/^autoconf-.*\.tar\.gz$/,
    version => qr/([0-9\.]+)/,
  );
  plugin Extract => 'tar.gz',
  
  plugin 'Build::Autoconf' => ( msys_version => '0.08' );
  
  meta->before_hook(
    gather_share => sub {
      my($build) = @_;
      my $bin = Path::Tiny->new('bin');

      # patch executables
      foreach my $exe ($bin->children)
      {
        my($shebang, @lines) = $exe->lines;
        if($shebang =~ /^#!.*perl/)
        {
          @lines = (
            "BEGIN {\n",
            "  use strict;\n",
            "  use warnings;\n",
            "  use File::Spec;\n",
            "  my(\$v,\$d) = File::Spec->splitpath(File::Spec->rel2abs(__FILE__));\n",
            "  my \@d = File::Spec->splitdir(\$d);\n",
            "  pop \@d for 1..2;\n",
            "  my \$dist_dir = File::Spec->catpath(\$v,File::Spec->catdir(\@d), '');\n",
            "  \$ENV{AUTOM4TE}            ||= File::Spec->catfile(\$dist_dir, 'bin/autom4te');\n",
            "  \$ENV{autom4te_perllibdir} ||= File::Spec->catdir(\$dist_dir, 'share/autoconf');\n",
            "  \$ENV{AC_MACRODIR}         ||= File::Spec->catdir(\$dist_dir, 'share/autoconf');\n",
            "  \$ENV{AUTOCONF}            ||= File::Spec->catfile(\$dist_dir, 'bin/autoconf');\n",
            "  \$ENV{AUTOHEADER}          ||= File::Spec->catfile(\$dist_dir, 'bin/autoheader');\n",
            "  \$ENV{AUTOM4TE_CFG}        ||= File::Spec->catfile(\$dist_dir, 'share/autoconf/autom4te.blib.cfg')\n",
            "    if \$d[-5] eq 'lib' && \$d[-6] eq 'blib'\n",
            "}\n",
            @lines
          );
          $exe->spew($shebang, @lines);
          $exe->chmod("0755");
        }
        elsif($shebang =~ /^#!.*sh/)
        {
          @lines = (
            ": \${AUTOM4TE=`alien_autoconf_root`'/bin/autom4te'}\n",
            @lines
          );
          $exe->spew($shebang, @lines);
          $exe->chmod("0755");
        }
      }
      
      # patch autom4te config
      {
        my $orig = Path::Tiny->new('share/autoconf/autom4te.cfg');
        my $new  = Path::Tiny->new('share/autoconf/autom4te.blib.cfg');
        my $stage = Path::Tiny->new($build->install_prop->{stage})->child('share/autoconf')->stringify;
        $orig->copy($new);
        $new->edit_lines(sub {
          s/--prepend-include '(.*)'/--prepend-include '$stage'/;
        });
      }
      
      # script to compute path to autoconf root
      {
        my $exe = Path::Tiny->new('bin/alien_autoconf_root');
        my $perl = $ENV{PERL} || '/usr/bin/perl';
        $exe->spew(
          "#!$perl\n",
          "use File::Spec;\n",
          "my(\$v,\$d) = File::Spec->splitpath(File::Spec->rel2abs(__FILE__));\n",
          "my \@d = File::Spec->splitdir(\$d);\n",
          "pop \@d for 1..2;\n",
          "my \$dist_dir = File::Spec->catpath(\$v,File::Spec->catdir(\@d), '');\n",
          "print \$dist_dir\n",
        );
        $exe->chmod('0700');
      }
    },
  );
};

#sys {
#
#  gather sub {
#    my($build) = @_;
#    $build->runtime_prop->{version} = $build->install_prop->{my_version};
#  };
#
#};
