package Alien::Autotools;

use strict;
use warnings;
use base qw( Exporter );
use File::Which ();
use Path::Tiny  ();
use Alien::autoconf 0.18;
use Alien::automake 0.18;
use Alien::libtool  0.15;
use Alien::m4       0.18;

our @EXPORT_OK = qw(autoconf_dir automake_dir libtool_dir);

# ABSTRACT: Build and install the GNU build system.
our $VERSION = '1.08'; # VERSION


sub bin_dir
{
  my @dir = map { $_->bin_dir }
            map { "Alien::$_" }
            qw( autoconf automake libtool m4 );
  @dir;
}

sub _dir_from_exe
{
  my($name) = @_;
  my $path = File::Which::which($name);
  die "unable to find $name in PATH" unless $path;
  Path::Tiny->new($path)->parent->stringify;
}

my %helper;

sub alien_helper
{
  return \%helper if keys %helper;

  foreach my $alien ( map { "Alien::$_" } qw( autoconf automake libtool m4 ))
  {
    my %sub = %{ $alien->alien_helper };
    %helper = (%helper, %sub);
  }

  return \%helper;
}


sub aclocal_dir
{
  my @dir;
  foreach my $alien (map { "Alien::$_" } qw( autoconf automake libtool m4 ))
  {
    my $dir = Path::Tiny->new($alien->dist_dir)->child(qw( share aclocal ));
    push @dir, $dir if -d $dir;
  }
  @dir;
}


sub versions
{
  my %ver;
  foreach my $alien (qw( autoconf automake libtool m4 ))
  {
    my $class = "Alien::$alien";
    $ver{$alien} = $class->version;
  }
  %ver;
}


sub autoconf_dir ()
{
  my($dir) = Alien::autoconf->bin_dir;
  $dir
    ? $dir
    : _dir_from_exe('autoconf');
}


sub automake_dir ()
{
  my($dir) = Alien::automake->bin_dir;
  $dir
    ? $dir
    : _dir_from_exe('automake');
}


sub libtool_dir ()
{
  my($dir) = Alien::libtool->bin_dir;
  $dir
    ? $dir
    : _dir_from_exe('libtool');
}

sub cflags       {}
sub libs         {}
sub dynamic_libs {}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Alien::Autotools - Build and install the GNU build system.

=head1 VERSION

version 1.08

=head1 SYNOPSIS

From Perl:

 use Alien::Autotools;
 use Env qw( @PATH @ACLOCAL_PATH );
 
 unshift @PATH, Alien::Autotools->bin_dir;
 unshift @ACLOCAL_PATH, Alien::Autotools->aclocal_dir;
 
 system 'autoconf', ...;

From L<alienfile>:

 use alienfile;
 
 share {
   # Alien::Autotools will pull in:
   #  - Alien::autoconf
   #  - Alien::automake
   #  - Alien::m4
   #  - Alien::libtool
   # all of which you will likely need.
   requires 'Alien::Autotools';
   plugin 'Build::Autoconf';
   build [
     '%{autoreconf} -vfi',
     '%{configure}',
     '%{make}',
     '%{make} install',
   ];
 };

=head1 DESCRIPTION

This L<Alien> provides the minimum tools requires for building C<autoconf> based projects
which do not come bundled with a working C<configure> script.  It currently delegates
most of its responsibilities to L<Alien::autoconf>, L<Alien::automake>, L<Alien::libtool>,
and L<Alien::m4>.

The most common use case from an L<alienfile> is shown above where C<autoreconf> is called
from this L<Alien>, which allows the L<Alien::Build::Plugin::Build::Autoconf> to then
configure and build the alienized package.

=head1 METHODS

=head2 bin_dir

 my @dirs = Alien::Autotools->bin_dir;

Returns the list of directories that need to be added to C<PATH> in order for the autotools
to work correctly.

=head2 aclocal_dir

 my @dirs = Alien::Autotools->aclocal_dir;

Returns the list of directories that need to be added to C<ACLOCAL_PATH> in order for the
autotools to work correctly.

=head2 versions

 my %versions = Alien::Autotools->versions;

Returns the versions of the various autotools that are available.

=head2 autoconf_dir

 # legacy interface
 use Alien:::Autotools qw( autoconf_dir );
 my $dir = autoconf_dir;

Returns the directory path to autoconf

=head2 automake_dir

 # legacy interface
 use Alien:::Autotools qw( automake_dir );
 my $dir = automake_dir;

Returns the directory path to automake

=head2 libtool_dir

 # legacy interface
 use Alien:::Autotools qw( libtool_dir );
 my $dir = libtool_dir;

Returns the directory path to libtool

=head1 CAVEATS

This module is typically needed for other L<Alien>s for a share install that use the
autotools / GNU build system without bundling a pre-built C<configure> script.  If
possible it is better to use a version of the alienized package that includes a
pre-built C<configure> script.

If you are a system vendor, then you should typically not need to package this module,
check to see if the dependency that requires it can be built as a system install
instead.

=head1 HELPERS

This L<Alien> provides all of the helpers provides by L<Alien::m4>, L<Alien::autoconf>,
L<Alien::automake> and L<Alien::libtool>.  Each helper will execute the corresponding
command.  You will want to sue the helpers instead of using the command names directly
because they will use the correct incantation on Windows.  The following list is a
subset of all of the helpers provided by this alien that are probably the most useful.

=over 4

=item m4

=item autoreconf

=item automake

=item libtool

=item libtoolize

=back

=head1 SEE ALSO

=over 4

=item L<Alien>

=item L<Alien::Build>

=item L<alienfile>

=item L<Alien::autoconf>

=item L<Alien::automake>

=item L<Alien::libtool>

=item L<Alien::m4>

=back

=head1 AUTHOR

Original author: Richard Simões

Current maintainer: Graham Ollis E<lt>plicease@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2012-2022 by Richard Simões.

This is free software, licensed under:

  The GNU Lesser General Public License, Version 3, June 2007

=cut
