#! perl

use v5.10;
use strict;
use warnings;

use alienfile;
use Path::Tiny;
use Sort::Versions;
use File::Which;

#<<<
use constant PACKAGE_VERSION =>  '8.0.2';
#>>>

our $VERSION = 'v8.0.2.2';

use constant PACKAGE_NAME_R        => 'qhull_r';
use constant PACKAGE_NAME_STATIC_R => 'qhullstatic_r';

##########################################################################################
# Some system installations (e.g. Debian 12) compile their static libraries in such a way
# that linking the Perl .so XS module against it fails with something like:

# relocation R_X86_64_PC32 against symbol `qh_version2' can not be
# used when making a shared object; recompile with -fPIC'

# As far as I can tell, Alien::Build is passing -fPIC, but the system
# library hasn't been compiled with it.  So, need to use dynamic
# linking against the system library, but want to use static linking
# for a share install.

# However, qhull uses separate .pc files for dynamic and static
# linking, and the PkgConfig plugin really wants to use a single file
# for both.

# I haven't figured out how to make that happen portably, so instead
# force a static share install by using the PkgConfig plugin specifying the
# static .pc file and ensuring that it always fails by requiring a non-existent version.

plugin PkgConfig => ( pkg_name => PACKAGE_NAME_STATIC_R, exact_version => 0 );

share {
    plugin 'Build::CMake';

    # start_url 'http://www.qhull.org/download/';
    start_url( ( path( 'src' )->children( qr/qhull.*-@{[PACKAGE_VERSION]}/ ) )[0] );

    plugin Download => (
        filter  => qr/(?:\d+[.]\d+[.]\d+)[.]tgz*$/,
        version => qr/(\d+[.]\d+[.]\d+)[.]tgz*$/,
    );

    plugin Extract => 'tar.gz';

    build [ [
            '%{cmake}', @{ meta->prop->{plugin_build_cmake}->{args} },
            '-D',       'BUILD_SHARED_LIBS=OFF',
            '%{.install.extract}',
        ],
        '%{make}',
        '%{make} install',
    ];

};
