use 5.006;
use strict;
use warnings;
use Alien::Base::ModuleBuild;

use Config;

my $image_magick_version = 'latest';
if ($ENV{PERL_ALIEN_IMAGEMAGICK_VERSION}) {
    if ($ENV{PERL_ALIEN_IMAGEMAGICK_VERSION} =~ /\A(?:latest|[0-9]+(?:\.[0-9]+){2}(?:\-[0-9]+)?)\z/) {
        $image_magick_version = $ENV{PERL_ALIEN_IMAGEMAGICK_VERSION};
    }
    else {
        warn 'ignoring invalid environment variable PERL_ALIEN_IMAGEMAGICK_VERSION';
    }
}

## GitHub source tarballs are per-tag, so there is no evergreen 'latest'
## URL like imagemagick.org's own archive used to provide. Resolve the
## actual latest release tag from the redirect that
## github.com/.../releases/latest issues, rather than the releases API,
## which is subject to a much stricter (and easily exhausted) rate limit
## for unauthenticated requests.
if ($image_magick_version eq 'latest') {
    $image_magick_version = _latest_imagemagick_release_tag();
}

sub _latest_imagemagick_release_tag {
    require HTTP::Tiny;
    my $url = 'https://github.com/ImageMagick/ImageMagick/releases/latest';
    my $response = HTTP::Tiny->new(timeout => 10, max_redirect => 0)->get(
        $url,
        { headers => { 'User-Agent' => 'Alien-ImageMagick-Build.PL' } },
    );
    my $location = $response->{headers}{location} // '';
    die "Unable to determine the latest ImageMagick version from $url (status $response->{status} $response->{reason})\n"
        unless $location =~ m{/releases/tag/([^/]+)\z};
    return $1;
}

my $perlbin = $Config{perlpath};
my $perlversion = $Config{version};
my $siteprefix = $Config{siteprefix};
my $dataprefix = $Config{siteprefix} . '/etc/';

my $core_lib =  $Config{archlib}.'/CORE/';
my $use_threads = $Config{usethreads} ? '' : '--without-threads';
my $enable_shared = ( ( $Config{useshrplib} || '' ) eq 'false' ) ? '' : '--enable-shared';
my $LDFLAGS= '-L'.$core_lib;

my ($ldconfig) = grep { -x } map { "$_/ldconfig" } split(/:/, $ENV{PATH});
$ldconfig = "$ldconfig || true" if $ldconfig;

my $builder = Alien::Base::ModuleBuild->new(
    module_name         => 'Alien::ImageMagick',
    license             => 'perl',
    dist_author         => q{Jerome Eteve <jerome.eteve@gmail.com>},
    dist_version_from   => 'lib/Alien/ImageMagick.pm',
    configure_requires => {
        'Alien::Base::ModuleBuild' => 1.15,
        'Module::Build' => 0.38,
    },
    build_requires => {
        'Test::More' => 1.001014,
        'Alien::Base' => 0.009,
        'IO::Socket::SSL' => 2.070,
    },
    requires => {
        'perl' => 5.006,
        'Alien::Base' => 0.009,
    },
    add_to_cleanup      => [ 'Alien-ImageMagick-*' ],

    alien_name => 'MagickWand',
    alien_repository => {
        protocol       => 'https',
        host           => 'github.com',
        location       => '/ImageMagick/ImageMagick/archive/refs/tags/',
        exact_filename => $image_magick_version . '.tar.gz',
    },

    alien_install_type => 'share',

    ## For development, to save the download time,
    ## download the package once and put it in an 'inc' directory.
    # alien_repository => {
    #                     protocol => 'local',
    #                     location => 'inc',
    #                     exact_filename => 'ImageMagick.tar.gz',
    #                    },
    alien_build_commands => [
                             ## This siteprefix will install the execs in perl's /bin and the libs in perl's lib/
                             'LDFLAGS='.$LDFLAGS.' %c --prefix=%s --sysconfdir='.$dataprefix.' --exec-prefix='.$siteprefix.' --with-perl='.$perlbin.' '.$enable_shared.' '.$use_threads . ' --with-modules',

                             # Patch the PerlMagick makefiles to reflect lib installation places.
                             # This is because ImageMagic's PerlMagick package comes with Makefile.PL files
                             # containing a relative reference to the build directory ../magick/.libs
                             #
                             # This causes https://rt.cpan.org/Public/Bug/Display.html?id=98979
                             #
                             # This (fairly) dirty patch solves the issue.
                             # If this breaks, refer to the original source in:
                             # https://github.com/ImageMagick/ImageMagick/blob/main/PerlMagick/Makefile.PL.in
                             q|echo "Patching Makefile.PL files in `pwd`. See |.__FILE__.q| for more"|,
                             q|perl -pi -e 's:^(\s*.INSTALL_BASE.*$):#$1:' PerlMagick/Makefile.PL|,
                             q|perl -pi -e 's:-L\.\./magick(.*?)/\.libs:-L|.$siteprefix.q|/lib:gi' PerlMagick/Makefile.PL|,
                             q|perl -pi -e 's:-L\.\./\.\./magick(.*?)/\.libs:-L|.$siteprefix.q|/lib:gi' PerlMagick/quantum/Makefile.PL|,
                             q|perl -pi -e 's:-L\.\./\.\./magick(.*?)/\.libs:-L|.$siteprefix.q|/lib:gi' PerlMagick/default/Makefile.PL|,
                             ,
                             'make',
    ],
    alien_install_commands => [
        'LDFLAGS='.$LDFLAGS.' make install', # This will build the included PerlMagick package.
        $ldconfig // "",
        $perlbin.' -e "use Image::Magick; print Image::Magick->QuantumDepth"', # This checks Image magick is there fine.

    ],
    meta_merge => {
        resources  => {
            repository => "https://github.com/jeteve/Alien-ImageMagick",
        },
    }
);

$builder->create_build_script();

