#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);
use integer;

$| = 1;

require $ENV{'ECHO_SCRIPTS_DIR'} . "/share/common_functions.pl";
consoleStatus("Searching for forbidden characters.");
#printlnInConsole('Test starting');
#printlnInContextOutput('testing context output');


# Filter_2_03_find_forbidden_characters.pl
# 2010-04-04
# Wolfgang Schmidle

# This script checks whether the text contains characters that will make 
# the workflow scripts either break oder produce unexpected results

# input: raw text, normally as a whole
# output: a (possibly empty) list of warnings
#printlnInConsole('Test starting');

my $reserved = "";
my %charSuggestions = ();
my @blockBlacklist = ();
my @charBlacklist = ();

sleep(5);

# text input

my @text;
while(<>) { push @text, $_; }


# read default parameters

my $filter = $ENV{'ECHO_SCRIPTS_DIR'} . "/share/mpdl-workflow-settings.pl";
my $result = `perl $filter`;

my $aux = ($result =~ m! aux=([^ ]+) ! ? $1 : "" );

while ($result =~ m! forbidden=([^ ]+)!g) { push @ARGV, "$aux/$1"; }


# read the "parameters" block

my $inText = 0;
my $inParameters = 0;

foreach (@text) {
	last if m!<pb[ ^]!;
	
	if (m!parameters:!) { $inParameters = 1; }
	if ($inParameters) {
		last if m!^ *$!;
		while (m!forbidden *= *([^ \n]+)!) { push @ARGV, "$aux/$1"; }
	}
}


# read helper files

if (@ARGV) {
	while (<>) {
		chomp;
		s!#.*$!!; # remove comments
		s! +$!!;
		next if m!^$!;
	
		if (m!reserved: *([^ ]+)!) { $reserved .= $1; }
		if (m!charSuggestion: *([^ ]+) *= *([^ ]+)!) { $charSuggestions{$1} = $2; }
		if (m!blockBlacklist: *([^ ]+) *= *([^ ]+)!) { push @blockBlacklist, $1; }
		if (m!charBlacklist: *([^ ]+) *= *([^ ]+)!) { push @charBlacklist, $1; }
	}
}


# go through the text

my $i = 0;
$inText = 0;

LINE: foreach (@text) {
	$i++;
	
	$inText = 1 if m!<pb[ >]!;
	next unless $inText;
	
	if (m!([$reserved])!) { 
		#kt warn "Line $i contains the reserved character \"$1\": \n$_\n"; 
		printlnInContextOutput("Line $i contains the reserved character \"$1\": $_");
		next LINE;
	}
	
	foreach my $char (keys %charSuggestions) {
		if (m!$char!)  { 
			#kt warn "Line $i contains \"$char\", which should be replaced by \"$charSuggestions{$char}\":\n$_\n";
			printlnInContextOutput("Line $i contains \"$char\", which should be replaced by \"$charSuggestions{$char}\":\n$_\nPlease add this line to the replacements file:\n$char = $charSuggestions{$char}"); 
			next LINE;
		}
	}

	foreach my $block (@blockBlacklist) {
		if (m!([\p{In$block}])!)  { 
			#kt warn "Line $i contains \"$1\" from Unicode block \"$block\":\n$_\n"; 
			printlnInContextOutput("Line $i contains \"$1\" from Unicode block \"$block\":\n$_\n");
			next LINE;
		}
	}

	foreach my $char (@charBlacklist) {
		if (m!$char!)  { 
			#kt warn "Line $i contains \"$char\":\n$_\n"; 
			printlnInContextOutput("Line $i contains \"$char\":\n$_\n"); 
			next LINE;
		}
	}
}
#printlnInConsole("Test end....That's all");
#printlnInConsole("That's all.");
consoleStatus("Script finished. Please check and edit the configuration file.");

# TO DO:

# weitere Kandidaten: 
# ► in GeometricShapes (in WO1, Archimedes 1565)

# insert_reg verwendet auch ¢ und £. Ebenfalls verbieten? Oder andere Zeichen suchen?

