#!/usr/bin/perl

require 'getopts.pl';

$debug = 0;
$message_file = "/usr/local/clx/config/adv_txt";

$verbose = 0;
$missing_only = 0;

&Getopts('mv');

if (length($opt_m) > 0) {
    $missing_only = 1;
}

if (length($opt_v) > 0) {
    $verbose = 1;
}


if ( $#ARGV != 0 ) {
	print "\nChecks a translated adv_txt file for completeness.\n";
        print "Usage: check_adv_txt <filename>\n\n";
        exit(1);
}


foreach $i ($message_file,@ARGV[0]) {

	open(MSG,"< $i") || die "Could not open $i";
	
	if ( $i eq $message_file ) {
		$to='$msg';
	} else {
		$to='$trans';
	}
	
	while (<MSG>) {
	        chop;
		next if /^[0-9]+\*/;
	        ($nr,$text) = split(/[\t\s]+/,$_,2);
	
	        # Ignore any `*' and `?'
	        $nr =~ s/[\*\?]//;
	
		# Save the %, \, " and ' characters
		$text =~ s/%/%%/g;
		$text =~ s/\\/\\\\/g;
		$text =~ s/\"/\\"/g;
		$text =~ s/\'/\\'/g;
	
		# Now construct a new command ...
		$cmd = sprintf("%s[$nr]=\"$text\"",$to);
	
		# ...and execute it
		eval $cmd;
	
		if ( $debug ) {
			printf("Msg #%s, text: %s\n",$nr,$text);
			print $cmd,"\n";
		}
	}
	close(MSG);
}

foreach ($i=1;$i<10000;$i++) {
	if (! $msg[$i] eq "") {
	if ( $missing_only == 0 ) {
		print "Nr. $i\n";
		print "$msg[$i]\n";
		if (! $trans[$i] eq "") {
			 print "$trans[$i]\n";
		} else {
			print "*************** MISSING *************\n";
		}
		print "\n\n";
	} else {
		if ($trans[$i] eq "") {
			print "Nr. $i\n";
			print "$msg[$i]\n";
			print "\n\n";
		}
	}
	
	}
}
