- add --changelog option
[tools/obs-build.git] / changelog2spec
1 #!/usr/bin/perl -w
2
3 #
4 # Convert a SUSE changelog file to rpm format
5 #
6
7 use Date::Parse;
8 use Time::Zone;
9
10 use strict;
11
12 my @wday = qw{Sun Mon Tue Wed Thu Fri Sat};
13 my @mon = qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec};
14
15 if (@ARGV == 2 && $ARGV[0] eq '--spec') {
16   die("bad --spec arg\n") unless $ARGV[1] =~ /^(.*)\/([^\/]+)\.spec$/;
17   my ($dir, $spec) = ($1, $2);
18   opendir(D, $dir) || die("$dir: $!\n");
19   my @changes = grep {/\.changes$/} readdir(D);
20   closedir(D);
21   @changes = sort {length($a) <=> length($b) || $a cmp $b} @changes;
22   exit(1) unless @changes;
23   if (@changes > 1) {
24     while ($spec ne '') {
25       my @c = grep {/\Q$spec\E/} @changes;
26       if (@c) {
27         @changes = @c;
28         last;
29       }
30       last unless $spec =~ s/[-.][^-.]*$//;
31     }
32   }
33   @ARGV = ("$dir/$changes[0]");
34 }
35
36 my $ok;
37 my $zone;
38 while (<>) {
39   chomp;
40   next if (/^--------------/);
41   next if (/^========================/);
42   if (/^(?:\* )?([A-Za-z]+\s+[A-Za-z]+\s+[0-9].*[0-9][0-9][0-9][0-9])(.*\@.*$)/) {
43     my $dt = $1;
44     my $who = $2;
45     $dt = lc($dt);
46     $who =~ s/^\s+//;
47     $who =~ s/^-\s*//;
48     $dt =~ /([0-9][0-9][0-9][0-9])/;
49     my $year = $1;
50     if (!defined($zone) && $dt =~ /\s([a-z]{3,4})(dst)?\s[0-9]{4}/) {
51       my $dst = $2;
52       $zone = tz_offset($1);
53       $zone += 3600 if defined($zone) && $dst;
54     }
55     my $tdt = str2time($dt);
56     $dt =~ /([0-9]+)/;
57     my $day = $1;
58     if (!$tdt) {
59       if ($dt =~ /([a-z]{3})\s+([a-z]{3})/) {
60         $tdt = str2time("$1 $2 $day $year");
61       }
62     }
63     if (!$tdt) {
64       if ($dt =~ /([a-z]{3})/) {
65         $tdt = str2time("$1 $day $year");
66       }
67     }
68     if (!$tdt) {
69       $tdt = str2time("$year-1-1");
70     }
71     $tdt += 12 * 3600 unless $dt =~ /\d:\d/;    # 12:00 if not specified
72     my @gm = gmtime($tdt + ($zone || 0));
73     printf("* %s %s %2d %4d %s\n", $wday[$gm[6]], $mon[$gm[4]], $gm[3], $gm[5] + 1900, $who);
74     $ok = 1;
75     next;
76   }
77   next unless $ok;
78   s/\s+$//;
79   next if $_ eq '';
80   s/%/%%/g;
81   s/^(\s*)(\#\d*)/$1\[$2\]/;    # lines starting with hash are a comment
82   s/^\s*-/-/ if $ok == 1;       # obsolete?
83   s/^\s*\*\s*/  * /;
84   if (!/^-/) {
85     s/^\s+-\s*/  - /;
86     s/^\s*/  / unless s/^    \s*/    /;
87   }
88   $ok = 2;
89   print "$_\n";
90 }