#!/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-2009 by Allen Winter <winter@kde.org>                    #
# Copyright (C) 2006 by Jaison Lee <lee.jaison@gmail.com>                     #
#                                                                             #
# 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 TRUE and FALSE

# 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 FindBin qw($Bin);
use lib "$Bin/../../../../lib";
use Krazy::PreProcess;
use Krazy::Utils;

my($Prog) = "captruefalse";
my($Version) = "1.5";

&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 (skip C source files)
if ($f =~ m/\.c$/) {
  print "okay\n" if (!&quietArg());
  Exit 0;
}
open(F, "$f") || die "Couldn't open $f";
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($true_cnt) = 0;
my($false_cnt) = 0;
my($line_cnt) = 0;
my($true_str) = "";
my($false_str) = "";

my($line);
foreach my $line (@lines) {
  if ($line =~ m+//.*[Kk]razy:excludeall=.*$Prog+ ||
      $line =~ m+//.*[Kk]razy:skip+) {
    $true_cnt = $false_cnt = 0;
    last;
  }
  $line_cnt++;
  next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);

  {
    if ($line =~ m/TRUE/) {
      last if ($line =~ m/\".*TRUE.*\"/);
      last if ($line =~ m/\w+TRUE/);
      last if ($line =~ m/TRUE\w+/);
      last if ($line =~ m/\/\/.*TRUE/);
      $true_cnt++;
      if ($true_cnt == 1) {
        $true_str = "using TRUE line\#" . $line_cnt;
      } else {
        $true_str = $true_str . "," . $line_cnt;
      }
      print "=> $line\n" if (&verboseArg());
    }
  }

  {
    if ($line =~ m/FALSE/) {
      last if ($line =~ m/\".*FALSE.*\"/);
      last if ($line =~ m/\w+FALSE/);
      last if ($line =~ m/FALSE\w+/);
      last if ($line =~ m/\/\/.*FALSE/);
      $false_cnt++;
      if ($false_cnt == 1) {
        $false_str = "using FALSE line\#" . $line_cnt;
      } else {
        $false_str = $false_str . "," . $line_cnt;
      }
      print "=> $line\n" if (&verboseArg());
    }
  }
}

if (!$true_cnt && !$false_cnt) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  print "$true_str ($true_cnt)\n" if (!&quietArg() && $true_cnt);
  print "$false_str ($false_cnt)\n" if (!&quietArg() && $false_cnt);
  Exit $true_cnt + $false_cnt;
}

sub Help {
  print "Check for TRUE and FALSE macros\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "The TRUE and FALSE macros are obsolete and should be replaced with true and false (all lower case) respectively.\n";
  Exit 0 if &explainArg();
}

