#!/usr/bin/perl -w

# dh_puredata_substvar: Generate variables ${puredata:Depends},
#     ${puredata:Recommends} and  ${puredata:Suggests}
#     for Pure Data Debian packages.
#
# This file is part of the dh-puredata Debian package

# Copyright (c) 2018, 2019  Rafael Laboissière <rafael@debian.org>
# Copyright (c) 2022 IOhannes m zmölnig <umlaeute@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_puredata_substvar - generate substitution variables for a Pd external package

=cut

use strict;
use Debian::Debhelper::Dh_Lib;
use File::Find::Rule;

init ();

=head1 SYNOPSIS

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

=head1 DESCRIPTION

B<dh_puredata_substvar> generates the substitution variables
C<${puredata:Depends}>, C<${puredata:Recommends}> and C<${puredata:Suggests}>,
useful for running Pd-external Debian packages.

=cut

my %depends = ();

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $dir = "debian/$package";
    my @meta_files = File::Find::Rule->file()->name('*-meta.pd')->in($dir);

    # Depends:
    # + Pd
    # - libraries used for abstractions ([declare]) ?
    # - TODO: how to deal with Pd64?
    delsubstvar ($package, 'puredata:Depends');
    addsubstvar ($package, 'puredata:Depends', "puredata | pd");

    # Recommends:
    # - libraries used for abstractions ([declare])
    delsubstvar ($package, 'puredata:Recommends');
    addsubstvar ($package, 'puredata:Recommends', "");

    # Suggests:
    # - libraries used for help-patches ([declare])
    # + pd-libdir if <pkgname>-meta.pd exists
    delsubstvar ($package, 'puredata:Suggests');
    addsubstvar ($package, 'puredata:Suggests', "");
    if (scalar @meta_files > 0) {
        addsubstvar ($package, 'puredata:Suggests', "pd-libdir");
    }
}

=head1 SEE ALSO

L<debhelper(7)>, L<dh(1)>

This program is meant to be used together with debhelper for Debian
packages derived from Pd externals packages.
It is recommended to enable it by adding the dh-sequence-pd-lib-builder
virtual package to Build-Depends, Build-Depends-Arch or
Build-Depends-Indep.
This is equivalent to adding dh-puredata to the same field and adding
--with=pd-lib-builder 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

IOhannes m zmölnig <umlaeute@debian.org>

=cut

exit;
