#!/usr/bin/perl -w

#TODO
#  check for auto-added captions that are equal to classname

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-2008 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.               #
#                                                                             #
###############################################################################

# Check for things kdesdk/scripts/fixuifiles should have fixed

# 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::Utils;

my($Prog) = "fixuifiles";
my($Version) = "1.1";

# the version number in .ui files cannot be bigger than these values
my $minversion_maj = 4;
my $minversion_min = 0;

&parseArgs();

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

my($f) = $ARGV[0];
open(F, "$f") || die "Couldn't open $f";

my($cnt) = 0;
my($linecnt) = 0;
my($line);

while ($line = <F>) {
  $linecnt++;

  # checks for designer version
  if ( $line =~ m/version=\"([0-9]+)\.([0-9]+)(\.[0-9]+)?\"/ ) {
    my $version_maj = $1;
    my $version_min = $2;
    if ( $version_maj > $minversion_maj ||
	 ( $version_maj == $minversion_maj && $version_min > $minversion_min ) ) {
      $cnt++;
    }
  }

  # checks for auto-added Alt+letter accelerators - these are untranslatable
  if ( $line =~ m/<string>Alt\+[A-Z]<\/string>/ ) {
    $cnt++;
  }

  # checks for auto-added captions that are equal to classname
  #TODO
}
close(F);

if (!$cnt) {
  print "okay\n" if (!&quietArg());
  Exit 0;
} else {
  print "fixuifiles needs to be run on this file\n" if (!&quietArg());
  Exit $cnt;
}

sub Help {
  print "Check for files that need to be fixed by 'fixuifiles'\n";
  Exit 0 if &helpArg();
}

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

sub Explain {
  print "Please run the trunk/kdesdk/fixuifiles on all your .ui files as there are some artifacts created by Qt designer that are undesireable for KDE.\n";
  Exit 0 if &explainArg();
}
