#!/usr/bin/perl -w

# dh_octave_substvar: Generate variables ${octave:Depends} and
#     ${octave:Upstream-Description} for Octave-Forge
#     Debian packages
# This file is part of the dh-octave Debian package and was also part
# of the deprecated octave-pkg-dev package.

# Copyright (c) 2018  Rafael Laboissière <rafael@laboissiere.net>
#
# 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;

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.

=cut

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

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

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 {
                $depends {"octave-$_"} = "";
            }
        } grep {not /^octave/i} split (", ", $deps);
    }

    $desc = $fields->get ('description');
    $desc =~ s/\n */\n/g;
    chomp $desc;
    $desc =~ s/\n/\${Newline}/g;
}

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});
    }
    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.

=head1 AUTHOR

Rafael Laboissière <rafael@debian.org>

=cut

exit;
