#! @PERL@
#
# Written by Balazs Nagy (julian7@kva.hu) 1998
# locale bugfix by Michal Svec (rebel@penguin.cz) 2000
# (C) 1998 The Free Software Foundation.
#
#

# override any locale for dates
delete $ENV{"LC_ALL"};
$ENV{"LC_TIME"}="C";

#print $ENV{"LC_ALL"};
#exit 0;

sub gd
{
    my ($dt) = @_;
    $dt =~ tr/ //s;
    $dt =~ s/^\w+ (\w+) (\d+) (\d+:\d+):\d+ .+\n?$/$1 $2 $3/;
    return $dt;
}

$DATE=gd(`date`);

sub list
{
    my (@rpms, %files, $i, $fn, $dn, $sz, $bt);
#    @rpms = `rpm -qa --qf "\%{NAME}-\%{VERSION}-\%{RELEASE}:\%{GROUP}:\%{SIZE}:\%{BUILDTIME:date}\n"`;
    @rpms = `rpm -qa --qf "\%{NAME}-\%{VERSION}:\%{GROUP}:\%{SIZE}:\%{BUILDTIME:date}\n"`;
    print @trpms;
    %files = ();
    %sizes = ();
    %dates = ();
    for $i (@rpms) {
	if ($i =~ /^([^:]+):([^:]+):([^:]+):(.+)$/) {
	    ($fn, $dn, $sz, $bt) = ($1, $2, $3, $4);
	    $dn =~ s/ /_/g;
	    if (defined $files{$dn}) {
		push(@{$files{$dn}}, $fn);
	    } else {
		@{$files{$dn}} = ($fn);
	    }
	    $sizes{$fn} = $sz;
	    $dates{$fn} = gd($bt);
	}
    }
    for $i (sort keys %files) {
	print "dr-xr-xr-x 1 0    0    0 $DATE $i/\n";
	for $fn (sort @{$files{$i}}) {
	    print "-r--r--r-- 1 0    0    $sizes{$fn} $dates{$fn} $i/$fn.trpm\n";
	}
    }
}

#open O, ">>/tmp/tt";
#print O "RPMS: ";
#for $i (@ARGV) {
#    print O "$i ";
#}
#print O "\n";
#close O;

if ($ARGV[0] eq "list") { list(); exit(0); }
elsif ($ARGV[0] eq "copyout") { open O, ">$ARGV[3]"; print O $ARGV[2], "\n"; close O; exit(0); }
exit(1);
