#!/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) 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 signals and slots vice Q_SIGNALS and Q_SLOTS

# 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';

my($Prog) = "sigandslots";
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; }

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

my($absf) = abs_path($f);
my($LibPath) = ($absf =~ m+/kde.*libs/+ ||
		$absf =~ m+/koffice/libs/+ ||
		$absf =~ m+/kdebase/libkonq/+) ? 1 : 0;
if (!$LibPath) {
  print "n/a (not in a library)\n";
  exit 0;
}
if ($f =~ m/\.h$/ || $f =~ m/\.hxx$/) {
  open(F, "$f") || die "Couldn't open $f";
} else {
  print "okay\n" if (!$quiet);
  exit 0;
}

#open file and slurp it in
open(F, "$f") || die "Couldn't open $f";
my(@data_lines) = <F>;
close(F);

#get all the c-style comments from the file
my($data)="@data_lines";
my(@comments) = ($data =~ /\/\*.*?\*\//gs);

#for each comment, remove everything but the linebreaks, so
#our line numbering report does not get screwed up.
foreach my $comment ( @comments ) {
        my($fixed_comment) = $comment;
        $fixed_comment =~ s/[^\n]//gs;
        $fixed_comment =~ s/\n/\n/gs;
        $data =~ s/\Q$comment/$fixed_comment/s;
}

#put it back into an array so we can iterate over it
my(@lines) = split(/\n/, $data);

# Check Condition
my($linecnt) = 0;
my($line);
my($sig_cnt) = 0;
my($slot_cnt) = 0;
my($sig_str) = "";
my($slot_str) = "";

while ($linecnt < $#lines) {
  $line = $lines[$linecnt++];

  next if ($line =~ m+//.*[Kk]razy:exclude=.*$Prog+);
  $line =~ s+//.*++;  #skip C++ comments

  if ($line =~ m+[[:space:]]*signals[[:space:]]*:+) {
    next if ($line =~ m+[[:space:]]*k_dcop_signals[[:space:]]*:+);
    $sig_cnt++;
    if ($sig_cnt == 1) {
      $sig_str = "line\#" . $linecnt;
    } else {
      $sig_str = $sig_str . "," . $linecnt;
    }
    print "=> $line\n" if ($verbose);
  }
  if ($line =~ m+[[:space:]]*slots[[:space:]]*:+) {
    $slot_cnt++;
    if ($slot_cnt == 1) {
      $slot_str = "line\#" . $linecnt;
    } else {
      $slot_str = $slot_str . "," . $linecnt;
    }
    print "=> $line\n" if ($verbose);
  }
}
close(F);

my($total_count) = $sig_cnt + $slot_cnt;
if (!$total_count) {
  print "okay\n" if (!$quiet);
  exit 0;
} else {
  if (!$quiet) {
    print "signals: $sig_str ($sig_cnt)" if ($sig_cnt);
    print ", " if ($sig_cnt && $slot_cnt);
    print "slots: $slot_str ($slot_cnt)" if ($slot_cnt);
    print "\n";
  }
  exit $total_count;
}

sub Help {
  print "Check for signals: and slots:\n";
  exit 0 if $help;
}

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

sub Explain {
  print "In the libraries, use Q_SIGNALS: and Q_SLOTS: instead of signals: and slots: They are syntactically equivalent and should be used to avoid conflicts with boost signals, and with python's use of \"slots\" in its headers.\n";
  exit 0 if $explain;
}
