#!/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-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.

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

=head1 OPTIONS

=over 4

=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-gnome-versions" => \$dh{GNOME_VERSIONS} });

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{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
