Added lots more modules from lintian. Maemian appears to work.
[maemian] / collection / changelog-file
1 #!/usr/bin/perl -w
2 # changelog-file -- maemian collector script
3
4 # Copyright (C) 1998 Richard Braakman
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, you can find it on the World Wide
18 # Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
19 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21
22 use strict;
23
24 use lib "$ENV{'MAEMIAN_ROOT'}/lib";
25 use Util;
26
27 ($#ARGV == 1) or fail("syntax: changelog-file <pkg> <type>");
28 my $pkg = shift;
29 my $type = shift;
30
31 -f "fields/package" or fail("changelog-file invoked in wrong directory");
32
33 unlink("changelog");
34
35 # Pick the first of these files that exists.
36 my @changelogs = ("unpacked/usr/share/doc/$pkg/changelog.Debian.gz",
37                "unpacked/usr/share/doc/$pkg/changelog.Debian",
38                "unpacked/usr/share/doc/$pkg/changelog.debian.gz",
39                "unpacked/usr/share/doc/$pkg/changelog.debian",
40                "unpacked/usr/share/doc/$pkg/changelog.gz",
41                "unpacked/usr/share/doc/$pkg/changelog",
42                "unpacked/usr/doc/$pkg/changelog.Debian.gz",
43                "unpacked/usr/doc/$pkg/changelog.Debian",
44                "unpacked/usr/doc/$pkg/changelog.debian.gz",
45                "unpacked/usr/doc/$pkg/changelog.debian",
46                "unpacked/usr/doc/$pkg/changelog.gz",
47                "unpacked/usr/doc/$pkg/changelog");
48
49 my $chl;
50
51 for (@changelogs) {
52     if (-l $_ || -f $_) {
53         $chl = $_;
54         last;
55     }
56 }
57
58 # If the changelog file we found was a symlink, we have to be careful.  It
59 # could be a symlink to some file outside of the laboratory and we don't want
60 # to end up reading that file by mistake.  Relative links within the same
61 # directory or to a subdirectory we accept; anything else is replaced by an
62 # intentinally broken symlink so that checks can do the right thing.
63 if (defined ($chl) && -l $chl) {
64     my $link = readlink $chl or fail("cannot readlink $chl: $!");
65     if ($link =~ /\.\./ || ($link =~ m%/% && $link !~ m%^[^/]+(/+[^/]+)*\z%)) {
66         symlink('file-is-in-another-package', 'changelog')
67             or fail("cannot create changelog symlink: $!");
68         undef $chl;
69     } elsif (! -f $chl) {
70         undef $chl;
71     }
72 }
73
74 # If the changelog was a broken symlink, it will be undefined and we'll now
75 # treat it the same as if we didn't find a changelog and do nothing.  If it
76 # was a symlink, copy the file, since otherwise the relative symlinks are
77 # going to break things.
78 if (not defined $chl) {
79     # no changelog found
80 } elsif ($chl =~ /\.gz$/) {
81     gunzip_file($chl, 'changelog');
82 } elsif (-f $chl && -l $chl) {
83     local $_;
84     open (CHL, '<', $chl) or fail("cannot open $chl: $!");
85     open (COPY, '>', 'changelog') or fail("cannot create changelog: $!");
86     print COPY while <CHL>;
87     close CHL;
88     close COPY;
89 } else {
90     link($chl, "changelog")
91         or fail("cannot link $chl to changelog: $!");
92 }
93
94 # Extract NEWS.Debian files as well, with similar precautious.  Ignore any
95 # symlinks to other packages here; in that case, we just won't check the file.
96 unlink('NEWS.Debian');
97 my $news = "unpacked/usr/share/doc/$pkg/NEWS.Debian.gz";
98 if (-f $news) {
99     if (-l $news) {
100         my $link = readlink $news or fail("cannot readlink $chl: $!");
101         if ($link =~ /\.\./ || ($link =~ m%/% && $link !~ m%^[^/]+(/+[^/]+)*\z%)) {
102             undef $news;
103         } elsif (! -f $news) {
104             undef $news;
105         }
106     }
107     if ($news) {
108         gunzip_file($news, "NEWS.Debian");
109     }
110 }