use 5.010;
use alienfile;
use Sort::Versions;
use Path::Tiny;

my $on_windows = ($^O =~ /mswin/i);
my $on_automated_rig
  =  $ENV{PERL_CPAN_REPORTER_DIR}
  || $ENV{PERL_CPAN_REPORTER_CONFIG}
  || $ENV{AUTOMATED_TESTING}
  || $ENV{TRAVIS}
  || $ENV{APPVEYOR}
  || $ENV{CI};

use Cwd;
my $base_dir = getcwd();

configure {
  requires
      'File::Copy'       => 0,
      'File::Find::Rule' => 0,
      'Capture::Tiny'    => 0;
};

my $min_target_version = '3.6.0';

plugin 'Build::MSYS';

plugin 'PkgConfig' => (
    pkg_name => 'geos',
    atleast_version => $min_target_version,
);
plugin 'Probe::CommandLine' => (
  command => 'geos-config',
  args    => [ '--version' ],
  version => qr/([0-9\.]+)/,
  atleast_version => $min_target_version,
);
if ($on_windows) {
  if (my $path = `where geos_c.dll`) {
    $path = Path::Tiny::path ($path)->parent->parent->stringify;
    $path =~ s|\\|/|g;  #  path separators
    Alien::Build->log ("Probe::CBuilder checking $path");
    plugin 'Probe::CBuilder' => (
      cflags => "-I$path/include",
      libs   => "-L$path/lib -lgeos -lgeos_c",
    );
  }
}

share {

  my $with_local = '';
  my $with_cpp11 = '';

  start_url 'https://download.osgeo.org/geos';
  #start_url "file://$base_dir";  #  debug
  plugin Download => (
    filter  => qr/^geos-([0-9\.]+)\.tar\.bz2$/,
    version => qr/^geos-([0-9\.]+)\.tar\.bz2$/,
  );

  my $geos_version = get_geos_version() // 'not yet defined';
  say "Downloaded version is $geos_version";
  
  plugin Extract => (format => 'tar.bz2');

  #plugin 'Build::Autoconf' => ();
  plugin 'Build::CMake';

  my $build_static = ($on_windows) ? '' : '--disable-shared';
  $build_static = '';
  $build_static = '--enable-static=no';  #  override - HUGE files if static
  #$build_static = '' if $ENV{FORCE_DYNAMIC};
  
  
  if ($^O =~ /bsd/) {
    plugin 'Build::Make' => 'gmake';
    if (-d '/usr/local') {
        $with_local = ' --with-local=/usr/local ';
    }
  }
  elsif ($^O =~ /dragonfly/) {
    #  might need to be combined with bsd check above
    #  but not sure if /usr/local is needed yet
    plugin 'Build::Make' => 'gmake';
  }
  elsif ($^O eq 'MSWin32') {
    plugin 'Build::Make' => 'gmake';
  }

  my $make_cmd = '%{make}';
  my $make_inst_cmd = '%{make} install';
  my $run_tests = $ENV{ALIEN_GEOS_ENABLE_TESTS};
  my $run_tests_bool_text = $run_tests ? 'ON' : 'OFF';

  #  try not to exceed the cpan-testers log limits
  if ($on_automated_rig) {
    say "Running under CI or automated testing";
    $make_cmd      .= q/ | perl -ne "BEGIN {$|=1; open our $log, q|>|, q|build.log|};   print qq|\n| if 0 == ($. %% 100); print q|.|; print {$log} $_;" || type build.log/;
    $make_inst_cmd .= q/ | perl -ne "BEGIN {$|=1; open our $log, q|>|, q|install.log|}; print qq|\n| if 0 == ($. %% 100); print q|.|; print {$log} $_;" || type install.log/;
    if (!$on_windows) {
        $make_cmd =~ s/%%/%/;
        $make_cmd =~ s/type/cat/;
        $make_cmd =~ s/"/'/g;
        $make_inst_cmd =~ s/%%/%/;
        $make_inst_cmd =~ s/type/cat/;
        $make_inst_cmd =~ s/"/'/g;
    }

    #  clean up the build dir on cpan testers etc
    #plugin 'Cleanse::BuildDir';
  }

  meta->around_hook(
    build => sub {
      my ($orig, $build, @args) = @_;
      $build->log("Setting CCACHE_BASEDIR to " . getcwd());
      local $ENV{CCACHE_BASEDIR} = getcwd();
      $orig->($build, @args);
    }
  );


  #  might fix some rpath issues?
  #  https://github.com/OSGeo/gdal/issues/5413#issuecomment-1060286925
  my @rpath =
    $on_windows ? ()
    : $^O =~ /darwin/
      ? q{-DCMAKE_INSTALL_RPATH='@loader_path'}
      : q{-DCMAKE_INSTALL_RPATH='\$ORIGIN'};

  my $cmake_cmd = [
    '%{cmake}',
    -G => '%{cmake_generator}',
    '-DCMAKE_MAKE_PROGRAM=%{make}',
    '-DBUILD_DOCUMENTATION=NO',
    #"-DGEOS_ENABLE_TESTS=$run_tests_bool_text", #  seems not be in the source tree?
    "-DBUILD_TESTING:BOOL=$run_tests_bool_text",
    #'-DCMAKE_BUILD_TYPE=Release',
    '-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true',
    '-DCMAKE_INSTALL_PREFIX:PATH=%{.install.prefix}',
    @rpath,
    '-DBUILD_GEOSOP=OFF',
    '..'
  ];
  
  #say join ' ', @$cmake_cmd;

  build [
    #\&set_rectangle_intersection,  #  disable
    #"%{configure} $with_local $with_cpp11 $build_static",
    #'echo %{make}',
    #\&pause,
    'mkdir _build',
    'cd _build',
    $cmake_cmd,
    $make_cmd,
    ($run_tests ? '%{make} test' : ()),
    #\&pause,
    \&update_geos_config,
    $make_inst_cmd,
    \&rename_la_files,
  ];

};


gather [
  \&copy_geos_c_dll,
  #\&pause,
  #  get all the geos-config fields
  \&set_runtime_props_from_config,
];

#  a bit brute force
sub copy_geos_c_dll {
  my $build = shift;
  
  return unless $^O eq 'MSWin32';
  
  log 'Searching for libgeos_c.dll';

  my @files
    = File::Find::Rule->file()
                      ->name( 'libgeos_c.dll' )
                      ->in( '.' );

  foreach my $from (@files) {
    my $to = Path::Tiny->new($from)->parent->child('libgeos_c-1.dll');
    if (!$to->exists) {
      log "copying $from to $to";
      eval {File::Copy::copy ("$from", "$to")};
      log $@ if $@;
    }
  }
}


sub set_rectangle_intersection {
    my ($build) = @_;
    
    #  disable as we get test failures in Biodiverse,
    #  which is probably for the same reasons this is
    #  disabled in GEOS in the first place
    return;  
    
    return if $build->install_type ne 'share';

    say 'set_rectangle_intersection: Currently in ' . getcwd();
    
    use File::Find::Rule;
    use Path::Tiny qw /path/;
    my (@files)
      = File::Find::Rule
                ->file()
                ->name( 'Geometry.cpp' )
                ->in( $base_dir );
    #  loop is brute force
    foreach my $file (@files) {
        say "Modifying file $file";
        path($file)->edit (
            sub {
              s{^//(#define USE_RECTANGLE_INTERSECTION 1)}{$1}ms;
            }
        );
    }
}

sub update_geos_config {
    my ($build) = @_;

    $build->log ('updating geos-config to use dynamic base dir');
    use File::Find::Rule;
    my ($geos_config)
      = $build->install_type eq 'share'
          ? File::Find::Rule
                ->file()
                ->name( 'geos-config' )
                ->in( getcwd() )
          : 'geos-config';

    $build->log("Updating $geos_config");

    open my $fh , '<', $geos_config
      or die "Could not open $geos_config for reading, $!";
    my $file_contents;

    while (defined (my $line = <$fh>)) {
        if ($line =~ /^\s*prefix=/) {
            #  MSYS1 does not have realpath
            my $part1
              = q{BASEPATH=`perl -MCwd -MFile::Basename -e"print File::Basename::dirname(Cwd::abs_path (qq{$0}))"`};
            #  some variants use an escape function
            my $part2 = $line =~ /`escape/
              ? 'prefix=`escape "${BASEPATH}"`'
              : 'prefix="${BASEPATH}"';
            my $part3 = 'prefix=$(dirname ${prefix})';
            $line = "$part1\n$part2\n$part3\n";
        }
        $file_contents .= $line;
    }
    $fh->close;
    my $permissions = (stat ($geos_config))[2];
    rename $geos_config, "$geos_config.bak";
    open my $ofh, '>', $geos_config
      or die "Could not open $geos_config for writing, $!";
    print {$ofh} $file_contents;
    $ofh->close or die $!;
    #  make sure we get the same permissions
    chmod $permissions, $geos_config or die $!;
    return;
}

sub set_runtime_props_from_config {
    my ($build) = @_;

    $build->log ('set_runtime_props_from_config: Currently in ' . getcwd());

    $build->log("UPDATING PKG_CONFIG_PATH");
    
    use Env qw /@PKG_CONFIG_PATH @PATH/;
    use Capture::Tiny;
    use Path::Tiny;

    # should use proper Alien::Build methods to get location of file     
    use File::Find::Rule;
    my ($pk_config)
      = $build->install_type eq 'share'
          ? File::Find::Rule
                ->file()
                ->name( 'geos.pc' )
                ->in( getcwd() )
          : 'geos.pc';
    
    my $pk_path = path($pk_config)->parent->stringify;
    unshift @PKG_CONFIG_PATH, $pk_path;
    $build->log("PKG_CONFIG_PATH is $ENV{PKG_CONFIG_PATH}");

    #  should use perl package api?
    my $pkgconf = File::Which::which ('pkg-config') || File::Which::which ('ppkg-config');
    foreach my $flag (qw /cflags libs static/) {
        my @cmd = ($pkgconf, "--$flag", 'geos');
        $build->log("Calling: " . join ' ', @cmd);
        my ($stdout, $stderr, @result) = Capture::Tiny::capture {system @cmd};
        $build->log("ERROR: $stderr") if $stderr;
        $build->runtime_prop->{$flag} = $stdout;
        $build->runtime_prop->{$flag} =~ s/[\r\n]+$//;  #  generic chomp
        if ($on_windows) {
            #  windowsify the paths
            $build->runtime_prop->{$flag} =~ s|(?<=-[IL])/C/|C:/|i;
        }
        if ($^O =~ /bsd/i) {
            #  maybe will help?
            $build->runtime_prop->{$flag} =~ s/-\d+$//;
        }
        $build->log ("Runtime prop $flag is " . $build->runtime_prop->{$flag});
    }
}

sub rename_la_files {
    #  need to return if not share
    return if !$on_windows;
    
    use File::Find::Rule;
    my @la_files
      = File::Find::Rule->file()
                        ->name( '*.la' )
                        ->in( '.' );
    foreach my $file (@la_files) {
        say "Renaming $file so it will not interfere with gdal compilation";
        rename $file, $file . '.bak';
    }

}

sub pause {
    #return;  #  disable
    #return if !$on_windows || $on_automated_rig;

    say "CONTINUE?";
    my $response = <>;
    while (not $response =~ /yes/) {
        $response = <>;
    }
}

sub get_geos_version {
    my $h = get_alien_state_hash();
    return $h->{runtime}{version};
}

sub get_stage_dir {
    my $h = get_alien_state_hash();
    return $h->{install}{stage};
}

sub get_alien_state_hash {
    use JSON::PP;
    my $root = "$base_dir/_alien";
    my $f = "$root/state.json";
    my $h = {};
    if (-e $f) {
        open my $fh, '<', $f or die $!;
        my $d = do {
            local $/ = undef;
            <$fh>;
        };
        $h = JSON::PP::decode_json($d);
    }
    return $h;
}
