#!/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}}) {
    # calculate a proper value for a <cpu> part of the extension
    my $deb_host_arch = dpkg_architecture_value("DEB_HOST_ARCH");
    chomp (my $cpu=qx{/usr/share/puredata/debian/dekencpu ${deb_host_arch} 2>/dev/null});
    if ($cpu eq "") {
        # if there's none (e.g. because the helper script doesn't exist),
        # fall back to some default
        $cpu = ${deb_host_arch};
    }
    my $dir = "debian/$package";
    my @meta_files = File::Find::Rule->file()->name('*-meta.pd')->in($dir);
    my @double_files = File::Find::Rule->file()->name("*.linux-${cpu}-64.so")->in($dir);

    my $can_pd64 = 0;
    if (scalar @double_files > 0) {
        $can_pd64 = 1;
    }
    if (package_arch($package) eq 'all') {
        $can_pd64 = 1;
    }


    my $depends_pd = "puredata | pd";
    if ($can_pd64) {
        $depends_pd = "$depends_pd | puredata64 | pd64"
    }

    # Depends:
    # + Pd
    # - libraries used for abstractions ([declare]) ?
    delsubstvar ($package, 'puredata:Depends');
    addsubstvar ($package, 'puredata:Depends', $depends_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-puredata
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=puredata 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;
