#!/usr/bin/perl

# Copyright © 2022 Felix Lechner <felix.lechner@lease-up.com>
#
# based on a shell script by the same name
#     by Marco Túlio Gontijo e Silva <marcot@debian.org>
#     and Joachim Breitner <nomeata@debian.org>
# which in turn was based on
#     mk-haskell-depends by Martin Sjögren <sjogren@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use v5.20;
use warnings;
use utf8;

use Const::Fast;
use File::Basename;
use Getopt::Long ();
use Path::Tiny;
use Unicode::UTF8 qw(encode_utf8 decode_utf8);

use Debian::Debhelper::Buildsystem::Haskell::Recipes qw(
  run
  package_ext
  find_config_for_ghc
  tmp_package_db
);

const my $SPACE => q{ };
const my $NEWLINE => qq{\n};

my $program_name = basename($0);

my @excludes;

my %options = (
    'exclude|X=s' => \@excludes,
    'help|h' => \&show_help,
);

Getopt::Long::Configure('gnu_getopt', 'pass_through');
Getopt::Long::GetOptions(%options)
  or die encode_utf8("error parsing options\n");

my @args_bytes = grep { /^-/ } @ARGV;
my @configs = grep { !/^-/ } @ARGV;

die encode_utf8("Installed package description file $_ can not be found")
  for grep { !-e } @configs;

$ENV{DH_EXCLUDES} = join($SPACE, @excludes);

my $output = run('dh_listpackages', @args_bytes);
chomp $output;

my @installables = split($SPACE, $output);
for my $installable (@installables) {

    my $extension = package_ext($installable);
    next
      unless $extension eq 'dev';

    @configs = find_config_for_ghc($installable)
      unless @configs;
}

warn encode_utf8("$program_name: No installed package description files found")
  unless @configs;

my $T_DIR = 'debian/dh_haskell_shlibdeps';
path($T_DIR)->mkpath;

my @hackages = map { path($_)->basename(qr{ [.]conf $}x) } @configs;

my @ghc_pkg = tmp_package_db(@configs);

my @gcc_args;
for my $hackage (@hackages) {

    my $libdir_string = decode_utf8(
        run(@ghc_pkg, qw{--simple-output field}, $hackage, 'library-dirs'));
    my @lib_dirs = split($SPACE, $libdir_string);
    push(@gcc_args, (map { "-L$_" } @lib_dirs));

    my $library_string = decode_utf8(
        run(@ghc_pkg, qw{--simple-output field}, $hackage, 'extra-libraries'));
    my @libraries = split($SPACE, $library_string);
    push(@gcc_args, (map { "-l$_" } @libraries));
}

my $source_path = "$T_DIR/probe.c";
my $probe_path = "$T_DIR/probe";

path($source_path)->spew_utf8('int main (void) {}');

my @gcc_args_bytes = map { encode_utf8($_) } @gcc_args;
run('gcc', @gcc_args_bytes, $source_path, '-o', $probe_path);

for my $installable (@installables) {

    my $extension = package_ext($installable);
    next
      unless $extension eq 'dev';

    my $substvars_path = "debian/$installable.substvars";

    run(qw{dpkg-shlibdeps --warnings=1}, "-T$substvars_path", $probe_path);
}

path($T_DIR)->remove_tree;

exit;

sub show_help {
    my $message =<<"EOT";
Usage: $program_name [options] cabal-file ...

Options:
    -X, --exclude INSTALLABLE    exclude INSTALLABLE from processing
EOT

    print encode_utf8($message);

    exit;
}

=head1 NAME

dh_haskell_shlibdeps - calculates Haskell external dependencies on Cabalized
libraries

=encoding utf8
=head1 SYNOPSIS

B<dh_haskell_depends> [S<I<debhelper options>>]
[B<-X>I<package>]
[B<--exclude=>I<package>]
[S<I<file>> ...]

=head1 DESCRIPTION

dh_haskell_shlibdeps is a debhelper program that helps with calculating
external dependencies for building Haskell libraries.

It automates building libraries for the different supported Haskell systems in
Debian.

This script writes the debian/$package.substvars file, including in it the
shlibs:Depends variable.  So, to use this package, include in the Depends:
field in debian/control ${shlibs:Depends}.

=head1 BUGS

hugs and ghc are the only supported targets at the moment.  Cabal does
not yet support nhc98.

=head1 SEE ALSO

L<dh_haskell_depends(1)>
L<dh_haskell_provides(1)>
L<debhelper(7)>

=head1 AUTHOR

Marco Túlio Gontijo e Silva <marcot@debian.org>

Based on ideas in mk-haskell-depends by Martin Sjögren <sjogren@debian.org>

=cut

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et
