use strict;
use warnings;
use alienfile;
 
my $on_windows = $^O eq 'MSWin32';

plugin 'Probe::CommandLine' => (
  command => 'patchelf',
);
 
share {

  Alien::Build->log ('$ENV{ALIEN_BUILD_PRELOAD} = ' . ($ENV{ALIEN_BUILD_PRELOAD} // ''));

  #my $version_filter = have_cpp17() ? qr/^([0-9\.]+)$/ : qr/^(0.13.1)$/;
  #Alien::Build->log("version filter: $version_filter");

  meta->prop->{start_url} = 'https://github.com/NixOS/patchelf.git';
  plugin 'Download::Git' =>(
    filter  => get_version_filter(),
    version => qr/^([0-9\.]+)$/,
  );

  my $config_call = '%{configure}';
  requires 'Alien::Autotools';
  plugin 'Build::Autoconf';
  if ($on_windows) {
    plugin 'Build::MSYS';
  }
  if ($^O =~ /solaris/i) {
    plugin 'Build::Make' => 'gmake';
    $config_call = "MAKE=gmake $config_call";
  }
  build [
    'sh ./bootstrap.sh',  #  windows needs explicit shell call
    $config_call,
    '%{make}',
    '%{make} install',
  ];
};

#  Compile a c++ file to see if we can support c++17.
#  If not then fall back to an older version.
#  We could use ExtUtils::CBuilder but it is not
#  cooperating - patches welcome.
sub get_version_filter {
  my $default_filter = qr/^([0-9\.]+)$/;

  #  compiler might not be called c++
  use ExtUtils::CppGuess 0.25;

  my $cppg = ExtUtils::CppGuess->new()
    or return $default_filter;
  my $have_cpp_17 = eval {$cppg->cpp_standard_flag('C++17'); 1};

  meta->prop->{my_have_cpp17} = $have_cpp_17;
  if (!$have_cpp_17) {
    Alien::Build->log("The c++ compiler does not support -std=c++17, falling back to patchelf 0.13.1");
  }
  return $have_cpp_17 ? $default_filter : qr/^(0.13.1)$/
}