#!/usr/bin/perl -w
###############################################################################
# Runs krazy over the KDE source code                                         #
# Copyright (C) 2006 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. #
#                                                                             #
###############################################################################

use strict;
use Getopt::Long;

my($Prog) = 'krazyall';
my
$VERSION = '1.1'; #split line so MakeMaker can find the version here

my($help) = '';
my($version) = '';
my($verbose) = '';
my($quiet) = '';
my($check) = '';
my($exclude) = '';
my($export) = 'text';

exit 1
if(!GetOptions('help' => \$help, 'version' => \$version,
               'verbose' => \$verbose, 'quiet' => \$quiet,
               'check=s' => \$check, 'exclude=s' => \$exclude,
               'export=s' => \$export));

&Help() if ($help);
&Version() if ($version);

if ($export) {
  $export = lc($export);
  if (($export ne "text") && ($export ne "textlist") &&
      ($export ne "ebn") && ($export ne "html")) {
    print "Unsupported export type \"$export\"... exiting\n";
    exit 1;
  }
}

# Options to pass to the checker programs
my($opts) = "";
$opts .= "--quiet "  if ($quiet);
$opts .= "--verbose " if ($verbose);
$opts .= "--check=$check " if ($check);
$opts .= "--exclude=$exclude " if ($exclude);
$opts .= "--export=$export " if ($export);
$opts .= "--explain" if ($export ne "textlist");

system("find . -name '*.cpp' -o -name '*.cc' -o -name '*.c' -o -name '*.h' -o -name '*.cxx' -o -name '*.hxx' | xargs krazy $opts");

# Help function: print help message and exit.
sub Help {
  &Version();
  print "Run krazy on all the source in the current working directory\n\n";
  print "Usage: $Prog [OPTION]... FILE...\n";
  print "  --help             display help message and exit\n";
  print "  --version          display version information and exit\n";
  print "  --check <prog[,prog1,prog2,...,progN]>\n";
  print "                     run the specified sanity checker program(s) only\n";
  print "  --exclude <prog[,prog1,prog2,...,progN]>\n";
  print "                     do NOT run the specified sanity checker program(s)\n";
  print "  --export <text|ebn|html>\n";
  print "                     output in one of the following formats:\n";
  print "                       text (default)\n";
  print "                       ebn -> English Breakfast Network style\n";
  print "                       html -> plain old html\n";
  print "  --quiet            suppress all output messages\n";
  print "  --verbose          print the offending content for each file processed\n";
  print "\n";
  exit 0 if $help;
}

# Version function: print the version number and exit.
sub Version {
  print "$Prog, version $VERSION\n";
  exit 0 if $version;
}
