build: include git info

If enabled with --with-pkg-gitversion on ./configure, this will append
git version strings and branch information at the following places:
 - overall version number: 0.99.21-g0123456
 - login motd and show version: tag information + git id + branches

Sample output:
  Hello, this is Quagga (version 0.99.21-g14b49ad-dirty).
  Copyright 1996-2005 Kunihiro Ishiguro, et al.

  This is a git build of quagga_0_99_21_release-106-g14b49ad-dirty
  Associated branch(es):
	local:master

[v2]: fix build without gitinfo (add "else" branch)
[v2]: fix for repos without any tags (different git describe output)
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/lib/gitversion.pl b/lib/gitversion.pl
new file mode 100644
index 0000000..448f13d
--- /dev/null
+++ b/lib/gitversion.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/perl -w
+use strict;
+
+my $dir = shift;
+chdir $dir || die "$dir: $!\n";
+
+my $gitdesc = `git describe --always --dirty || echo -- \"0-gUNKNOWN\"`;
+chomp $gitdesc;
+my $gitsuffix = ($gitdesc =~ /([0-9a-fA-F]{7}(-dirty)?)$/) ? $1 : "-gUNKNOWN";
+
+printf STDERR "git suffix: %s\n", $gitsuffix;
+printf "#define GIT_SUFFIX \"%s\"\n", $gitsuffix;
+
+my $gitcommit = `git log -1 --format=\"%H\" || echo DEADBEEF`;
+chomp $gitcommit;
+open(BRANCHES, "git branch -a -v --abbrev=40|") || die "git branch: $!\n";
+my @names = ();
+while (<BRANCHES>) {
+	chomp $_;
+	if (/\s+(.*?)\s+$gitcommit/) {
+		my $branch = $1;
+		if ($branch =~ /^remotes\/(.*?)(\/.*)$/) {
+			my $path = $2;
+			my $url = `git config --get "remote.$1.url"`;
+			chomp $url;
+			$url =~ s/^(git:|https?:|git@)\/\/github\.com/github/i;
+			$url =~ s/^(ssh|git):\/\/git\.sv\.gnu\.org\/srv\/git\//savannah:/i;
+			$url =~ s/^(ssh|git):\/\/git\.savannah\.nongnu\.org\//savannah:/i;
+
+			push @names, $url.$path;
+		} else {
+			push @names, 'local:'.$branch;
+		}
+	}
+}
+
+printf STDERR "git branches: %s\n", join(", ", @names);
+
+my $cr = "\\r\\n\\";
+printf <<EOF, $gitdesc, join($cr."\n\\t", @names);
+#define GIT_INFO "$cr
+This is a git build of %s$cr
+Associated branch(es):$cr
+\\t%s$cr
+"
+EOF
+