#!/usr/bin/perl -w

###############################################################################
# Sanity checks CMakeLists.txt files.                                         #
# 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. #
#                                                                             #
###############################################################################
#
# A program to check KDE CMakeLists.txt files for common errors.
#
# Program options:
#   --help:          display help message and exit
#   --version:       display version information and exit
#

use strict;
use Getopt::Long;
use File::Basename;
use Tie::IxHash;
use Text::Wrap;
use HTML::Entities;
use POSIX qw (strftime);

my($Prog) = 'cmakelint.pl';
my($Version) = '1.0';

my($help) = '';
my($version) = '';

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

&Help() if ($help);
if ($#ARGV < 0){ &Help(); exit 0; }
&Version() if ($version);

# hash of all issues we need to keep track of
tie my(%Issues), "Tie::IxHash";

my($f,$num_issues,$tot_issues);
$tot_issues=0;
for $f (@ARGV) {
  $num_issues = &processFile($f);
  $tot_issues += $num_issues;

#  &printHeader($f,$num_issues);
#  if (&printResults() != $num_issues) {
#    print "ERROR: Number of incorrect number of issues printed.  Bad programmer!\n";
#    exit 1;
#  }
#  &printFooter();
}
exit $tot_issues;

sub processFile() {
  my($in) = @_;
  print "Processing $in:\n";
  open(IN,"$in") || die "Couldn't open $in";

  my(@lines) = <IN>;
  my($line);
  my($linecnt)=0;
  my($issues)=0;
  #look for "bad" stuff
  foreach $line (@lines) {
    $linecnt++;
    chomp($line);
    $line =~ s/#.*$//; #remove comments
    next if (! $line);                   #skip empty lines
    next if ($line =~ m/^[[:space:]]$/); #skip blank lines

    $issues += &checkLine($line,$linecnt,
			  'DESTINATION[[:space:]]lib[[:space:]]*\)',
			  'replace "bin" with "${LIB_INSTALL_DIR}"');
    $issues += &checkLine($line,$linecnt,
			  'DESTINATION[[:space:]]include[[:space:]]*\)',
			  'replace "bin" with "${INCLUDE_INSTALL_DIR}"');
    $issues += &checkLine($line,$linecnt,
			  'DESTINATION[[:space:]]bin[[:space:]]*\)',
			  'replace "bin" with "${BIN_INSTALL_DIR}"');
  }

  #look for "missing" stuff
  my($has_project) = 0;
  foreach $line (@lines) {
    chomp($line);
    $line =~ s/#.*$//; #remove comments
    next if ($line =~ m/^[[:space:]]$/); #skip blank lines
    if ($line =~ m/[Pp][Rr][Oo][Jj][Ee][Cc][Tt]/) {
      $has_project=1;
      last;
    }
  }
  if (! $has_project) {
    print "\tMissing a PROJECT() command\n";
  }

  close(IN);
  return $issues;
}

sub checkLine {
  my($line,$cnt,$regex,$explain) = @_;
  if ($line =~ m/$regex/) {
    print "\tline#$cnt: $explain\n";
    return 1;
  }
  return 0;
}

#==============================================================================
# Help function: print help message and exit.
sub Help {
  &Version();
  print "Check KDE CMakeLists.txt files for common errors.\n\n";
  print "Usage: $Prog [OPTIONS] FILES\n";
  print "  --help             display help message and exit\n";
  print "  --version          display version information and exit\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;
}

__END__
