#!/usr/local/bin/perl
# Check the camlmsg.txt file.
# (Run this script from the src/ directory)

# Scan the camlmsg.txt file

open(MSG, "camlmsgs.txt") || die("can't open camlmsgs.txt");
while(<MSG>) {
  next if /^\s*$/;
  while (s/\\$//) {
    $cont = <MSG>;
    $cont =~ s/^\s*//;
    $_ .= $cont;
  }
  if (m/^([a-z]+):\s*/) {
    if ($1 eq "src") {
      $sourcemsg = $';
      push(@sourcemsgs, $sourcemsg);
    } else {
      $languages{$1} = 1;
      $translations{"$1/$sourcemsg"} = $';
    }
  } else {
    print STDERR "Ill-formed line: $_";
  }
}
close(MSGS);

# Check that all messages have translations in all languages,
# and that %x appear in the same positions.

foreach $msg (@sourcemsgs) {
  $sourceformats = do formats($msg);
  foreach $lang (keys(%languages)) {
    $transl = $translations{"$lang/$msg"};
    if (!$transl) {
      print STDERR "No translation in $lang for $msg";
    } else {
      if (do formats($transl) ne $sourceformats) {
        print STDERR "Format clash between $msg and $transl";
      }
    }
  }
}

sub formats {
  $_ = $_[0];
  s/^[^%]*//;
  s/%(.)[^%]*/$1/g;
  return $_;
}
   
# Check that we have not missed any error message from the source files

foreach $msg (@sourcemsgs) { $sourcemsgs{$msg} = 'known'; }

open(FIND, "./camlrun tools/findmsgs */*.ml |");
while(<FIND>) {
  next if /^\s*$/;
  while (s/\\$//) {
    $_ .= <FIND>;
  }
  if (! $sourcemsgs{$_}) {
    print STDERR "New message: $_";
  } else {
    $sourcemsgs{$_} = 'used';
  }
}
close(FIND);

foreach $msg (@sourcemsgs) {
  if ($sourcemsgs{$msg} eq 'known') {
    print STDERR "Unused message: $msg";
  }
}


