#!/usr/bin/perl -w

# dh_octave_substvar: Generate variables ${octave:Depends} and
#     ${octave:Upstream-Description} for Octave-Forge
#     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, 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, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

=encoding utf8

=head1 NAME

dh_octave_substvar - generate substitution variables for an Octave-Forge 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}> and C<${octave:Upstream-Description}> needed for
building an Octave-Forge 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";
    }
}

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

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

my $octave_version = qx {/usr/bin/octave-config --print VERSION};
$octave_version =~ s/-rc/~rc/;
chomp $octave_version;

if (-f "DESCRIPTION") {

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

    my $deps = $fields->get ('depends');
    if (defined $deps) {
        map {
            if (/([^ ]+) \((.*)\)/) {
                my $pkg = $1;
                my $ver = $2;
                my $out = qx{apt-cache show octave-$pkg | grep ^Version: | head -n1};
                chomp $out;
                my $epoch = "";
                if ($out =~ /^Version: (\d+):.*/) {
                    $epoch = "$1:";
                }
                $ver =~ s/(\d)/$epoch$1/;
                $depends {"octave-$pkg"} = $ver;
            } else {
		## Remove trailing newline, see Bug#1025714
		chomp;
                $depends {"octave-$_"} = "";
            }
        } grep {not /^octave/i} split (", ", $deps);
    }

    $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');
    addsubstvar ($package, 'octave:Depends', "octave (>= $octave_version)");
    for my $pkg (keys %depends) {
        # add dependencies from the DESCRIPTION file
        addsubstvar ($package, 'octave:Depends', $pkg, $depends {$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-Forge 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;
