#!/usr/bin/perl

#     describe_unicodes -- add human readable descriptions to unicodes
#     Copyright © 2005 Anton Zinoviev <anton@lml.bas.bg>

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

#     If you have not received a copy of the GNU General Public License
#     along with this program, write to the Free Software Foundation, Inc.,
#     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;

my $utf = "/usr/share/i18n/charmaps/UTF-8.gz";

sub debug {
    print STDERR "@_";
}

if ($ARGV[0] eq "--help" || $ARGV[0] eq "-h") {
    print STDERR <<EOT;
Usage:	describe_unicodes FILE

Adds a comment lines in FILE for every substring of the form "U+NNNN",
where NNNN are hexadecimal digits.
EOT
    exit 0
}

my $file = $ARGV[0];

my $line = 0;
my %descriptions;
open UTF, "zcat $utf |" or die "$0: zcat $utf: $!\n";
while (<UTF>) {
    $line++;
    last if (/^[[:space:]]*CHARMAP[[:space:]]*$/);
}
while (<UTF>) {
    $line++;
    last if (/^[[:space:]]*END[[:space:]]*CHARMAP[[:space:]]*$/);
    /\<U([0-9a-fA-F]+)\>[[:space:]]+[^[:space:]]+[[:space:]]+(.*)/
	or die "$0: $utf: syntax error on line $line: $_\n";
    $descriptions{hex ($1)} = $2;
}
close UTF;

my @output;
open FILE, "$file" or die "$0: $file: $!\n";
while (<FILE>) {
    push (@output, $_) unless (/^\# U\+[0-9a-fA-F]+:   [^ ]/);
    s/#.*//;
    while (/^.*?U\+([0-9a-fA-F]+)(.*)/) {
	push (@output, 
	      sprintf ("# U+%s:   %s\n", $1, $descriptions{hex ($1)}));
	$_ = $2;
    }
}
close FILE;
    
open FILE, ">$file" or die "$0: $file: $!\n";
foreach (@output) {
    print FILE;
}
close FILE;

