#!/usr/bin/perl -w

# dh_octave_substvar: Generate variables ${octave:Depends} and
#     ${octave:Upstream-Description} for Octave add-on
#     Debian packages. Also, adds the virtual package
#     octave-abi-N to ${shlibs:Depends}
#
# This file is part of the dh-octave Debian package and was also part
# of the deprecated octave-pkg-dev package.

# Copyright (c) 2018, 2019, 2022, 2025  Rafael Laboissière <rafael@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 <https://www.gnu.org/licenses/>.

=encoding utf8

=head1 NAME

dh_octave_substvar - generate substitution variables for an Octave add-on package

=cut

use strict;
use Debian::Debhelper::Dh_Lib;
use MIME::Head;
use File::Find::Rule;
use Text::Wrapper;

init ();

=head1 SYNOPSIS

B<dh_octave_substvar> [S<I<debhelper options>>]

=head1 DESCRIPTION

B<dh_octave_substvar> generates the substitution variables
C<${octave:Depends}>, C<${octave:Recommends}>, and
C<${octave:Upstream-Description}> needed for building an Octave add-on
Debian package.  It also injects the dependency on the appropriate
virtual package octave-abi-N into C<${shlibs:Depends}>, for packages
that contain *.oct and *.mex, according to the current Octave's
API_VERSION.

=cut

sub find_oct_or_mex {
    my $F = $File::Find::name;
    if ($F =~ /\.oct$/ or $F =~ /\.mex$/ ) {
	print "$F\n";
    }
}

sub process_dependencies {
    my $value = shift;
    my %relations = ();
    map {
        if (/([^ ]+) \((.*)\)/) {
            my $pkg = $1 eq "octave" ? $1 : "octave-$1";
            my $ver = $2;
            my $out = qx{apt-cache show $pkg | grep ^Version: | head -n1};
            chomp $out;
            my $epoch = "";
            if ($out =~ /^Version: (\d+):.*/) {
                $epoch = "$1:";
            }
            $ver =~ s/(\d)/$epoch$1/;
            $relations {$pkg} = $ver;
        } else {
            ## Remove trailing newline, see Bug#1025714
            chomp;
            ## At 2026-08-12, no package has an unversioned octave dependency,
            ## but stay on the safe side.
            my $pkg = $_ eq "octave" ? $_ : "octave-$_";
            $relations {$pkg} = "";
        }
    } split (", ", $value);
    return %relations;
}

my %depends = ();
my %recommends = ();
my $desc = "";

### Prefix for the Octave ABI virtual package name
my $octave_abi_pkg_prefix = "octave-abi-";

if (-f "DESCRIPTION") {

    my $fields = MIME::Head->new->from_file ("DESCRIPTION");

    my $deps = $fields->get ('depends');
    if (defined $deps) {
        %depends = process_dependencies ($deps)
    }
    if (!exists $depends {"octave"}) {
        ## Ensure that there is a dependency on octave, if the DESCRIPTION file
        ## does not contain one (e.g. octave-iso2mesh as of 2026-08-12)
        $depends {"octave"} = "";
    }

    my $optdeps = $fields->get ('optionaldepends');
    if (defined $optdeps) {
        %recommends = process_dependencies ($optdeps)
    }

    $desc = $fields->get ('description');
    $desc =~ s/\n */ /g;
    my $wrapper = Text::Wrapper->new (columns => 75);
    $desc = $wrapper->wrap ($desc);
    chomp $desc;
    $desc =~ s/\n/\${Newline}/g;

}

### Get the current ABI verson of Octave
my $api_version = qx {/usr/bin/mkoctfile --print API_VERSION};
$api_version =~ s/api-v//;
chomp $api_version;

foreach my $package (@{$dh{DOPACKAGES}}) {
    delsubstvar ($package, 'octave:Depends');
    for my $pkg (keys %depends) {
        # add dependencies from the DESCRIPTION file
        addsubstvar ($package, 'octave:Depends', $pkg, $depends {$pkg});
    }

    delsubstvar ($package, 'octave:Recommends');
    for my $pkg (keys %recommends) {
        # add optional dependencies from the DESCRIPTION file
        addsubstvar ($package, 'octave:Recommends', $pkg, $recommends {$pkg});
    }

    ## Add the appropiate octave-abi-N dependency if the package contains
    ## dynamically loadable files (*,oct and *.mex).
    my $dir = "debian/$package";
    my @oct_files = File::Find::Rule->file()->name('*.oct')->in($dir);
    my @mex_files = File::Find::Rule->file()->name('*.mex')->in($dir);
    if (scalar @oct_files > 0 or scalar @mex_files > 0) {
        addsubstvar ($package, 'shlibs:Depends', "$octave_abi_pkg_prefix$api_version");
    }
    delsubstvar ($package, 'octave:Upstream-Description');
    addsubstvar ($package, 'octave:Upstream-Description', $desc);
}

=head1 SEE ALSO

L<debhelper(7)>, L<dh(1)>, L<dh_octave_clean(1)>, L<dh_octave_check(1)>,
L<dh_octave_version(1)>, L<dh_octave_changelogs(1)>, , L<dh_octave_examples(1)>

This program is meant to be used together with debhelper for Debian
packages derived from Octave add-on packages.
It is recommended to enable it by adding the dh-sequence-octave
virtual package to Build-Depends, Build-Depends-Arch or
Build-Depends-Indep.
This is equivalent to adding dh-octave to the same field and adding
--with=octave to the dh sequencer in debian/rules, except that only
-arch/-indep/profiled builds are affected.
This is also slightly more simple in the most common case.

=head1 AUTHOR

Rafael Laboissière <rafael@debian.org>

=cut

exit;

# Local Variables:
# indent-tabs-mode: nil
# tab-width: 4
# End:
