#!/usr/bin/perl
#
# Copyright © Jan Engelhardt <jengelh [at] inai de>, 2012(-April-17)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the WTF Public License version 2 or
# (at your option) any later version.
#
# Because raw numbers that the Open Build Service daemons themselves
# report may also contain packages for various distributions, this
# script will, for the purpose of finding the "bigger is better"
# coefficient of a distro, exclude experimental home: projects, known
# non-SUSE projects (Fedora:), and (non-SUSE) build targets from the project
# list retrieved from the openSUSE.org Build Service instance as a
# number sanitizing measure.
#
# countpkg-suse3: read the FTP space instead. This only counts successful
# builds, and avoids the network latency of the osc client.
#
#	gzip -cd /F/find.gz |
#	pcregrep -o '(?<=./opensuse/repositories/)\S+\.rpm' |
#	countpkg-suse3
#
# (/F can be the nfs mount of ftp5.gwdg.de:/pub for example)
#
use Getopt::Long;
use strict;

my $excludeprj = qr{
	Arch|
	CentOS|
	DISCONTINUED|
	Debian|
	Fedora|
	Maemo|
	Mandriva|
	Meego|
	MicroSUSE|
	Ports|
	RedHat|
	SUSE|
	ScientificLinux|
	Ubuntu|
	Univention|
	deleted|
	home
}x;
# SUSE: contains - pretty much - only brpms for SLES

my %srcpkg; # unique source names
my %binpkg; # unique bin names
my $bin_cnt = 0; # raw binrpm count (usually useless number)
my $bin_dbg = 0; # raw binrpm (including debug*) count
my $noalarm;
my $wantrepo;
my $current_prj;

&Getopt::Long::Configure(qw(bundling));
&GetOptions(
	"a" => \$noalarm,
	"r=s" => \$wantrepo,
);

print STDERR "\n\n\n\n\n";
$SIG{ALRM} = $noalarm ? sub {} : \&stats;

# Use with `find * -type f -name "*.rpm" | countpkg-suse3`
&scan_path();

sub scan_path
{
	alarm(1);
	while (defined(my $line = <>)) {
		chomp($line);
		next if $line !~ m{\.rpm$};
		# Apache/SLE_10/i586/apache2-debuginfo-2.2.22-59.1.i586.rpm
		my($repo, $name, $ext) = ($line =~ m{([^/]+)/[^/]+/([^/]+)\.(\w+)\.rpm$});
		if (!defined($name)) {
			#print STDERR "BUG. Your line was:\n$line\n";
			# openSUSE:/Factory/images/repo/openSUSE-Factory-DVD-Biarch-i586-x86_64-Build0717-Media/boot/i386/arabic-fonts.rpm
			next;
			exit 1;
		}
		if (length($`) > 0) {
			$current_prj = $`;
		}
		if ($current_prj =~ m{^(?:$excludeprj)(?::|$)}) {
			next;
		}
		if ($wantrepo ne "" && $repo !~ m{^$wantrepo$}) {
			next;
		}
		if ($name =~ m{debuginfo|debugsource}) {
			++$bin_dbg;
			next;
		}
		$name =~ s{-[^-]+-[^-]+$}{}g;
		if ($ext eq "src") {
			++$srcpkg{$name};
		} else {
			++$bin_cnt;
			++$binpkg{$name};
		}
	}
	close FL;
	alarm(0);
	&stats(1);
}

&finalstats();
print STDERR "\n";

sub stats
{
	print STDERR "\e[5A",
	      "Unique source package names: ", scalar(keys %srcpkg), "\e[K\n",
	      "Unique binary package names: ", scalar(keys %binpkg), "\e[K\n",
	      "Raw count of program RPMs:   $bin_cnt\e[K\n",
	      "Raw count of debug RPMs:     $bin_dbg\e[K\n";
	if (defined($_[0])) {
		print STDERR "Current project looking at:  ", substr($current_prj, -40), "\e[K\n";
	} else {
		print STDERR "Current project looking at:  -\e[K\n";
	}
	alarm(1);
}

sub finalstats
{
	my $cnt = 0;
	print scalar(keys %srcpkg), "\t", scalar(keys %binpkg), "\n";
	print STDERR "\n";
	print STDERR "srcpkg with the highest number of copies:\n";
	foreach (sort { $srcpkg{$b} <=> $srcpkg{$a} } keys %srcpkg) {
		printf STDERR "%2d  %5d  %s\n", ++$cnt, $srcpkg{$_}, $_;
		if ($cnt >= 10) {
			last;
		}
	}
}
