# debhelper format -- lintian check script

# Copyright (C) 1999 by Joey Hess
#
# 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., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

package Lintian::debhelper;
use strict;
use Tags;
use Util;

sub run {

my $pkg = shift;
my $type = shift;

use lib "$ENV{'LINTIAN_ROOT'}/lib";
use Dep;

my %commands;

my $seencommand = '';
my $needbuilddepends = '';
my $needtomodifyscripts = '';
my $needversiondepends = '';
my $seenversiondepends = '0';
my $compat = '';

# Parse the debian/rules file, and try to figure out if debhelper commands
# are run in it that like to modify maintainer scripts. Those debhelper
# commands can be found by "grep -l autoscript /usr/bin/dh_*", but I'll
# hardcode them here.

map { $commands{$_}=1 } qw(dh_desktop
			   dh_gconf
			   dh_installcatalogs
			   dh_installdebconf
                           dh_installdefoma
                           dh_installdocs
			   dh_installemacsen
			   dh_installinfo
			   dh_installinit
			   dh_installmenu
			   dh_installmime
			   dh_installmodules
			   dh_installwm
			   dh_installxfonts
			   dh_installxmlcatalogs
			   dh_makeshlibs
			   dh_python
			   dh_scrollkeeper
			   dh_suidregister
			   dh_usrlocal
			   );

open(RULES, "debfiles/rules") or fail("cannot read debian/rules: $!");
while (<RULES>) {
    if (m/^\s+(dh_\w+)/) {
        my $dhcommand = $1;
    	if ($dhcommand =~ /dh_testversion(?:\s+(.+))?/) {
	    $needversiondepends = $1 if ($1);
            tag "dh_testversion-is-deprecated", "";
	}
	if ($dhcommand eq 'dh_dhelp') {
	    tag "dh_dhelp-is-deprecated", "";
	}
	if ($dhcommand eq 'dh_suidregister') {
	    tag "dh_suidregister-is-obsolete", "";
	}
	# if command is passed -n, it does not modify the scripts
	if ($commands{$dhcommand} and not m/\s+\-n\s+/) {
	    $needtomodifyscripts = 1;
	}
	$seencommand = 1;
	$needbuilddepends = 1;
    } elsif (/^\s*export\s+DH_COMPAT\s*=\s*(\d+)/) {
	$needversiondepends = $1;
    }
}
close RULES;

return unless $seencommand;

# We may need to make a difference between deb and udeb packages
# so try to find out
my %pkgs;
opendir(BINPKGS, 'control')
    or fail("Can't open control directory.");
while(my $binpkg = readdir(BINPKGS)) {
    if (-d "control/$binpkg") {
        if (open TYPE, "<", "control/$binpkg/xc-package-type") {
            $pkgs{$binpkg} = <TYPE> || 'deb';
        } else {
            $pkgs{$binpkg} = 'deb';
        }
    }
}

# If we got this far, they need to have #DEBHELPER# in their scripts.
# search for scripts that look like maintainer scripts.
opendir(DEBIAN, 'debfiles')
    or fail("Can't open debfiles directory.");
while (defined(my $file=readdir(DEBIAN))) {
    if ($file =~ m/^(?:(.*)\.)?(?:post|pre)(?:inst|rm)$/) {
	
        my $binpkg = $1 || '';
	open(IN,"debfiles/$file")
	    or fail("Can't open debfiles/$file: $!");
	my $seentag = '';
	while (<IN>) {
	    if (m/\#DEBHELPER\#/) {
		$seentag = 1;
		last;
	    }
	}
	close IN;
	
	if ((! $seentag) and $needtomodifyscripts) {
	    tag "maintainer-script-lacks-debhelper-token", "debian/$file" unless $binpkg && $pkgs{$binpkg} && ($pkgs{$binpkg} =~ /udeb/i);
	}
    } elsif ($file =~ m/^compat$/) {
	open IN, "debfiles/$file"
	    or fail("Can't open debfiles/$file: $!");
	$compat = <IN>;
	close IN;
	if ($compat) {
	    chomp $compat;
	    if ($needversiondepends) {
		tag "declares-possibly-conflicting-debhelper-compat-versions", "$needversiondepends $compat";
	    } else {
		$needversiondepends = $compat;
	    }
	} else {
	    tag "debhelper-compat-file-is-empty", "";
	}
    } elsif ($file =~ m/^control$/) {
	open(IN,"debfiles/$file")
	    or fail("Can't open debfiles/$file: $!");
	while (<IN>) {
	    if (m/^(Build-Depends|Build-Depends-Indep):/i) {
		if (m,debhelper\s*\(\s*>(?:>|=)\s*([^)]+),) {  
		    $seenversiondepends = $1;
	        }
	    }
	    if ($needbuilddepends and m/debhelper/) {
		$needbuilddepends = 0; # seen them, all is good
	    }
	    if (m/^\s*$/) { # end of first stanza
		last;
	    }
	}
	if ($needbuilddepends) {
	    tag "package-uses-debhelper-but-lacks-build-depends", "";
	}
	close IN;
    } elsif ($file =~ m/^ex\.|\.ex$/i) {
        tag "dh-make-template-in-source", "debian/$file";
    }
}
closedir(DEBIAN);

if ($needversiondepends and ($needversiondepends > 1) and ! Dep::versions_lte($needversiondepends, $seenversiondepends)) {
    tag "package-lacks-versioned-build-depends-on-debhelper", "$needversiondepends";
}

}

1;

# vim: syntax=perl
