#!/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}>,
as well as C<${puredata:Provides}>.
Useful for creating Debian packages for Pd-externals.

=cut

my %depends = ();

# 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};
}

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $dir = "debian/$package";
    my @pd_files = File::Find::Rule->file()->name('*.pd')->in($dir);
    my @meta_files = File::Find::Rule->file()->name('*-meta.pd')->in($dir);
    my @single_files = File::Find::Rule->file()->name("*.linux-${cpu}-32.so", "*.pd_linux")->in($dir);
    my @double_files = File::Find::Rule->file()->name("*.linux-${cpu}-64.so")->in($dir);

    my $can_pd32 = 0;
    if (scalar @single_files > 0) {
        $can_pd32 = 1;
    }
    my $can_pd64 = 0;
    if (scalar @double_files > 0) {
        $can_pd64 = 1;
    }
    if (package_arch($package) eq 'all') {
        $can_pd32 = 1;
        $can_pd64 = 1;
    }
    if ($can_pd32 || $can_pd64) {;} else {
        if (scalar @pd_files > 0) {
            # platform independent (but arch:any)
            $can_pd32 = 1;
            $can_pd64 = 1;
        }
    }



    my @depends_pd;
    @depends_pd = ();

    my @depends_pdx;
    my @recommends_pdx;
    my @suggests_pdx;
    my @provides_pdx;
    @depends_pdx = ();
    @recommends_pdx = ();
    @suggests_pdx = ();
    @provides_pdx = ();


    if ($can_pd32) {
        @depends_pd = (@depends_pd, "puredata", "pd");
        if (rindex($package, "pd64-", 0) == 0) {
            @provides_pdx = (@provides_pdx, "pd-" . substr($package, 5));
        }
        if (scalar @meta_files > 0) {
            @suggests_pdx = (@suggests_pdx, "pd-libdir");
        }
    }
    if ($can_pd64) {
        @depends_pd = (@depends_pd, "puredata64", "pd64");
        if (rindex($package, "pd-", 0) == 0) {
            @provides_pdx = (@provides_pdx, "pd64-" . substr($package, 3));
        }
        if (scalar @meta_files > 0) {
            @suggests_pdx = (@suggests_pdx, "pd64-libdir");
        }
    }


    @depends_pdx = (@depends_pdx, join(' | ', @depends_pd));

    # Depends:
    # + Pd
    # - libraries used for abstractions ([declare]) ?
    delsubstvar ($package, 'puredata:Depends');
    addsubstvar ($package, 'puredata:Depends', join(' | ', @depends_pdx));

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

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

    # Provides:
    # + Pd32: 'pd-<library>'
    # + Pd64: 'pd64-<library>'
    delsubstvar ($package, 'puredata:Provides');
    addsubstvar ($package, 'puredata:Provides', join(', ', @provides_pdx));
}

=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;
