use alienfile;

use File::Spec::Functions qw/catfile/;

# download utility available
plugin 'Probe::CommandLine' => (
   command   => 'curl',
);
plugin 'Probe::CommandLine' => (
   command   => 'wget',
   secondary => 1,
);

share {
   start_url 'https://github.com/stedolan/jq/releases/';
   # china mirror
   #start_url 'https://hub.fastgit.org/stedolan/jq/releases/';
   plugin Download => (
      version => qr/^jq-([0-9\.]+)\.tar\.gz$/o,
   );
   plugin Extract => 'tar.gz';
   plugin 'Build::CMake';
   build [
      # pitch in required cmake stuff
      'echo %cd%',
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'scripts', 'gen_builtin_inc.pl'), 'scripts' ],
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'src', 'setenv.h'), 'src' ],
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'src', 'setenv.c'), 'src' ],
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'builtin.c.patch'), '.' ],
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'CMakeLists.txt'),  '.' ],
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'Config.cmake.in'), '.' ],
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'dist.info'),       '.' ],
      [ '%{cp}', catfile('..', '..', '..', 'jq', 'jq.pc.cmake.in'),  '.' ],
      # execute cmake build
      [ '%{cmake}', -G => '%{cmake_generator}', ( $^O eq 'MSWin32' ? '-DCMAKE_MAKE_PROGRAM=%{make}' : () ),
         '-DBUILD_SHARED_LIBS=OFF', '-DCMAKE_POSITION_INDEPENDENT_CODE=ON',
         '-DENABLE_MAINTAINER_MODE=OFF', '-DWITH_ONIGURUMA=builtin',
         '-DCMAKE_INSTALL_PREFIX=%{.install.prefix}',
         '.'
      ],
      '%{make}',
      '%{make} install',
   ];
   plugin 'Gather::IsolateDynamic';
   # save correct package settings
   # for now there is no good way to retrieve them from cmake
   gather sub {
      my ( $build ) = @_;
      my $prefix = $build->runtime_prop->{prefix};
      $build->runtime_prop->{cflags}        = "-I$prefix/include";
      $build->runtime_prop->{cflags_static} = "-I$prefix/include";
      my $extra_libs = ' -lm';
      $extra_libs .= ' -lshlwapi' if $^O eq 'MSWin32';
      $build->runtime_prop->{libs}          = "-L$prefix/lib -ljq -lonig". $extra_libs;
      $build->runtime_prop->{libs_static}   = "-L$prefix/lib -ljq -lonig". $extra_libs;
   };
};