#!/usr/bin/perl -w
# sshfs bernhard@delta.zq1.de:/home/bernhard/public_html/mirror/opensuse/distribution/sles11-sp1/updates/src ~/ssh/delta/
# usage: find ~/ssh/delta/ -type f | perl importlatestsrcrpm
use strict;
use Data::Dumper;
#use XML::Simple;
use DB_File;

my %cache;
tie %cache, 'DB_File', "/tmp/obscache.dbm";

sub pkgstrsplit($)
{
	my $path=shift;
	my $filename=$path;
	$filename=~s{.*/}{};
	my $pkgname=$filename;
	$pkgname=~s/\.src\.rpm$//;
	# separate version from name
	$pkgname=~m/^(.*)-([^-]+-[0-9.]+)$/;
	$pkgname=$1;
	my $pkgver=$2;
	die "can not split version in $path" unless defined $pkgver;
	return {path=>$path, filename=>$filename, name=>$pkgname, ver=>$pkgver};
}

sub getzobsver($)
{ my $pkg=shift;
	if(defined(my $vcache=$cache{$pkg})) {return $vcache}
	# needs obsfs from openSUSE:Tools repo
	#my $mnt="/tmp/obs"; #mkdir $mnt;
	#system(qw"obsfs -o", "host=https://api.build.zq1.de:444,user=opensuseapibmw,pass=08400ea9edb08555086e3b8262e1f839", $mnt);
	#system "ls -l $mnt/source/GECKO/glibc/*.spec";
	#my $xml=`osc api /source/home:bmwiedemann/ceph/_history`;
#	my $xml=`osc api /source/GECKO/$pkg/_history`;
#	return 0 unless $xml=~m/^<revisionlist>/;
#	my $ref = XMLin($xml, ForceArray => 1);
#	print Dumper($ref);
	my $spec=`osc api /source/GECKO/$pkg/$pkg.spec 2>/dev/null`;
	$spec=~m/^Version:\s*(.*)$/m;
	my $version=$1;
	$spec=~m/^Release:\s*(.*)$/m;
	my $release=$1;
	if(defined($version) && defined($release)) {
		$version.="-$release";
	} else { $version="" }
	$cache{$pkg}=$version; # update cache
	#system(qw"fusermount -u", $mnt);
	return $version;
}

#print getzobsver("permissions"); #debug

sub cmpverpart($$);
sub cmpverpart($$)
{
	my $a=shift; my $b=shift;
	return 0 if $a eq $b;
	if($a=~s/^(\d+)//) {
		my $asplit=$1;
		if($b=~s/^(\d+)//) {
			my $bsplit=$1;
			my $cmp=$asplit<=>$bsplit;
			return $cmp if $cmp;
			return cmpverpart($a,$b); # recursively compare remaining
		}
		else { die "version type mismatch: $a $b" }
	} elsif($a=~s/^([a-zA-Z]+)//) {
		my $asplit=$1;
		if($b=~s/^([a-zA-Z]+)//) {
			my $bsplit=$1;
			my $cmp=$asplit cmp $bsplit;
			return $cmp if $cmp;
			return cmpverpart($a,$b); # recursively compare remaining
		}
		else { die "version type mismatch: $a $b" }
	}
	die "other ver: $a $b";
}

sub cmpver($$)
{ 
	my $a=shift; my $b=shift;
	my @a1=split(/[._-]/, $a);
	my @b1=split(/[._-]/, $b);
	for(my $i=0; $i<$#a1; ++$i) {
		my $cmp=cmpverpart($a1[$i], $b1[$i]);
		return $cmp if $cmp;
	}
	return 0;
}

my $selftest=1;
if($selftest) {
die unless cmpver("2.9-75.76.1", "2.9-75.29.1") == 1;
die unless cmpver("ruby-1.8.7.p72-5.28.1", "ruby-1.8.7.z72-5.28.1") == -1;
die unless cmpver("0.48-101.18.1", "0.48-101.20.1") == -1;
die unless cmpver("1.2.3_20090314-0.10.1", "1.2.3_20090314-0.12.1") == -1;
}

my %pkg;
while(<>) {
	chomp;
	next unless /\.src\.rpm$/;
	next if /branding/; # respect trade marks
	my $d=pkgstrsplit($_);
	push(@{$pkg{$d->{name}}}, $d);
}

foreach my $pkgname (keys %pkg) {
	my @pkgs=sort({cmpver $b->{ver},$a->{ver}} @{$pkg{$pkgname}});
	#print "$pkgs[0]->{ver} > $pkgs[1]->{ver}\n" if @pkgs>1;
#	print Dumper(@pkgs)."\n";
	my $latestpkg=$pkgs[0];
	my $zobsver=getzobsver($pkgname);
	#print $zobsver,Dumper($latestpkg)."\n";
	if(!$zobsver || cmpver($latestpkg->{ver}, $zobsver)>0) {
		print "need import of $pkgname-$latestpkg->{ver}\n";
		chdir("$ENV{HOME}/code/osc/GECKO/");
		#system(qw"osc importsrcpkg --commit", $latestpkg->{path});
		#$cache{$pkgname}=$latestpkg->{ver};
	}
}
#system "osc rebuild --all GECKO";

