#!/usr/bin/perl
#changelog2pkgformat
#use DateTime;
my ($changelog,$type,$dest,$package,$version,$release,$distributions,$urgency)=@ARGV;
#to upgrade to this version
#URGENCY=low
#, medium, high, emergency, or critical


print "cl2pkgf.pl DATA: $changelog, $type, $dest, $package, $version, $release, $distributions , $urgency\n";

if (not $changelog) {$changelog="ChangeLog"}

if (not -e $changelog) {print "no se encuentra ChangLog $changelog\n"; exit;}
#if (not $type) {print "no se especifico tipo de resultado: deb, rpm\n";exit}
if ($type eq 'deb'){}
elsif ($type eq 'rpm'){}
else {print "mal el tipo $type\n"; exit}
if (not $dest){print "No indico archivo destino\n"; exit}
if (not  $urgency) {$urgency="low";}
if (not $package) {print "NO PAQUETE\n"; exit;}
if (not $version) {print "NO VERSION\n"; exit;}
if ($release eq '') {print "NO RELEASE\n"; exit;}
if (not  $distributions) {
#$distributions="oldstable stable testing unstable experimental";
$distributions="testing unstable experimental";
}

$distributions=~s/-/ /;

$tieneentities=0;
if ($type eq 'rpm'){
    open SPEC, ">$dest" or die $! ;

    #open DEB, ">/tmp/cldeb" or die $!;
$primera=0;
print SPEC "%changelog\n";

open FILE, "<$changelog" or die $!;
my $comienzo=0;
my $final=0;
my $maintainer;
my $fechadeb;
my $fechaspec;
while(<FILE>){
chomp;

if ($_=~/^$/) {if ($final==0) {  } 
                else {
                   #print  DEB   "\n -- $maintainer  $fechadeb\n\n";
                   $final=0;$comienzo=0; 
                   print  SPEC   "\n"
}
                    };
if ($_=~/^\s+\* \[r([0-9]+)\] (.*)/) {
                   print SPEC "- r$1 $2\n";
                   $primera++;
		   if ($primera == 1) {print "PAQUETE $version - ChangeLog $1, ¿hay cambios en el changelog mas nuevos que el paquete?,¿el primero es mas viejo?\n"}
                   #print DEB "$package ($version-r$1) $distributions; urgency=$urgency\n\n";
                   #print DEB "  * $2\n";
                   $final=1}
if ($_=~/^\s+([^\*]+)/) {
                   print SPEC "  $1\n"; 
                   #print DEB  "    $1\n"; 
                   $final=1}
if ($_=~/^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9]) ([0-9][0-9]):([0-9][0-9])  ([^<]+) <*([^>]*)/)
                  {#print "CAMBIO "; 
                   $fechaspec=`LANG=us;date +"%a %b %d %Y" --date="$1-2-$3 $4:$5"`;
                   chomp($fechaspec);
                   $fechadeb=`LANG=us;date -R --date="$1-$2-$3 $4:$5"`;
                   chomp($fechadeb);
                   $comienzo=1;
                   #print "$1 - $5 - $6 - $7";
                   if ($7){
                        $maintainer="$6 <$7>";
                   }else{  
                        $maintainer="$6";
                      }
                   #$dt=DateTime->new(year=>$1,month=>$2,day=>$3,hour=>$4,minute=>$5);
                   print SPEC "* $fechaspec $maintainer\n";
                $tieneentities=1; 
		  }elsif ($_=~/^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9]) ([0-9][0-9]):([0-9][0-9])/)
                  {#print "CAMBIO "; 
                   $fechaspec=`LANG=us;date +"%a %b %d %Y" --date="$1-2-$3 $4:$5"`;
                   chomp($fechaspec);
                   $fechadeb=`LANG=us;date -R --date="$1-$2-$3 $4:$5"`;
                   chomp($fechadeb);
                   $comienzo=1;
                   #print "$1 - $5 - $6 - $7";
                   if ($6){
                   if ($7){
                        $maintainer="$6 <$7>";
                   }else{  
                        $maintainer="$6";
                      }
		   }else{
		       $maintainer="none";
                   }
                   #$dt=DateTime->new(year=>$1,month=>$2,day=>$3,hour=>$4,minute=>$5);
                   print SPEC "* $fechaspec $maintainer\n";
                 
		  }
#print "\n";
#print $_;

}

    if (not $tieneentities){
	print "OJO, svn2cl no encontro entidades en authors.xml en la generacion
          del archivo ChangeLog, este changelog en el spec para rpm falla\n  ";
        }

close SPEC;
}elsif($type eq 'deb'){

    #open SPEC, ">/tmp/clspec" or die $! ;
#       print "DEST $dest";

open DEB, ">$dest" or die $!;

#print SPEC "%changelog\n";
$primera=0;
open FILE, "<$changelog" or die $!;
my $comienzo=0;
my $final=0;
my $maintainer;
my $fechadeb;
my $fechaspec;
while(<FILE>){
chomp;

if ($_=~/^$/) {if ($final==0) {  } 
                else {
                   print  DEB   "\n -- $maintainer  $fechadeb\n\n";
                   $final=0;$comienzo=0; 
                  # print  SPEC   "\n"
                    }
                    };
if ($_=~/^\s+\* \[r([0-9]+)\] (.*)/) {
                  # print SPEC "- r$1 $2\n";
                   $primera++;
		   if ($primera == 1){print "PAQUETE $version - ChangeLog $1, ¿hay cambios en el changelog mas nuevos que el paquete?,¿el primero es mas viejo?\n";}
                   print DEB "$package ($version-$release) $distributions; urgency=$urgency\n\n";
                   # sacamos el $1 o version svn (en realidad ya esta en version)
                   print DEB "  * svn:$1 - $2\n";
                   $final=1}
if ($_=~/^\s+([^\*]+)/) {
                   #print SPEC "  $1\n"; 
                   print DEB  "    $1\n"; 
                   $final=1}
if ($_=~/^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9]) ([0-9][0-9]):([0-9][0-9])  ([^<]+) <*([^>]*)/)
                  {#print "CAMBIO "; 
                   $fechaspec=`LANG=us;date +"%a %b %d %Y" --date="$1-2-$3 $4:$5"`;
                   chomp($fechaspec);
                   $fechadeb=`LANG=us;date -R --date="$1-$2-$3 $4:$5"`;
                   chomp($fechadeb);
                   $comienzo=1;
                   #print "$1 - $5 - $6 - $7";
                   if ($7){
                        $maintainer="$6 <$7>";
                   }else{  
                        $maintainer="$6";
                      }
                   #$dt=DateTime->new(year=>$1,month=>$2,day=>$3,hour=>$4,minute=>$5);
                   #print SPEC "* $fechaspec $maintainer\n";
                 
		  }
#print "\n";
#print $_;

}
close DEB;
}
