#!/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 substvars 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 {

    # Generates ${gnome:Version}, ${gnome:NextVersion},
    # ${gnome:UpstreamVersion} and ${gnome:NextUpstreamVersion} substvars
    foreach my $package (@{$dh{DOPACKAGES}}) {
        my $gnome_version;
        my $gnome_next_version;
        my $epoch;
        isnative($package);
        if ($dh{VERSION} =~ /^(\d+:)?(\d+)\.(\d+)\.[\d.]+.*$/) {
            if ($2 < 40) {
                $epoch = $1;
                $gnome_version = "$2.$3";
                $gnome_next_version = ($2 == 3 && $3 == 38 ? "" : "$2.") .
                    ($3 + 2);
            }
        }
        if (!$gnome_version) {
            $dh{VERSION} =~ /^(\d+:)?(\d+)(\.[\d.]+|~(alpha|beta|rc)[\d.]*|[+~])?.*$/
                or die "Unable to determine major version from $dh{VERSION}";
            $epoch = $1;
            $gnome_version = $2 . "~";
            $gnome_next_version = $2 + 1 . "~";
        }
        $epoch = $epoch ? $epoch : "";
        open FILE, ">>", "debian/$package.substvars" or die $!;
        print FILE "gnome:Version=$epoch$gnome_version\n";
        print FILE "gnome:UpstreamVersion=$gnome_version\n";
        print FILE "gnome:NextVersion=$epoch$gnome_next_version\n";
        print FILE "gnome:NextUpstreamVersion=$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
