#! /usr/bin/env perl
use v5.26;
use warnings;
use lib (__FILE__ =~ s,[^\\/]+$,lib,r);
use Test2AndUtils;
use experimental qw( signatures );
use Cwd 'abs_path';
use Fcntl 'S_IFREG';
use Sys::Export::Linux;

package Sys::Export::MockDst {
   sub new($class)        { bless { files => {} }, $class }
   sub files($self)       { $self->{files} }
   sub add($self, $attrs) { $self->{files}{$attrs->{name}}= $attrs; }
   sub finish($self)      {}
}

my $tmp= File::Temp->newdir;
my $dst= Sys::Export::MockDst->new();

subtest ld_so_conf => sub {
   subtest newer_arch => sub {
      my $tmp= File::Temp->newdir;
      mkdir "$tmp/etc";
      mkfile "$tmp/etc/ld.so.conf", <<~END;
         # Dynamic linker/loader configuration.
         # See ld.so(8) and ldconfig(8) for details.
         
         include /etc/ld.so.conf.d/*.conf
         include /usr/lib/ld.so.conf.d/*.conf
         END
      mkdir "$tmp/etc/ld.so.conf.d";
      mkfile "$tmp/etc/ld.so.conf.d/perf.conf", <<~END;
         /usr/lib/perf
         END
      mkfile "$tmp/etc/ld.so.conf.d/android-sdk.conf", <<~END;
         /opt/android-sdk/tools/lib
         END
      mkfile "$tmp/etc/ld.so.conf.d/README.txt", <<~END;
         Some Text
         /this/should/not/get/included
         END
      mkdir "$tmp/usr";
      mkdir "$tmp/usr/lib";
      mkdir "$tmp/usr/lib/ld.so.conf.d";
      mkfile "$tmp/usr/lib/ld.so.conf.d/fakeroot.conf", <<~END;
         /usr/lib/libfakeroot
         END
      mkdir "$tmp/usr/lib/perf";
      mkdir "$tmp/usr/lib/libfakeroot";

      my $exporter= Sys::Export::Linux->new(src => "$tmp", dst => $dst);
      is { map +($_ => 1), $exporter->parse_ld_so_conf },
         { 'usr/lib/perf' => 1, 'opt/android-sdk/tools/lib' => 1, 'usr/lib/libfakeroot' => 1 },
         'parse_ld_so_conf';
      like { map +($_ => 1), $exporter->src_lib_path_list },
         {
            'usr/lib/perf' => 1,
            'opt/android-sdk/tools/lib' => DNE,
            'usr/lib/libfakeroot' => 1,
         },
         'src_lib_path_list';
   };

   subtest newer_gentoo => sub {
      my $tmp= File::Temp->newdir;
      mkdir "$tmp/etc";
      mkfile "$tmp/etc/ld.so.conf", <<~END;
         # ld.so.conf autogenerated by env-update; make all changes to
         # contents of /etc/env.d directory.
         include ld.so.conf.d/*.conf
         /usr/lib64
         /usr/local/lib64
         /usr/lib
         /usr/local/lib
         END
      mkdir "$tmp/etc/ld.so.conf.d";
      mkfile "$tmp/etc/ld.so.conf.d/05gcc-x86_64-pc-linux-gnu.conf", <<~END;
         /usr/lib/gcc/x86_64-pc-linux-gnu/15
         END
      mkdir "$tmp/usr";
      mkdir "$tmp/usr/lib";
      mkdir "$tmp/usr/lib/gcc";
      mkdir "$tmp/usr/lib/gcc/x86_64-pc-linux-gnu";
      mkdir "$tmp/usr/lib/gcc/x86_64-pc-linux-gnu/15";

      my $exporter= Sys::Export::Linux->new(src => "$tmp", dst => $dst);
      is { map +($_ => 1), $exporter->parse_ld_so_conf },
         { 'usr/lib/gcc/x86_64-pc-linux-gnu/15' => 1,
           'usr/lib64' => 1,
           'usr/local/lib64' => 1,
           'usr/lib' => 1,
           'usr/local/lib' => 1,
         },
         'parse_ld_so_conf';
      like { map +($_ => 1), $exporter->src_lib_path_list },
         { 'usr/lib/gcc/x86_64-pc-linux-gnu/15' => 1,
           'usr/lib64' => DNE,
           'usr/local/lib64' => DNE,
           'usr/lib' => 1,
           'usr/local/lib' => DNE,
         },
         'src_lib_path_list';
   };

   subtest older_gentoo => sub {
      my $tmp= File::Temp->newdir;
      mkdir "$tmp/etc";
      mkfile "$tmp/etc/ld.so.conf", <<~END;
      # ld.so.conf autogenerated by env-update; make all changes to
      # contents of /etc/env.d directory
      /usr/local/lib
      /usr/lib/gcc/i386-pc-linux-gnu/4.2.4
      /usr/i386-pc-linux-gnu/lib
      /usr/qt/3/lib
      /usr/lib/libstdc++-v3/
      END
      mkdir "$tmp/usr";
      mkdir "$tmp/usr/i386-pc-linux-gnu";
      mkdir "$tmp/usr/i386-pc-linux-gnu/lib";
      mkdir "$tmp/usr/qt";
      mkdir "$tmp/usr/qt/3";
      mkdir "$tmp/usr/qt/3/lib";
      mkdir "$tmp/usr/lib";
      mkdir "$tmp/usr/lib/libstdc++-v3";

      my $exporter= Sys::Export::Linux->new(src => "$tmp", dst => $dst);
      is { map +($_ => 1), $exporter->parse_ld_so_conf },
         { 'usr/local/lib' => 1,
           'usr/lib/gcc/i386-pc-linux-gnu/4.2.4' => 1,
           'usr/i386-pc-linux-gnu/lib' => 1,
           'usr/qt/3/lib' => 1,
           'usr/lib/libstdc++-v3/' => 1,
         },
         'parse_ld_so_conf';
      like { map +($_ => 1), $exporter->src_lib_path_list },
         {
            'usr/local/lib' => DNE,
            'usr/lib/gcc/i386-pc-linux-gnu/4.2.4' => DNE,
            'usr/i386-pc-linux-gnu/lib' => 1,
            'usr/qt/3/lib' => 1,
            'usr/lib/libstdc++-v3' => 1,
         },
         'src_lib_path_list';
   };
};

done_testing;
