#!/usr/bin/perl -w

=head1 NAME

dh_gnome - tools for the Debian GNOME Packaging Team

=cut

use strict;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<dh_gnome> [I<debhelper options>] [--no-clean-la] [--no-gnome-version]

=head1 DESCRIPTION

gnome-pkg-tools contains some useful tools for the Debian GNOME Packaging Team.

dh_gnome is a tool designed to provide useful functions to GNOME packages
which do use of debhelper as preferred build system, similar to the CDBS
routines provided by gnome-pkg-tools.

dh_gnome handles some routines to be run during binary-install phase.

=head1 OPTIONS

=over 4

=item B<--no-clean-la>

Use this flag if you do not want to automatically empty dependency_libs fields
provided by your packages' .la files.

=item B<--no-gnome-versions>

Use this flag if you do not want to generate GNOME subsvars for your packages.

=back

=cut

init(options => { "no-clean-la" => \$dh{CLEAN_LA},
                  "no-gnome-versions" => \$dh{GNOME_VERSIONS} });

sub clean_la_files {

    # Empty dependency_libs lines from .la files
    foreach my $package (@{$dh{DOPACKAGES}}) {
        if (-d "debian/$package/usr/lib") {
            opendir (DIR, "debian/$package/usr/lib");
            my @files = grep (/\.la$/, readdir (DIR) );
            closedir (DIR);
            foreach my $file (@files) {
                my $la = "debian/$package/usr/lib/$file";
                open FILE, $la or die $!;
                my @lines = <FILE>;
                close FILE;
                open FILE, "+>", $la or die $!;
                foreach (@lines) {
                    if ($_ =~ /^dependency_libs/) {
                        $_ =~ s/'.*'/''/;
                    }
                    print FILE $_;
                }
                close FILE;
            }
        }
    }

}

sub gnome_versions {

    # Generate ${gnome:Version} and ${gnome:NextVersion} substvars
    foreach my $package (@{$dh{DOPACKAGES}}) {
        my $gnome_version;
        my $gnome_next_version;
        isnative($package);
        $dh{VERSION} =~ /^([0-9]+:)?([0-9]\.[0-9]+)\..*$/;
        if ($1) {
            $gnome_version = $1.$2;
        } else {
            $gnome_version = $2;
        }
        $gnome_version =~ /^([0-9]+:)?([0-9]\.)([0-9]+)$/;
        if ($1) {
            $gnome_next_version = $1.$2.($3+1);
        } else {
            $gnome_next_version = $2.($3+1);
        }
        open FILE, ">>", "debian/$package.substvars" or die $!;
        print FILE "gnome:Version=$gnome_version\n";
        print FILE "gnome:NextVersion=$gnome_next_version\n";
        close FILE;
    }

}

unless ($dh{CLEAN_LA}) { clean_la_files(); }
unless ($dh{GNOME_VERSIONS}) { gnome_versions(); }

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of gnome-pkg-tools but is made to work with debhelper.

=head1 AUTHORS

Luca Falavigna <dktrkranz@debian.org>

=cut
