Blame SOURCES/rpmsort

837a2d
#! /usr/bin/perl -w
837a2d
837a2d
# This program is free software; you can redistribute it and/or
837a2d
# modify it under the terms of the GNU General Public License
837a2d
# as published by the Free Software Foundation; either version 2
837a2d
# of the License, or (at your option) any later version.
837a2d
#
837a2d
# This program is distributed in the hope that it will be useful,
837a2d
# but WITHOUT ANY WARRANTY; without even the implied warranty of
837a2d
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
837a2d
# GNU General Public License for more details.
837a2d
#
837a2d
# You should have received a copy of the GNU General Public License
837a2d
# along with this program; if not, write to the Free Software
837a2d
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
837a2d
# MA 02110-1301 USA.
837a2d
837a2d
use Getopt::Long qw(:config gnu_getopt);
837a2d
837a2d
sub rpm_cmp_versions {
837a2d
    my ($evr1, $evr2) = @_;
837a2d
837a2d
    sub _rpm_cmp {
837a2d
	my ($s1, $s2) = @_;
837a2d
837a2d
	return defined $s1 <=> defined $s2
837a2d
	    unless defined $s1 && defined $s2;
837a2d
837a2d
	my ($r, $x1, $x2);
837a2d
	do {
837a2d
	    $s1 =~ s/^[^a-zA-Z0-9]+//;
837a2d
	    $s2 =~ s/^[^a-zA-Z0-9]+//;
837a2d
	    if ($s1 =~ /^\d/ || $s2 =~ /^\d/) {
837a2d
		$s1 =~ s/^0*(\d*)//;  $x1 = $1;
837a2d
		$s2 =~ s/^0*(\d*)//;  $x2 = $1;
837a2d
		$r = length $x1 <=> length $x2 || $x1 cmp $x2;
837a2d
	    } else {
837a2d
		$s1 =~ s/^([a-zA-Z]*)//;  $x1 = $1;
837a2d
		$s2 =~ s/^([a-zA-Z]*)//;  $x2 = $1;
837a2d
		return 0
837a2d
		    if $x1 eq '' && $x2 eq '';
837a2d
		$r = $x1 cmp $x2;
837a2d
	    }
837a2d
	} until $r;
837a2d
	return $r;
837a2d
    }
837a2d
837a2d
    my ($e1, $v1, $r1) = $evr1 =~ /^(?:(\d*):)?(.*?)(?:-([^-]*))?$/;
837a2d
    my ($e2, $v2, $r2) = $evr2 =~ /^(?:(\d*):)?(.*?)(?:-([^-]*))?$/;
837a2d
    my $r = _rpm_cmp($e1 || 0, $e2 || 0);
837a2d
    $r = _rpm_cmp($v1, $v2)
837a2d
	unless $r;
837a2d
    $r = _rpm_cmp($r1, $r2)
837a2d
	unless $r;
837a2d
    return $r;
837a2d
}
837a2d
837a2d
my $reorder = sub { return @_ };
837a2d
my $key = 0;
837a2d
837a2d
GetOptions ("r|reverse"	    => sub { $reorder = sub { return reverse @_ } },
837a2d
	    "k|key=i"	    => \$key)
837a2d
or do {
837a2d
    print STDERR "Usage\n";
837a2d
    exit 1;
837a2d
};
837a2d
837a2d
if ($key == 0) {
837a2d
    # Sort by entire lines
837a2d
    map { print } &$reorder(sort { rpm_cmp_versions($a, $b) } <>);
837a2d
} else {
837a2d
    # Sort by field $key
837a2d
    my @data = map { [(split)[$key-1], $_] } <>;
837a2d
    map { print } &$reorder(map { $_->[1] }
837a2d
        sort { rpm_cmp_versions($a->[0], $b->[0]) } @data);
837a2d
}