#!/usr/bin/perl

#uso: pruebaupackname.pl 



$UPACKNAME="/home/dsa/dsatex/ubuild/scripts/upackname";
open(FILE, "/home/dsa/dsatex/ubuild/scripts/pruebaupackname.testfile");

#scp ../dsatex/ubuild/scripts/upackname ../dsatex/ubuild/scripts/pruebaupackname.testfile ../dsatex/ubuild/scripts/pruebaupackname.pl root@g.unsa.edu.ar:/srv/www/htdocs/sumapack/download

#http://www.sumapack.org/download/pruebaupackname.testfile
#http://www.sumapack.org/download/pruebaupackname.pl
#http://www.sumapack.org/download/upackname
sub first{
   #my ($data2,$data1)=@_;
    $data1=$a;
    $data2=$b;
    #print "MMMM  $UPACKNAME -o ${data2}.can $data1";
   my $pack=`$UPACKNAME -o ${data2}.can $data1`;
    #print "MMMM";
    #print "/home/dsa/dsatex/ubuild/scripts/upackname -o ${data2}.can $data1";
    #print "pack:$pack";
   my ($normresp,$order)=split(/ /,$pack);
    #print "O:$order";
    return $order+0;
}
use Data::Dumper;

while(<FILE>){

    $count++;
    if ($count>200){last}
    chomp $_;
    $pack=`$UPACKNAME -o $_`;
    chomp $pack;
    ($nombre,$version,$release,$system,$ext)=split(/_/,$pack);
    $datos=[$nombre,$version,$release,$system,$ext,$pack,$_];
    print "Pro: $_  -> $nombre $version $release $system $ext\n";
    $dat->{$nombre}=[@{$dat->{$nombre}},$datos];
    $lista->{$nombre}=[@{$lista->{$nombre}},$pack];
    $canon->{$pack}="$_";
}
close(FILE);

#print"hh", Dumper $dat;

# ordenada:
#        baselayout_2.0.1_5.0__tbz2   => baselayout-2.0.1.tbz2
#        baselayout_2.0.0_5.0__tbz2   => baselayout-2.0.0.tbz2
#        baselayout_2.0.0_1.3.1__tbz2   => baselayout-2.0.0_alpha3-r1.tbz2
#        baselayout_1.13.0_1.12.0__tbz2   => baselayout-1.13.0_alpha12.tbz2
#        baselayout_1.13.0_1.10.2__tbz2   => baselayout-1.13.0_alpha10-r2.tbz2
#        baselayout_1.13.0_1.7.1__tbz2   => baselayout-1.13.0_alpha7-r1.tbz2
#        baselayout_1.13.0_1.5.1__tbz2   => baselayout-1.13.0_alpha5-r1.tbz2
#        baselayout_1.13.0_1.3.0__tbz2   => baselayout-1.13.0_alpha3.tbz2
#        baselayout_1.13.0_1.1.1__tbz2   => baselayout-1.13.0_alpha1-r1.tbz2
#        baselayout_1.12.12_5.0__tbz2   => baselayout-1.12.12.tbz2
#        baselayout_1.12.11.1_5.0__tbz2   => baselayout-1.12.11.1.tbz2
#        baselayout_1.12.10_5.5__tbz2   => baselayout-1.12.10-r5.tbz2
#        baselayout_1.12.10_5.4__tbz2   => baselayout-1.12.10-r4.tbz2
#        baselayout_1.12.5_5.0__tbz2   => baselayout-1.12.5.tbz2
#        baselayout_1.12.4_5.7__tbz2   => baselayout-1.12.4-r7.tbz2
#        baselayout_1.12.4_5.3__tbz2   => baselayout-1.12.4-r3.tbz2

#        b2evolution_2.4.2_4.2_all__deb   => b2evolution_2.4.2-4.2_all.deb
#        b2evolution_2.4.2_4.1_all__deb   => b2evolution_2.4.2-4.1_all.deb
#        b2evolution_2.4.2_4_all__deb   => b2evolution_2.4.2-4_all.deb
#        b2evolution_0.9.2_3+etch1_all__deb   => b2evolution_0.9.2-3+etch1_all.deb
#        b2evolution_0.9.2_3_all__deb   => b2evolution_0.9.2-3_all.deb


foreach my $nombre (keys %$dat){
    print "$nombre \n";
    $datacomp=$dat->{$nombre}->[0]->[5]; 
     print " ingresa: @{$lista->{$nombre}} \n";
   
    @ordenada=sort first @{$lista->{$nombre}};

    if (scalar @ordenada>1){
	print " ordenada: \n";
	foreach (@ordenada){
    
          print "        $_   => $canon->{$_}\n";
	}

#    foreach my $data (@{$dat->{$nombre}}){
        #
#	($n,$v,$r,$s,$e,$p,$o)=@$data;

#        $pack2=`/home/dsa/dsatex/ubuild/scripts/upackname -o $o $datacomp`;
        #print "BBBB $pack2\n";
#        ($normresp,$order)=split(/ /,$pack2);
#        ($nc,$vc,$rc,$sc,$ec)=split(/_/,$datacomp);
#	print "   $v $r $s $e $p $o \n";
#	print "   $vc $rc $sc $ec  - $normresp - $datacomp : $order \n";

#$normresp - $datacomp - $order\n";
#    }
    }else
{print "Solo uno\n"}
print "=======================================================\n";
}

#print " @lista \n";
