#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell
###############################################################################
# Sanity check plugin for the Krazy project.                                  #
# Copyright (C) 2006-2007 by Allen Winter <winter@kde.org>                    #
#                                                                             #
# 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, write to the Free Software                 #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. #
#                                                                             #
###############################################################################

# Tests KDE source for proper include directives

# Program options:
#   --help:          print one-line help message and exit
#   --version:       print one-line version information and exit
#   --explain:       print an explanation with solving instructions, then exit
#   --quiet:         suppress all output messages
#   --verbose:       print the offending content

# Exits with status=0 if test condition is not present in the source;
# else exits with the number of failures encountered.

use strict;
use Getopt::Long;
use Cwd 'abs_path';
use File::Basename;

my($Prog) = "includes";
my($Version) = "1.0";

my($help) = '';
my($version) = '';
my($explain) = '';
my($quiet) = '';
my($verbose) = '';

exit 1
if (!GetOptions('help' => \$help, 'version' => \$version,
		'explain' => \$explain,
		'verbose' => \$verbose, 'quiet' => \$quiet));

&Help() if $help;
&Version() if $version;
&Explain() if $explain;
if ($#ARGV != 0){ &Help(); exit 0; }

# Check Condition
my($f) = $ARGV[0];
open(F, "$f") || die "Couldn't open $f";
my($topmod) = &topModule($f);
my($subdir) = &topSubdir($f);
my($dir) = &topDir($f);
my($incpath,$p1,$p2);
my($cnt) = 0;
my($linecnt) = 0;
my($line);
my($lstr) = "";
while ($line = <F>) {
  $linecnt++;
  chomp($line);

  if ($line =~ m+^[[:space:]]*#include[[:space:]]".*/.*"+) {
    $incpath = &incPath($line);
    # if the incpath basename is in $dir
    $p1 = $dir . "/" . basename($incpath);
    # of the incpath is not in $topmod
    $p2 = $topmod . "/" . $incpath;
    if (-e $p1 || ! -e $p2) {
      $cnt++;
      if ($cnt == 1) {
	$lstr = "line\#" . $linecnt;
      } else {
	$lstr = $lstr . "," . $linecnt;
      }
      print "=> $line\n" if ($verbose);
    }
    next;
  }

  if ($line =~ m+^[[:space:]]*#include[[:space:]]".*"+) {
    # the incpath must reside in $topmod or in $dir
    $incpath = &incPath($line);
    next if ($incpath =~ m/\.moc$/);
    next if ($incpath eq "config.h");
    next if ($dir =~ m+kdepimlibs/kcal+ && $incpath eq "htmlexportsettings.h");
    next if ($dir =~ m+kdepimlibs/kabc+ && $incpath eq "addressee.h");
    $p1 = $topmod . "/" . $incpath;
    $p2 = $dir . "/". $incpath;
    if (! -e $p1 && ! -e $p2 ) {
      $cnt++;
      if ($cnt == 1) {
	$lstr = "line\#" . $linecnt;
      } else {
	$lstr = $lstr . "," . $linecnt;
      }
      print "=> $line\n" if ($verbose);
    }
    next;
  }

  if ($line =~ m+^[[:space:]]*#include[[:space:]]\<.*\>+) {
    # the incpath must NOT reside in $topmod or in $dir
    $incpath = &incAnglePath($line);
    $p1 = $topmod . "/" . $incpath;
    $p2 = $dir . "/". $incpath;
    if (-e $p1 || -e $p2 ) {
      $cnt++;
      if ($cnt == 1) {
	$lstr = "line\#" . $linecnt;
      } else {
	$lstr = $lstr . "," . $linecnt;
      }
      print "=> $line\n" if ($verbose);
    }
  }

  if ($line =~ m+^[[:space:]]*#include[[:space:]]\<.*\>+) {
    $incpath = &incAnglePath($line);
    # no _export.h stuff in angle brackets.
    # no config.h in angle brackets.
    ### No, controversial. deal with new naming method of Qt includes.
    if ($incpath =~ m+_export\.h$+ ||
	$incpath eq "config.h") {
 # || $incpath =~ m+^q.*\.h$+) {
      next if ($incpath eq "qtest_kde.h");
      next if ($incpath =~ m+^q3.*\.h$+);
      $cnt++;
      if ($cnt == 1) {
	$lstr = "line\#" . $linecnt;
      } else {
	$lstr = $lstr . "," . $linecnt;
      }
      print "=> $line\n" if ($verbose);
    }
  }
}
close(F);

if (!$cnt) {
  print "okay\n" if (!$quiet);
  exit 0;
} else {
  print "$lstr ($cnt)\n" if (!$quiet);
  exit $cnt;
}

sub Help {
  print "[TEST] Check for proper include directives\n";
  exit 0 if $help;
}

sub Version {
  print "$Prog, version $Version\n";
  exit 0 if $version;
}

sub Explain {
  print "Enclose the include path with double quotes \"\" only if that path exists within the same module; else enclose the include path with angle brackets <>. If the include file is in the same directory with the source, then simply use the file name in double quotes.";
#print "Use <QFoo> instead of <qfoo.h>";
  exit 0 if $explain;
}

sub topModule {
  my($in) = @_;
  my($apath) = abs_path($in);
  my($top) = $apath;
  $top =~ s+/kde(libs|pimlibs|base|accessibility|addons|admin|artwork|bindings|edu|games|graphics|multimedia|network|pim|sdk|toys|utils|develop|webdev)/.*++;
  my($module) = $apath;
  $module =~ s+$top/++;
  $module =~ s+/.*++;
  return "$top/$module";
}

sub topSubdir {
  my($in) = @_;
  my($apath) = abs_path($in);
  my($top) = $apath;
  $top =~ s+/kde(libs|pimlibs|base|accessibility|addons|admin|artwork|bindings|edu|games|graphics|multimedia|network|pim|sdk|toys|utils|develop|webdev)/.*++;
  my($module) = $apath;
  $module =~ s+$top/++;
  my($subdir);
  ($module,$subdir) = split("/",$module);
  return "$top/$module/$subdir";
}

sub topDir {
  my($in) = @_;
  return dirname(abs_path($in));
}

sub incPath {
  my($in) = @_;
  my($fred);
  $in =~ s+.*include[[:space:]]++;
  ($fred,$in) = split('"',$in);
  return $in;
}

sub incAnglePath {
  my($in) = @_;
  $in =~ s+.*include[[:space:]]++;
  $in =~ s+<++; $in =~ s+>++;
  return $in;
}
