#!/usr/bin/env perl
# Copyright (C) 2012 Zvi Gilboa (zgilboa@virginia.edu)
# This script is hereby released to the public domain.
# You may use or modify it at your own risk in any way you see fit.
use strict;
use warnings;

my %glyph_unicode_values;
my %glyph_names;
my %glyph_references;

my $glyph_test;
my $glyph_tex_definition;
my $font_sfd_file;
my $anchor_info_dir;

open (SortedFontGlyphs, $ARGV[0]);
open (GlyphReferences, $ARGV[1]);

$font_sfd_file = $ARGV[2];
$anchor_info_dir = $ARGV[3];

while (<SortedFontGlyphs>) {
  chomp;
  my ($glyph_unicode, $glyph_name) = split (": ", $_); # output from SortGlyphs.pl
  $glyph_unicode_values{$glyph_name} = $glyph_unicode;
  $glyph_names{$glyph_unicode} = $glyph_name;
}

while (<GlyphReferences>) {
  chomp;
  my ($glyph_unicode, $glyph_tex_reference) = split (": ", $_); # input from the user
  $glyph_references{$glyph_unicode} = $glyph_tex_reference;
}

my @sorted_glyphs = sort {
  $glyph_unicode_values{$a} <=> $glyph_unicode_values{$b}
} keys %glyph_unicode_values;

my @sorted_refs = sort { $a <=> $b } keys %glyph_references;

for my $glyph_unicode (@sorted_refs) {
    if (defined $glyph_names{$glyph_unicode} && length $glyph_names{$glyph_unicode} >0) {
        print "FontForgeGetGlyphAnchorInfo.pe $font_sfd_file $glyph_names{$glyph_unicode} \> $anchor_info_dir\/$glyph_names{$glyph_unicode}.AnchorInfo\n\n";
    }
}

close (SortedFontGlyphs);
close (GlyphReferences);
