use strict;
use warnings;

use Module::Build;
use ExtUtils::PkgConfig;

my %build_args;

sub eu_pc_modversion
{
   my ( $module ) = @_;

   # Silence its scary errors
   local *STDERR = *STDERR;
   open STDERR, ">>", "/dev/null" or die "Cannot reopen STDERR - $!";

   eval { ExtUtils::PkgConfig->modversion( $module ) };
}

sub add_cflags_libs
{
   my ( $modname ) = @_;

   my $cflags = ExtUtils::PkgConfig->cflags( $modname );
   my $libs   = ExtUtils::PkgConfig->libs( $modname );

   push @{ $build_args{extra_compiler_flags} }, split( m/ +/, $cflags );

   push @{ $build_args{extra_linker_flags} }, split( m/ +/, $libs );
}

if( eu_pc_modversion( "tickit" ) ) {
   # libtickit installed; just use it
   print "Using installed libtickit\n";

   add_cflags_libs "tickit";
}
else {
   # libtickit not installed; use bundled source code instead
   print "Using bundled libtickit source\n";

   $build_args{c_source}     = "src";
   $build_args{include_dirs} = "include";

   $build_args{extra_compiler_flags} = [qw( -std=c99 )];

   # bundled source still needs libtermkey and optionally can use
   # unibilium
   if( eu_pc_modversion( "termkey" ) ) {
      print "Using installed libtermkey\n";

      add_cflags_libs "termkey";
   }
   elsif( eval { require Alien::libtermkey } ) {
      print "Using Alien::libtermkey\n";

      push @{ $build_args{extra_compiler_flags} },
         split m/ +/, Alien::libtermkey->cflags;

      push @{ $build_args{extra_linker_flags} },
         split m/ +/, Alien::libtermkey->libs;
   }
   else {
      die "OS unsupported - need libtermkey";
   }

   if( eu_pc_modversion( "unibilium" ) ) {
      print "Using installed unibilium\n";

      push @{ $build_args{extra_compiler_flags} }, qw( -DHAVE_UNIBILIUM );

      add_cflags_libs "unibilium";
   }
   elsif( eval { require Alien::unibilium } ) {
      print "Using Alien::unibilium\n";

      push @{ $build_args{extra_compiler_flags} }, qw( -DHAVE_UNIBILIUM ),
         split m/ +/, Alien::unibilium->cflags;

      push @{ $build_args{extra_linker_flags} },
         split m/ +/, Alien::unibilium->libs;
   }
   elsif( eu_pc_modversion( "ncursesw" ) ) {
      print "Using installed ncursesw\n";

      add_cflags_libs "ncursesw";
   }
   else {
      print "Using installed ncurses\n";

      push @{ $build_args{extra_linker_flags} }, qw( -lncurses );
   }
}

my $build = Module::Build->new(
   module_name => 'Tickit',
   %build_args,
   requires => {
      'Exporter' => '5.57',
      'List::MoreUtils' => 0,
   },
   configure_requires => {
      'ExtUtils::PkgConfig' => 0,
      'Module::Build' => 0,
   },
   # Note to downstream package maintainers:
   # We don't -actually- depend directly on these Alien modules but it's far
   # easier to get CPAN smoketest machines to install them this way than
   # anything else.
   # These dependencies can be omitted if libtermkey/unibilium are installed
   # by some other mechanism
   meta_merge => {
      configure_requires => {
         'Alien::libtermkey' => 0,
         'Alien::unibilium' => 0,
      },
   },
   build_requires => {
      'Test::Builder::Tester' => 0,
      'Test::Fatal' => 0,
      'Test::HexString' => 0,
      'Test::Identity' => 0,
      'Test::More' => 0,
      'Test::Refcount' => 0,
   },
   auto_configure_requires => 1,
   license => 'perl',
   create_makefile_pl => 'small',
   create_license => 1,
   create_readme  => 1,
);

$build->create_build_script;
