#!/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) 2009-2010 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 includes that could be replaced by CamelCase headers.

# Program options:
#   --help:          print one-line help message and exit
#   --version:       print one-line version information and exit
#   --priority:      report issues of the specified priority only
#   --strict:        report issues with the specified strictness level only
#   --explain:       print an explanation with solving instructions
#   --installed      file is to be installed
#   --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 Cwd 'abs_path';
use FindBin qw($Bin);
use lib "$Bin/../../../../lib";
use Krazy::PreProcess;
use Krazy::Utils;

my($Prog) = "camelcase";
my($Version) = "1.22";

&parseArgs();

&Help() if &helpArg();
&Version() if &versionArg();
&Explain() if &explainArg();
if ($#ARGV != 0){ &Help(); Exit 0; }

my($f) = $ARGV[0];

# open file and slurp it in (C++ only)
if (fileType($f) eq "c++"){
  open(F, "$f") || die "Couldn't open $f";
} else {
  print "okay\n" if (!&quietArg());
  Exit 0;
}
my(@data_lines) = <F>;
close(F);

# Remove C-style comments and #if 0 blocks from the file input
my(@lines) = RemoveIfZeroBlockC( RemoveCommentsC( @data_lines ) );

my($cnt) = 0;
my($linecnt) = 0;
my($lstr) = "";

my($sep,$incpath);
my($line);
while ($linecnt < $#lines) {
  $line = $lines[$linecnt++];
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $cnt = 0;
    last;
  }
  next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);
  $line =~ s+//.*++;  #skip C++ comments

  # get the include path, if there is one
  if ($line =~ m+^[[:space:]]*#include[[:space:]]+) {
    if ($line =~ m+\"+ ) {
      $sep = "\"";
      $incpath = &incPath($line);
    } else {
      $sep = "<";
      $incpath = &incAnglePath($line);
    }
    next if ($sep eq "\"");
    next if ($incpath =~ m:^(lib|kd)\w+/:);
    next if ($incpath =~ m:^phonon/:);
    next if ($incpath =~ m:^boost/:);
    next if (&isCamelCase($incpath));
    next if ($sep eq "<" && $incpath =~ m/^(c|io)/ && $incpath !~ m/\.h$/);
    next if ($sep eq "<" && $incpath =~ m/^config/ && $incpath =~ m/\.h$/);
    next if ($sep eq "<" && $incpath =~ m/^ical/);
    next if ($sep eq "<" && $incpath =~ m/^uuid/);
    next if ($sep eq "<" && $incpath =~ m/^algorithm/);
    next if ($sep eq "<" && $incpath =~ m/^calendarsupport/);
    next if ($sep eq "<" && $incpath =~ m/^messagecomposer/);
    next if ($sep eq "<" && $incpath =~ m/^messagecore/);
    next if ($sep eq "<" && $incpath =~ m/^messagelist/);
    next if ($sep eq "<" && $incpath =~ m/^messageviewer/);
    next if ($sep eq "<" && $incpath =~ m/^incidenceeditor/);
    next if ($incpath eq "kdemacros.h");

    $cnt++;
    if ($cnt == 1) {
      $lstr = "line\#" . $linecnt;
    } else {
      $lstr = $lstr . "," . $linecnt;
    }
    print "=> $line\n" if (&verboseArg());
  }
}
close(F);

if (!$cnt) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  print "$lstr ($cnt)\n" if (!&quietArg());
  Exit $cnt;
}

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

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

sub isCamelCase {
  my($in) = @_;
  if ($in =~ m:^[A-Z]{1,2}\w+[A-Z]{1}: ||
      $in =~ m:^[A-Z]{1,2}\w+/*:) {
    return 1;
  } else {
    return 0;
  }
}

sub Help {
  print "Check for not using CamelCase headers\n";
  Exit 0 if &helpArg();
}

sub Version {
  print "$Prog, version $Version\n";
  Exit 0 if &versionArg();
}

sub Explain {
  print "Qt4, kdelibs, and kdepimlibs provide CamelCase headers. For example, #include <kfoo.h> can be replaced with #include <KFoo>. Using CamelCase headers is up to personal taste in most cases.\n";
  Exit 0 if &explainArg();
}
