#!/usr/bin/perl

true and exec '/bin/sh', $0;
__END__ 2>/dev/null || true

# Test to make sure all the tags are actually implemented

# Perl's prove, the shell way :)

####################
#    Copyright (C) 2009 by Raphael Geissert <atomo64@gmail.com>
#
#
#    This file 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 file 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 file.  If not, see <http://www.gnu.org/licenses/>.
####################

set -e

LINTIAN_ROOT=${LINTIAN_ROOT:=$(pwd)}

descs="$(find "$LINTIAN_ROOT/checks/" -name '*desc' -type f)"

total="$(echo "$descs" | wc -l)"

printf "1..%d\n" "$total"

c=1

echo "$descs" |
while read desc; do
    tags="$(sed -n 's/^Tag:\s*//g;T;p' "$desc" | perl -ne 'my @exclude = qw(
		.*-script-but-no-.*-dep
		.*-contains-.*-control-dir
		maintainer-script-needs-depends-on-.*
		.*-contains-.*-file
		.*-contains-cvs-conflict-copy
	    ); my $exclude = join("|", @exclude);
	    print $_ unless ($_ =~ m/^$exclude$/);')"
    missing=
    for tag in $tags; do
	found=yes
	if [ "${desc#$LINTIAN_ROOT/checks/}" = "lintian.desc" ]; then
	    grep -q "$tag" "$LINTIAN_ROOT/frontend/lintian" || found=
	else
	    grep -q "$tag" "${desc%.desc}" || found=
	fi
	[ "$found" ] || {
	    missing="$missing
#   Unimplemented tag '$tag' detected
#   at $desc"
	}
    done

    [ -z "$missing" ] || printf 'not '
    printf 'ok %d - %s has all its tags implemented\n' "$c" "${desc#$LINTIAN_ROOT/}"
    [ -z "$missing" ] || printf '%s\n' "$missing" >&2
    c=$((c+1))
done

exit
