# patch-systems -- lintian check script -*- perl -*-
#
# Copyright (C) 2007 Marc Brockschmidt
#
# 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::patch_systems;
use strict;
use lib "$ENV{'LINTIAN_ROOT'}/checks/";
use common_data;
use Dep;
use Tags;
use Util;

sub run {
	my ($pkg, $type) = @_;

	unless (-d "fields") {
    	fail("directory in lintian laboratory for $type package $pkg missing: fields");
	}

	#Some (cruft) checks are valid for every patch system, so we need to record that:
	my $uses_patch_system = 0;

	#Get build deps so we can decide which build system the maintainer
	#meant to use:
	my $build_deps = "";
    if (open(IN, '<', "fields/build-depends")) {
		local $/ = undef;
        chomp($build_deps .= <IN>);
		close(IN);
    }
	if (open(IN, '<', "fields/build-depends-indep")) {
		local $/ = undef;
		chomp($build_deps .= <IN>);
		close(IN);
	}
	$build_deps = Dep::parse($build_deps);

	#----- dpatch
	if (Dep::implies($build_deps, Dep::parse("dpatch"))) {
		$uses_patch_system++;
		#check for a debian/patches file:
		if (! -r "debfiles/patches/00list") {
			tag "dpatch-build-dep-but-no-patch-list", $pkg;
		} else {
			if (open(IN, '<', "debfiles/patches/00list")) {
				my @patches;
				while(<IN>) {
					chomp;
					next if (/^\#/); #ignore comments
					next if (/^\s*$/); #ignore blank lines
					push @patches, split(' ', $_);
				}
				close(IN);

				# Check each patch.
				foreach my $patch_file (@patches) {
					if (! -r "debfiles/patches/$patch_file" && ! -r "debfiles/patches/$patch_file.dpatch") {
						tag "dpatch-index-references-non-existant-patch", $patch_file;
					}
					if (open(PATCH_FILE, '<', "debfiles/patches/" . $patch_file)) {
						my $has_comment = 0;
						while (<PATCH_FILE>) {
							#stop if something looking like a patch starts:
							last if /^---/;
							#note comment if we find a proper one
							$has_comment = 1 if (/^\#+\s*DP:\s*(.*)$/ && $1 !~ /^no description\.?$/i)
						}
						close(PATCH_FILE);
						unless ($has_comment) {
							tag "dpatch-missing-description", $patch_file;
						}
					}
				}
			}
		}
	}

	#----- general cruft checking:
	if ($uses_patch_system) {
		if ($uses_patch_system > 1) {
			#probably a bug too use more than one system, but we don't check anything but dpatch at the moment
		}

		open(STAT, '<', "diffstat") or fail("cannot open diffstat file: $!");
		while (<STAT>) {
			my ($file) = (m,^\s+(.*?)\s+\|,)
			     or fail("syntax error in diffstat file: $_");

			if ($file !~ /^debian/) {
				tag "patch-system-but-direct-changes-in-diff", $file;
			}
		}
		close (STAT) or fail("error reading diffstat file: $!");
	}
}

1;

# Local Variables:
# indent-tabs-mode: t
# cperl-indent-level: 8
# End:
# vim: syntax=perl sw=4 ts=4 noet shiftround
