# cruft -- lintian check script -*- perl -*-
#
# based on debhelper check,
# Copyright (C) 1999 Joey Hess
# Copyright (C) 2000 Sean 'Shaleh' Perry
# Copyright (C) 2002 Josip Rodin
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::cruft;
use strict;
use Tags;

use Cwd;
use File::Find;
use File::Basename;

my $dir;
my $pkg;
my $atdinbd;

sub run {

$pkg = shift;
my $type = shift;

unless ((not -e "debfiles/files") or (-z "debfiles/files")) {
    tag "debian-files-list-in-source", "";
}

# read architecture file and see if it's a doc package
my $arch;
if (open IN, "fields/architecture") {
    chop($arch = <IN>);
    close IN;
    if ($pkg =~ /-docs?$/ && $arch ne 'all') {
	tag "documentation-package-not-architecture-independent", "";
    }
}

# read build-depends file and see if it depends on autotools-dev
$atdinbd = 0;
if (open IN, "fields/build-depends") {
    my $bd;
    chop($bd = <IN>);
    close IN;
    $atdinbd = 1 if ($bd =~ /,\s*autotools-dev/);
}

my $cwd = cwd;
$dir = readlink "$cwd/unpacked"; # File::Find in Perl 5.8 appears to need it

find(\&find_cruft, "$dir");

} # </run>

sub find_cruft {
    (my $name = $File::Find::name) =~ s,^\Q$dir\E/,,;
    if (-d) {
	# More or less copied from files, but this time it checks the source
	if ($name =~ m,^(.+/)?CVS$,) {
	    tag "source-contains-CVS-dir", "$name";
	} elsif ($name =~ m,^(.+/)?\.svn$,) {
	    tag "source-contains-svn-control-dir", "$name";
	} elsif ($name =~ m,^(.+/)?\.bzr$,) {
	    tag "source-contains-bzr-control-dir", "$name";
	} elsif ($name =~ m,^(.+/)?\{arch\}$,) {
	    tag "source-contains-arch-control-dir", "$name";
	} elsif ($name =~ m,^(.+/)?\.arch-ids$,) {
	    tag "source-contains-arch-control-dir", "$name";
	}
    }

    -f or return; # we just need normal files, for now

    # More or less copied from files, but this time it checks the source
    if ($name =~ m,^(.+/)?svn-commit\.(.+\.)?tmp$,) {
	tag "svn-commit-file-in-source", "$name";
    } elsif ($name =~ m,^(.+/)?\.arch-inventory$,) {
	tag "arch-inventory-file-in-source", "$name";
    } elsif ($name =~ m,^(.+/)?\.\#(.+?)\.\d+(\.\d+)*$,) {
	tag "source-contains-cvs-conflict-copy", "$name";
    } elsif ($name =~ m,^(.+/)?(.+?)\.(r\d+|mine)$,) {
	tag "source-contains-svn-conflict-copy", "$name";
    }

    if ($name =~ m,^(.+/)?config.(?:cache|log|status)$, and $pkg ne "lintian") {
	tag "configure-generated-file-in-source", "$name";
    } elsif ($name =~ m,^(.+/)?config.(?:guess|sub)$, and not $atdinbd) {
	my $b = basename $name;
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $b;
	open F, $b or die "can't open $name: $!";
	while (<F>) {
            last if $. > 10; # it's on the 6th line, but be a bit more lenient
	    if (/^(?:timestamp|version)='(\d+)(.+)'$/ and $1 < 2004) {
		tag "outdated-autotools-helper-file", "$name $1$2";
	    }
	}
	close F;
    }
}
1;

# vim: ts=8 sw=4 noet syntax=perl
