use alienfile;
use strict;
use Env qw/@PKG_CONFIG_PATH/;

# Try probing with pkg-config, but at least on
# OSX the provided avro-c.pc file has some broken
# entries, so this always fails:
plugin 'PkgConfig' => (
    pkg_name        => 'avro-c',
);

# And then try with Probe::CBuilder:
plugin 'Probe::CBuilder' => (
  libs    => '-lavro',
  program => q{
#include <stdio.h>
#include <avro.h>
#include <avro/errors.h>

int main() {
    avro_schema_t schema = avro_schema_string();
    if ( schema == NULL ) {
        printf("ERROR: %s\n", avro_strerror());
    }
    else {
        printf("Success!\n");
    }
    return 0;
}
},
);

share {
    require Config;
    require Alien::libjansson;
    require Alien::libsnappy;

    plugin Download => (
        url     => 'https://github.com/apache/avro/archive/release-1.10.0.tar.gz',
        version => qr/release-([0-9\.]+)\.tar\.gz$/,
    );

    plugin Extract => 'tar.gz';

    # TODO: Shellwords
    my $libs = join ' ',
        Alien::libsnappy->libs,
        Alien::libjansson->libs,
    ;

    my $lib_ext = $Config::Config{lib_ext};
    my $lddlflags = join ' ',
        map { m<(\S+\Q$lib_ext\E\b)> ? $1 : () }
        Alien::libsnappy->libs_static,
    ;

    plugin 'Build::CMake' => ();
    build [
        \&update_pkg_conf_path,
        'cd %{.install.extract}/lang/c',
        [
            '%{cmake}' => '.',
                '-DCMAKE_C_FLAGS=' . Alien::libsnappy->cflags,
                '-DSNAPPY_ROOT_DIR:PATH=' . Alien::libsnappy->dist_dir,
                '-DCMAKE_BUILD_TYPE=RelWithDebInfo',
                @{ meta->prop->{plugin_build_cmake}->{args} },
                '%{.install.extract}/lang/c',
        ],
        [
            '%{make}',
                '-C' => '%{.install.extract}/lang/c',
        ],
        [
            '%{make}',
                '-C' => '%{.install.extract}/lang/c',
                'install',
        ],
        sub {
            my ($build) = @_;
            # For whatever reason the pkg-config plugin isn't picking up
            # the avro-c.pc that we build, so:
            my $prefix = $build->runtime_prop->{prefix};
            $build->runtime_prop->{cflags}      ||= "-I$prefix/include ";
            $build->runtime_prop->{libs}        ||= "-L$prefix/lib -lavro";
            $build->runtime_prop->{libs_static} ||= "$prefix/lib/libavro$Config::Config{lib_ext}";
        },
    ];
};

sub update_pkg_conf_path {
    require File::Spec;
    push @PKG_CONFIG_PATH,
        File::Spec->catdir(Alien::libsnappy->dist_dir,  'lib', 'pkgconfig'),
        File::Spec->catdir(Alien::libjansson->dist_dir, 'lib', 'pkgconfig'),
    ;
    return;
}

meta->after_hook(
    'gather_system' => sub {
        # if we are using the system libraries, we need to manually add -lavro into the libs
        my ($build) = @_;
        my $install_prop    = $build->{install_prop} // {}; # make sure not to vivify this!
        my $cbuilder_gather = $install_prop->{plugin_probe_cbuilder_gather} // {};
        foreach my $entry ( keys %$cbuilder_gather ) {
            $build->{runtime_prop}->{$entry} ||= $cbuilder_gather->{$entry};
        }
    }
);

