use strict;
use warnings;
use alienfile;
use Config;
#
my $Win32 = $^O eq 'MSWin32';
my $VC    = $Win32                              && ( $Config{ccname} eq 'cl' ? 1 : 0 );
my $x64   = $Config{archname} =~ /^MSWin32-x64/ && $Config{ptrsize} == 8;

#
plugin 'PkgConfig' => (
    pkg_name => 'sdl2',
);

probe [ 'pkg-config --exists libsdl2', 'pkg-config --exists libsdl2-dev' ];

share {
    start_url 'https://libsdl.org/release';

    if ( $Win32 && 0 ) {
        if ($VC) {
            plugin 'Download' => (

                #https://www.libsdl.org/release/SDL2-devel-2.0.14-VC.zip
                filter  => qr/^SDL2-devel-[0-9\.]+-VC\.zip$/,
                version => qr/^SDL2-devel-([0-9\.]+)-VC\.zip$/,
            );
            plugin 'Extract' => 'zip';
            build [
                'move lib _lib',
                'move _lib/' . ( $x64 ? 'x64' : 'x86' ) . ' lib',

            ];
        }
        else {
            plugin 'Download' => (

                #https://libsdl.org/release/SDL2-2.0.14.tar.gz
                filter  => qr/^SDL2-[0-9\.]+\.tar\.gz$/,
                version => qr/^SDL2-([0-9\.]+)\.tar\.gz$/,
            );
            plugin 'Extract' => 'tar.gz';
            plugin 'Build::CMake';
            build [
                # this is the default build step, if you do not specify one.
                'mkdir build', 'cd build',
                [
                    '"%{cmake}" -S %{.install.extract} -B .',
                    @{ meta->prop->{plugin_build_cmake}->{args} },

                    # ... put extra cmake args here ...
                    #'%{.install.extract}'
                ],
                'dir',
                'make',
                'make install'
            ];
            _gather();

        }
    }
    else {
        plugin 'Download' => (

            #https://libsdl.org/release/SDL2-2.0.14.tar.gz
            filter  => qr/^SDL2-[0-9\.]+\.tar\.gz$/,
            version => qr/^SDL2-([0-9\.]+)\.tar\.gz$/,
        );
        plugin 'Extract' => 'tar.gz';

        plugin 'Build::Autoconf';
        plugin 'Build::Make' => 'gmake';
        build [
            '%{configure}',    # --prefix=%{.install.prefix}', # --enable-threads=no',
            '%{gmake} -j 10',
            '%{gmake} install',
        ];
        _gather();
    }
};

plugin 'Gather::IsolateDynamic';

sub _gather() {
    gather [
        [ 'pkg-config --modversion sdl2',                                  \'%{.runtime.version}' ],
        [ 'pkg-config --cflags ' . ( $Win32 ? '--static' : '' ) . ' sdl2', \'%{.runtime.cflags}' ],
        [ 'pkg-config --libs       sdl2',                                  \'%{.runtime.libs}' ],
    ];
}

#gather sub {
#        my ($build) = @_;
#        $build->runtime_prop->{version} = $version;
#        $build->runtime_prop->{version} =~ s/^v//;
#        $build->runtime_prop->{cflags} = "-I@{[ $build->runtime_prop->{prefix} ]}/include -lsetupapi -lhid";
#        $build->runtime_prop->{libs}   = "-L@{[ $build->runtime_prop->{prefix} ]}/lib -lSDL2";
#    };

__END__

use alienfile;
use FFI::CheckLib qw[find_lib_or_die];
use Config;
plugin 'PkgConfig' => 'sdl2';

my $version = $ENV{ALIEN_LIBSDL2_VERSION} || '2.0.14';
my $arch    = 'x86_64';

meta->around_hook(
    probe => sub {
        my $orig  = shift;
        my $build = shift;
        my $type  = $orig->( $build, @_ );
        return $type if $type eq 'share';
        find_lib_or_die lib => sdl2 => symbol => [qw[SDL_log SDL_rect]];
        return $type;
    }
);

#probe sub {'share'};

share {
    if ( $^O eq 'MSWin32' ) {
        plugin 'Download' => start_url(
            ( $Config{archname} !~ /^MSWin32-x64/ || $Config{ptrsize} != 8 )
            ? "https://www.libsdl.org/release/SDL2-$version-win32-x86.zip"
            : "https://www.libsdl.org/release/SDL2-$version-win32-x64.zip"
        );
        plugin Extract => 'zip';
    }
    else {    # Linux, OSX, etc.

        plugin 'Download' => ( url => "https://libsdl.org/release/SDL2-$version.tar.gz" );
        plugin Extract    => 'tar.xz';
        plugin 'Build::Autoconf';
        plugin 'Build::Make' => 'gmake';
        build [
            #'mkdir build',
            #'cd build',
            '%{configure} --prefix=%{.install.prefix}',    # --enable-threads=no',
            '%{gmake} -j 10',
            '%{gmake} install',
        ];
    }

    plugin 'Build::Copy';
    gather sub {
        my ($build) = @_;
        $build->runtime_prop->{version} = $version;
        $build->runtime_prop->{version} =~ s/^v//;
        $build->runtime_prop->{cflags} = "-I@{[ $build->runtime_prop->{prefix} ]}/include";
        $build->runtime_prop->{libs}   = "-L@{[ $build->runtime_prop->{prefix} ]}/lib -lSDL2";
    };

    plugin 'Gather::IsolateDynamic';
};
