#! perl -w

######################################################################
#
# fontinstall.pl
# (c) 2000 Vincent Zoonekynd
# Installe proprement des fontes TTF ou PostScript
# afin de les utiliser sous LaTeX
# (non terminé)
#
######################################################################

=head1 NAME

       fontinstall.pl - Install TTF or PFB fonts for LaTeX use

=head1 SYNOPSYS

       perl fontinstall.pl

=head1 DESCRIPTION

Looks for *.ttf or *.afm files in the current directory, 
feeds them to ttf2tfm or afm2tfm
and then to fontinst
to get all the possible fonts:
(upright/slanted/italic/upright italic,
small caps/normal,
medium/bold (or fake bold, in the case ther is no bold),
extended/condensed/normal)
and finally installs them.

=head1 NOTES

The fonts are in the current directory and are named *.ttf, *.afm or *.pfb. 

There is a single family in the directory.

The fonts will be installed in $HOME/gnu/Linux/lib/texmf_tmp:
update your texmf.cnf file accordingly.

The dvips configuration file is supposed to be in 
$HOME/gnu/Linux/lib/texmf_tmp/dvips/config/config.ps
and will be altered (for PostScript fonts).

The pdftex configuration file is supposed to be in 
$HOME/gnu/Linux/lib/texmf_tmp/pdftex/config/pdftex.cfg
and will be altered (for TTF fonts).

The main ttfonts.map file is supposed to be in 
$HOME/gnu/Linux/lib/texmf_tmp/fonts/truetype/, 
is supposed to be the concatenation of the *.map files
in this directory (it will be modified).

=head1 TODO

Remove the wealth of temporary files.

Are all the tfm files necessary? Do not the f_*tfm suffice?

It is not yet possible to use another directory. 

One should be allowed to change the default parameters 
for slanted/condensed/extended fonts.

There is no warning if some font 
(such as the upright medium one) is missing. 

Always produce a _complete_ *.fd file.
For instance, when there is no italics, replace 
T1/foo/m/it by T1/foo/m/sl, 
when there are only italics, replace 
T1/foo/m/n by T1/foo/m/ui, etc.

Also produce OT1 files ?

=head1 BUGS

There is no bold variant of the math fonts.

I should also use fontinst to get the math symbols 
that could be present in the fonts.

ttf2afm may crash.

=head1 AUTHOR

       Vincent Zoonekynd <zoonek@math.jussieu.fr>

=cut

#
# BUGS
#
# Less important bugs:
#  - The old *.mtx should be removed (there are sometimes errors in them...)
#    More generally, the directory should be cleaned before starting. 
#    Better solution : work in a temporary directory.
#  - We tacitely assume that there is a single font in the directory 
#    (especially when launching ttf2tfm ou vptovf).
#    solution : create a temporary directory for each font family.
#  - Unexpected results may appear if the same font is present both in 
#    Postscript and TTF formats. 
#

#
# A FAIRE:
#  - Un peu plus d'instructions de débuggage.
#    Une variable $DEBUG.
#    La possibilité de se débarasser (ou non) des messages d'erreur de ttf2tfm.
#  - Il manque le *.sty
#  - TTF + pdf 
#  - Il manque un fichier d'exemple avec le *.sty
#  - Il manque les fontes PostScript
#  - Il faudrait reconnaître des fontes non latines (unicode, symboles)
#    ou à défaut prévoir une option --symbol demandant de n'installer 
#    qu'une seule variante de la fonte. 
#  - Option --install pour l'installer dans l'arborescence
#    (liste des fichiers à installer)
#  - italique droit : il devrait trouver lui-même l'inclinaison.
#  - Possibilité de choisir l'épaisseur du faux gras, les coefficients 
#    (slant, extend) par une option.
#  - Prévoir une option --test qui permettrait de tester les différentes
#    valeurs des paramères
#  - prevoir une option --table qui produise simplement un tableau des
#    caractères présents dans la fonte. 
#    Il serait bon que ce tableau soit complet, ie, donne _tous_ les 
#    caractères, même s'il y en a plus de 256 (fonte PostScript 
#    ou TTF Unicode, par exemple)
#  - Une option pour calculer les bitmaps (à 720dpi et 576dpi) 
#

use strict;

######################################################################

##
## Lecture des options
##
use Getopt::Long;
my %optctl = ();
GetOptions(\%optctl, 
	   "name=s", 
           "roman=s", "italic=s", "bold=s", "bolditalic=s",
	   "symbols", "initials",
	   "condensed=s", "extended=s", "slant=s", "texte=s", "epais=s",
	  );

my $condensed = $optctl{condensed} || .7;
my $extended = $optctl{extended} || 2;
my $slant = $optctl{slant} || .33;
my $texte = $optctl{texte} || "The quick brown fox...";
my $bold = $optctl{epais} || 
  1; # Ce n'est pas un coefficient, mais une distance en points

my $DEBUG = 3;

######################################################################

use constant TRUE  => (0==0);
use constant FALSE => (0==1);

my %fontes = ();

my $pwd = `pwd`;
chomp $pwd;

sub find_files {
  foreach (@_) {
    open(L, "locate $_|");
    my $f = <L>;
    chomp $f;
    cp($f, '.');
    close L;
  }
}

sub write_a_few_files {
  debug(1, "\nWriting a few files");
  find_files(qw(cmbsy10.tfm  cmex10.tfm  cmmi10.tfm  cmmib10.tfm  cmr10.tfm  cmsy10.tfm));
  tftopl();
  write_ENC();
  write_fauxgras();
  write_caracteres();
  write_dotlessj();
  write_no_unfakable_greek();
  write_template_sty();
  write_template_symbols_sty();
  write_template_initials_sty();
  write_template_tex();
  write_template_fontsmpl();
  write_template_initial();
  write_template_table();
  write_template_cm_tex();
  write_template_cmbright_tex();
  write_template_euler_tex();
  write_extendnfss_sty();

  debug(1, "Removing *.map in the current directory");
  unlink 'ttfonts.map';
  unlink 'psfonts.map';
}

sub cp { 
  debug(1, "  Copying $_[0] to $_[1]");
  system 'cp', @_; 
}

sub temp_dir {
  my ($dir, @files) = @_;
  $dir = "$pwd/${dir}_tmp";
  debug(1, "Creating temporary directory $dir");
  mkdir $dir || die "Cannot create temporary directory $dir: $!";
  chdir $dir || die "Cannot move to temporary directory $dir: $!";
  foreach my $f (@files) {
    next unless defined $f;
    debug(1, "Moving file $f to it.");
    cp("$pwd/$f", "$dir/$f");
    if( $f =~ s/\.afm$/\.pfa/ ){
      debug(1, "Moving file $f to it.");
      cp("$pwd/$f", "$dir/$f");
      $f =~ s/\.pfa$/\.pfb/;
      debug(1, "Moving file $f to it.");
      cp("$pwd/$f", "$dir/$f");
    }
  }
  write_a_few_files();
}

sub parent_dir { chdir $pwd; }

######################################################################

debug(1, "\nSetting up paths");
my $texmf = `kpsexpand '\$TEXMFMAIN'`;
$texmf = "$ENV{HOME}/gnu/Linux/lib/texmf.TTF"; # if $DEBUG>1;

my $configps = "$texmf/dvips/config/config.ps";
my $pdftexcfg = "$texmf/pdftex/config/pdftex.cfg";
debug(1, "  Fonts will be installed in $texmf");
debug(1, "  dvips configuration file is $configps");
debug(1, "  pdftex configuration file is $pdftexcfg");

######################################################################

sub remplace {
  my ($in, $out, $a, $b) = @_;
  open(F, $in)     || die "Cannot open $a for reading: $!";
  open(G, ">$out") || die "Cannot open $a for writing: $!";
  while(<F>){
    s/$a/$b/;
    print G;
  }
  close F;
  close G;
}

sub latex {
  my ($f) = @_;
  debug(1, "\nRunning latex $f");
  system 'latex', '--interaction=batchmode', $f;
}

######################################################################
#
#    Recherche des fontes présentes dans le répertoire courrant.
#
######################################################################

# Si on a donné des arguments (en ligne de commande), 
# on ne regarde pas le contrenu du répertoire courrant.
if( defined $optctl{name} ){
  debug(1, "\nNot looking for fonts in the current directory");
  $optctl{name} =~ s/[-_]//;
  debug(1, "The command line said font $optctl{name} was in $optctl{roman} $optctl{bold} $optctl{italic} $optctl{bolditalic}");
  my ($type, $italic_angle);
  $type = ($optctl{roman} =~ m/ttf$/i) ? 'ttf' : 'pfb';
  
  $fontes{$optctl{name}} = 
    {
     type => 'ttf',
     files => [
	      $optctl{roman},
	      $optctl{bold},
	      $optctl{italic},
	      $optctl{bolditalic},
	     ],
    };
  if( defined $optctl{italic} ){
    if( $type eq 'ttf' ){
      open(AFM, "ttf2afm $optctl{italic}|") || 
	warn "Cannot run ttf2afm on $optctl{italic}: $!";
    } else {
      $type="afm";
      open(AFM, "$optctl{italic}") || 
	warn "Cannot open $optctl{italic} for reading: $!";
    }
    while(<AFM>){
      if( m/^ItalicAngle\s+(.*)/ ){
	$italic_angle = $1;
	$italic_angle = $italic_angle /180*3.1415926535897932384626433832795029;
      }
    }
    close AFM;
    $fontes{$optctl{name}}->{italic} = sin($italic_angle)/cos($italic_angle);
  }

} else {
  debug(1, "\nLooking for fonts in the current directory\n");

  opendir(DIR,'.') || die "Cannot open current directory: $!";
  foreach my $file (readdir DIR){
    next unless $file =~ m/(ttf|afm)$/;
    my ($type, $name, $fullname, $family, $italic_angle);
    if( $file =~ m/\.ttf$/i ){
      $type="ttf";
      open(AFM, "ttf2afm $file|") || warn "Cannot run ttf2afm on $file: $!";
    } else {
      $type="afm";
      open(AFM, "$file") || die "Cannot open $file for reading: $!";
    }
    while(<AFM>){
      if( m/^FontName\s+(.*)/ ){ 
	$name = $1;
      }
      if( m/^FullName\s+(.*)/ ){ 
	$fullname = $1;
      }
      if( m/^ItalicAngle\s+(.*)/ ){
	$italic_angle = $1;
	$italic_angle = $italic_angle /180*3.1415926535897932384626433832795029;
      }
    }
    $family = $fullname;
    $family =~ s/Regular//;
    $family =~ s/Sans//;
    $family =~ s/Italic//;
    $family =~ s/SemiBold//i;
    $family =~ s/Bold//;
    $family =~ s/Condensed//;
    $family =~ s/Roman//;
    $family =~ s/[-_]$//g;
    $family =~ y/A-Z/a-z/;
    $family =~ s/[^a-z]//g;
    close(AFM);
    debug(1, "Found $type file $file");
    debug(1, "  family:   $family");
    debug(1, "  name:     $name");
    debug(1, "  fullname: $fullname");
    unless(exists $fontes{$family}){
      $fontes{$family} = {};
      $fontes{$family}->{files} = [undef, undef, undef, undef]; 
    }
    $fontes{$family}->{type} = $type;
    $fontes{$family}->{italic} = sin($italic_angle)/cos($italic_angle)
      if $fullname =~ m/italic/i;
    my $position=0;
    $position  = 2 if $fullname =~ m/italic/i;
    $position += 1 if $fullname =~ m/bold/i;
    ${ $fontes{$family}->{files} }[$position] = $file;
}}
      
######################################################################
#
#    Pour chaque fonte, on lance ttf2tfm ou afm2tfm
#    avec les bonnes options      
#
######################################################################

debug(1, "\nProcessing these fonts");

sub get_fontname {
  my ($file) = @_;
  if($file =~ m/\.ttf$/){
    open(AFM, "ttf2afm $file|") || die "Cannot run ttf2afm on $file: $!";
  } else {
    open(AFM, $file) || die "Cannot opne $file for reading: $!";
  }
  my $result = "";
  while(<AFM>){
    chomp;
    $result = $_ if s/^FontName\s*//;
  }
  close AFM;
  return $result;
}

sub ttfontsmap_to_pdftexmap {
  print STDERR "Converting ttfonts.map to pdftex.map\n";
  open(TTFONTS, "ttfonts.map") || die "Cannot open ttfonts.map for reading: $!";
  open(PDFTEXMAP, ">pdftex.map") || die "Cannot open pdftex.map for writing: $!";
  while(<TTFONTS>){
    next unless m/^([^\s]*)\s+([^\s]*)\s+Slant\=([^\s]*)\s+Extend\=([^\s]*)\s+Encoding\=([^\s]*)/;
    my ($name, $file, $s, $e, $enc) = ($1, $2, $3, $4, $5);
    my $fontname = get_fontname($file);
    print PDFTEXMAP "$name $fontname \" $s SlantFont  $e ExtendFont T1Encoding ReEncodeFont \"  <T1-WGL4.enc <$file\n";
  }
  close TTFONTS;
  close PDFTEXMAP;
}

sub ttf2tfm {
  my ($ttf, $raw, $vf, $arg) = @_;
  my $base = $ttf;
  $base =~ s/\.ttf$//i;
  my $command;

  $command = "ttf2tfm $ttf -q -T T1-WGL4.enc $arg $vf.vpl $raw.tfm >> ttfonts.map";
  debug(2, "$command");
  system "$command 2>/dev/null";
  
  $command = "vptovf $vf.vpl $vf.vf $vf.tfm";
  debug(2, "$command");
  system "$command >/dev/null 2>&1";
}

sub afm2tfm {
  my ($afm, $raw, $vf, $arg) = @_;
  my $base = $afm;
  $base =~ s/\.afm$//i;
  my $command;

  $command = "afm2tfm $afm -t ec.enc $arg $vf.vpl $raw.tfm | perl -p -e 's/\"/\" ECEncoding ReEncodeFont /; s/\$/ <ec.enc <$base.pfb/' >> psfonts.map";
  debug(2, "$command");
  system "$command";
  
  $command = "vptovf $vf.vpl $vf.vf $vf.tfm";
  debug(2, "$command");
  system "$command >/dev/null 2>&1";
}

sub fontinst {
  my ($family, $in, $out, $series, $shape) = @_;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,latin,no_unfakable_greek,cmr10}
            {T1}{T1}{$family}{$series}{$shape}{10}\n";
  $out =~ s/-cork-/-ot1-/;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,latin}
            {OT1}{OT1}{$family}{$series}{$shape}{10}\n";
}

sub fontinst_bold {
  my ($family, $in, $out, $series, $shape) = @_;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,latin,fauxgras}
            {T1}{T1}{$family}{$series}{$shape}{10}\n";
  $out =~ s/-cork-/-ot1-/;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,latin,fauxgras,no_unfakable_greek,cmr10}
            {OT1}{OT1}{$family}{$series}{$shape}{10}\n";
}

sub fontinst_math {
  my ($family, $in, $out) = @_;
  $out =~ s/-cork-/-oml-/;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,cmmi10}
            {OML}{OML}{tutu}{m}{n}{10}\n";
  $out =~ s/-oml-/-omx/;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,cmex10}
            {OMX}{OMX}{tutu}{m}{n}{10}\n";
  $out =~ s/-omx-/-oms/;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,cmsy10}
            {OMS}{OMS}{tutu}{m}{n}{10}\n";
}
sub fontinst_math_bold {
  my ($family, $in, $out) = @_;
  $out =~ s/-cork-/-oml-/;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,cmmib10}
            {OML}{OML}{tutu}{b}{n}{10}\n";
  $out =~ s/-oml-/-oms/;
  print FONTINST "\\installfont{$out}
            {$in,dotlessj,cmbsy10}
            {OMS}{OMS}{tutu}{b}{n}{10}\n";
}


sub tftopl {
  debug(1, "\nTurning *.tfm files into *.pl files");
  opendir(DIR, ".") || die "Cannot open current directory for reading: $!";
  foreach my $tfm (readdir(DIR)){
    next unless $tfm =~ m/\.tfm$/;
    my $pl = $tfm;
    $pl =~ s/\.tfm$/.pl/;
    my $command = "tftopl $tfm > $pl";
    debug(2, "$command");
    system "$command";
  }
  closedir DIR;
}

sub vptovf {
  debug(1, "\nTurning *.vpl files into *.vf files");
  opendir(DIR, ".") || die "Cannot open current directory for reading: $!";
  foreach my $vpl (readdir(DIR)){
    next unless $vpl =~ m/\.vpl$/;
    my $base = $vpl;
    $base =~ s/\.vpl$//;
    my $command = "vptovf $base.vpl $base.vf $base.tfm";
    debug(2, "$command");
    system "$command >/dev/null 2>&1";
  }
  closedir DIR;
}

foreach my $family (keys %fontes) {
  my $fonte = $fontes{$family};
  debug(1, "\nProcessing family $family ($fonte->{type})");
  debug(1, "  normal:      ${$fonte->{files}}[0]") 
    if defined ${$fonte->{files}}[0];
  debug(1, "  bold:        ${$fonte->{files}}[1]") 
    if defined ${$fonte->{files}}[1];
  debug(1, "  italic:      ${$fonte->{files}}[2]") 
    if defined ${$fonte->{files}}[2];
  debug(1, "  bold italic: ${$fonte->{files}}[3]") 
    if defined ${$fonte->{files}}[3];
  debug(1, "");

  # $a : normal
  # $b : bold
  # $c : italic
  # $d : bold italic
  my ($a,$b,$c,$d) = @{ $fonte->{files} };

  # On regarde si la fonte $a a un nom.
  my $I_have_a_name = not ($fonte->{type} eq "ttf" );
  if( $fonte->{type} eq "ttf" ){
    print STDERR "Checking if the font has a name\n";
    print STDERR "  (Running ttf2afm $a)\n";
    open(TTF2AFM, "ttf2afm $a|") || die "Cannot run ttf2afm $a. $!";
    while(<TTF2AFM>){
      if( m/^FontName\s+(.*)/ ){
	$I_have_a_name = TRUE unless $1 =~ m/^\s*$/;
	print STDERR "  It does ($1)\n";
	last;
      }
    }
    if( not $I_have_a_name ){
      print STDERR "  It doesn't --- it cannot be used with pdftex\n";
    }
    close TTF2AFM;
  }

  temp_dir($family, $a, $b, $c, $d);

  debug(1, "Running ttf2tfm or afm2tfm");

  my @caps = qw/normalcaps smallcaps/; # RAJOUTER ALLCAPS
  my @shape = qw/upright slant italic uprightitalic/;
  my @width = qw/normal condensed extended/;
  my @series = qw/medium bold/;

  if( defined $optctl{symbols} or defined $optctl{initials} ){

    # S'il ne s'agit pas d'une fonte textuelle, ie, si on n'a
    # pas besoin de gras, d'italique, de petites capitales, etc.

    my $base = $a;
    $base =~ s/\.(ttf|afm)$//;

    open(FD, ">u$family.fd") || die "Cannot open u$family.fd for writing: $!";
    print FD "\\ProvidesFile{u$family.fd}\n";
    print FD "\\DeclareFontFamily{U}{$family}{}\n";
    print FD "\\DeclareFontShape{U}{$family}{m}{n}{<->${base}}{}\n";
    close FD;
    
    my $command;
    $command = ( $fonte->{type} eq 'ttf' ) ? "ttf2tfm" : "afm2tfm";
    $command .= " $a -q -T T1-WGL4.enc -v $base.vpl REC_$base.tfm >> ttfonts.map";
    debug(2, "$command");
    system "$command 2>/dev/null";
    
    $command = "vptovf $base.vpl $base.vf $base.tfm";
    debug(2,"$command");
    system "$command 2>/dev/null";

    remplace("template.symbols.sty", "$family.sty", "toto", $family);

  } else {

    open(FONTINST, ">tmp.fontinst.tex") || die "Cannot open tmp.fontinst.tex for writing: $!";
    print FONTINST "\\input fontinst.sty\n";
    print FONTINST "\\declareencoding{T1ENCODING}{T1}\n";
    print FONTINST "\\declareencoding{ADOBESTANDARDENCODING + ECENCODING}{T1}\n";
    print FONTINST "\\installfonts\n";
    
    open(FD, ">t1$family.fd") || die "Cannot open t1$family.fd for writing: $!";
    print FD "\\ProvidesFile{t1$family.fd}\n";
    print FD "\\DeclareFontFamily{T1}{$family}{}\n";
    
    open(TEX, ">$family.simple.tex") || die "Cannot open file $family.simple.tex for writing : $!";
    print TEX "\\documentclass[a4paper,12pt]{article}\n";
    print TEX "\\usepackage[latin1]{inputenc}\n";
    print TEX "\\begin{document}\n";
    
    foreach my $caps (@caps){
      foreach my $shape (@shape){
	foreach my $width (@width){
	  foreach my $series (@series){
	    
	    # A-t-on de quoi construire la fonte désirée ?
	    next if ($shape =~ m/italic/) and ($series eq "medium")
	      and (not defined $c) ;
	    next if ($shape =~ m/italic/) and ($series eq "bold")
	      and (not defined $d) and (not defined $c);
	    
	    # Faut-il du faux gras ?
	    my $fake_bold = FALSE;
	    if( $series eq "bold" ){
	      if($shape =~ m/italic/){
		unless(defined $d){
		  $fake_bold = TRUE;
		}
	      } else {
		unless(defined $b) {
		  $fake_bold = TRUE;
		}
	      }
	    }
	    
	    # Calcul des valeurs de « series » et « shape » pour LaTeX
	    my $shortseries = "";
	    $shortseries .= "b" if $series eq "bold";
	    $shortseries .= "c" if $width eq "condensed";
	    $shortseries .= "x" if $width eq "extended";
	    $shortseries = "m" if $shortseries eq "";
	    
	    my $shortshape = "";
	    $shortshape .= "sc" if $caps eq "smallcaps";
	    $shortshape .= "sl" if $shape eq "slant";
	    $shortshape .= "it" if $shape eq "italic";
	    $shortshape .= "ui" if $shape eq "uprightitalic";
	    $shortshape = "n" if $shortshape eq "";
	    
	    # Calcul des paramètres à donner à ttf2tfm
	    my $e = 1;
	    $e = $extended if $width eq "extended";
	    $e = $condensed if $width eq "condensed";
	    
	    my $s = 0;
	    $s = $slant if $shape eq "slant";
	    $s = $fonte->{italic} * $e if $shape eq "uprightitalic";
	    
	    my $arg="";
	    $arg .= "-e $e ";
	    $arg .= "-s $s ";
	    $arg .= ($caps eq "smallcaps") ? "-V " : "-v ";
	    
	    # Calcul du nom de la fonte ************************************
	    # Supplier-Family-Weight-Variants-Width-Encoding--Size
	    my $raw="ttf-${family}-${series}-${shape}=${caps}-${width}-wgl4--10";
	    my $cork="ttf-${family}-${series}-${shape}=${caps}-${width}-cork=incomplete--10";
	    my $fontinst="ttf-${family}-${series}-${shape}=${caps}-${width}-cork--10";
	    
	    # Calcul du nom du fichier dont on va partir
	    my $file=$a;
	    if( $shape =~ m/italic/ ){
	      if( $series eq "medium" ){ $file = $c }
	      else { 
		if( not defined $d ){ $file = $c }
		else { $file = $d }
	      }
	    } else {
	      if( $series eq "medium" ){ $file = $a }
	      else {
		if( not defined $b ){ $file = $a }
		else { $file = $b }
	      }
	    }
	    
	    next unless defined $file;
	    
	    my $message = "";
	    $message = sprintf "%-38s", "$caps/$shape/$width/$series";
	    $message .= sprintf "%-30s", "(T1/$family/$shortseries/$shortshape)";
	    $message .= $fake_bold ? "[fake bold]" : "           ";
	    $message .= "  from $file";
	    debug(1,$message);
	    
	    if( $fonte->{type} eq "ttf" ){
	      ttf2tfm($file, $raw, $cork, $arg);
	    } else {
	      afm2tfm($file, $raw, $cork, $arg);
	    }
	    if( $fake_bold ) { 
	      fontinst_bold($family, $cork, $fontinst, $shortseries, $shortshape);
	    } else {
	      fontinst($family, $cork, $fontinst, $shortseries, $shortshape);
	    }
	    
	    # Si nécessaire, on rajoute aussi les fontes 
	    # mathématiques correspondantes.
	    fontinst_math($family, $cork, $fontinst)
	      if $shortseries eq "m" and $shortshape eq "n";
	    fontinst_math_bold($family, $cork, $fontinst)
	      if $shortseries eq "b" and $shortshape eq "n";

	    my $base = $file;
	    $base =~ s/\.(ttf|afm)$//;
	    
	    print TEX "\\par\\noindent\\mbox{}";
	    print TEX "\\llap{$family/$shortseries/$shortshape\ }";
	    print TEX "{\\usefont{T1}{$family}{$shortseries}{$shortshape}$texte}\n";
	    
	    print FD "\\DeclareFontShape{T1}{$family}{$shortseries}{$shortshape}{<->${fontinst}}{}\n";
	    
	  }}}}

    print FONTINST '\endinstallfonts\bye';
    close FONTINST;
    tftopl();
    debug(1, "\nRunning tex tmp.fontinst.tex");
    system "tex -interaction=batchmode tmp.fontinst.tex";
    vptovf();

    print FD "\\endinput\n";
    close FD;

    print TEX "\\end{document}\n";
    close TEX;

    open(TEMPLATE, "template.sty") || die "Cannot open template.sty for reading: $!";
    open(STY,">$family.sty") || die "Cannot open $family.sty for writing: $!";
    while(<TEMPLATE>){ s/toto/$family/g; print STY $_; }
    close STY;
    close TEMPLATE;

  }

  ttfontsmap_to_pdftexmap()
    if ($fonte->{type} eq "ttf") and $I_have_a_name;

  # A FAIRE : other sample files for math (CM/cmbright/euler/mathtimes/etc.)
  # A FAIRE : pdftex
  remplace("template.tex", "$family.tex", "toto", $family);
  remplace("template_fontsmpl.tex", "$family.fontsmpl.tex", "toto",$family);
  remplace("template_table.tex", "$family.table.tex", "toto",$family);
  remplace("template_initial.tex", "$family.initial.tex", "toto",$family);
  remplace("template.cm.tex", "$family.cm.tex", "toto",$family);
  remplace("template.cmbright.tex", "$family.cmbright.tex", "toto",$family);
  remplace("template.euler.tex", "$family.euler.tex", "toto",$family);

  latex("$family.simple.tex");
  latex("$family.tex");
  latex("$family.fontsmpl.tex");
  latex("$family.table.tex");
  latex("$family.initial.tex");
  latex("$family.cm.tex");
  latex("$family.cmbright.tex");
  latex("$family.euler.tex");

  debug(1, "\nRunning dvips -f $family.simple.dvi (be patient)");
#  system "dvips -o $family.simple.ps $family.simple.dvi >/dev/null 2>&1";

  debug(1, "Création des répertoires $texmf/fonts/*/$family");
  mkdir "$texmf", 0777 ||  die "Cannot mkdir $texmf: $!";
  mkdir "$texmf/tex", 0777 ||  die "Cannot mkdir $texmf/tex/: $!";
  mkdir "$texmf/tex/latex", 0777 ||  die "Cannot mkdir $texmf/tex/latex: $!";
  mkdir "$texmf/tex/latex/$family", 0777 ||  die "Cannot mkdir $texmf/tex/latex/$family: $!";
  mkdir "$texmf/fonts", 0777 ||  die "Cannot mkdir $texmf/fonts: $!";
  mkdir "$texmf/fonts/truetype", 0777 ||  die "Cannot mkdir $texmf/fonts/truetype: $!";
  mkdir "$texmf/fonts/vf/", 0777 ||  die "Cannot mkdir $texmf/fonts/vf/$family: $!";
  mkdir "$texmf/fonts/vf/$family", 0777 ||  die "Cannot mkdir $texmf/fonts/vf/$family: $!";
  mkdir "$texmf/fonts/tfm/", 0777 ||  die "Cannot mkdir $texmf/fonts/tfm/$family: $!";
  mkdir "$texmf/fonts/tfm/$family", 0777 ||  die "Cannot mkdir $texmf/fonts/tfm/$family: $!";
  if( $fonte->{type} eq "ttf" ){
    mkdir "$texmf/fonts/truetype/$family", 0777 ||  die "Cannot mkdir $texmf/fonts/truetype/$family: $!";
  } else {
    mkdir "$texmf/fonts/type1/", 0777 ||  die "Cannot mkdir $texmf/fonts/type1/$family: $!";
    mkdir "$texmf/fonts/type1/$family", 0777 ||  die "Cannot mkdir $texmf/fonts/type1/$family: $!";
    mkdir "$texmf/fonts/afm/", 0777 ||  die "Cannot mkdir $texmf/fonts/afm/$family: $!";
    mkdir "$texmf/fonts/afm/$family", 0777 ||  die "Cannot mkdir $texmf/fonts/afm/$family: $!";
  }

  debug(1, "\nInstallation des fichiers *.vf  dans $texmf/fonts/vf/$family");
  system "cp *.vf $texmf/fonts/vf/$family";

  debug(1, "Installation des fichiers *.tfm dans $texmf/fonts/tfm/$family");
  system "cp *.tfm $texmf/fonts/tfm/$family";

  debug(1, "Installation du fichier $family.sty dans $texmf/tex/latex/$family");
  system "cp $family.sty $texmf/tex/latex/$family";

  debug(1, "Installation du fichier extendnfss.sty dans $texmf/tex/latex");
  system "cp extendnfss.sty $texmf/tex/latex/";

  debug(1, "Installation du fichier *$family.fd dans $texmf/tex/latex/$family");
  system "cp t1$family.fd $texmf/tex/latex/$family";
  system "cp ot1$family.fd $texmf/tex/latex/$family";
  system "cp u$family.fd $texmf/tex/latex/$family";

 if( $fonte->{type} eq "ttf" ){

    debug(1, "Installation du fichier T1-WGL4.enc dans $texmf/fonts/truetype");
    system "cp T1-WGL4.enc $texmf/fonts/truetype";

    debug(1, "Installation des fichiers *.ttf dans $texmf/fonts/truetype/$family");
    system "cp *.ttf $texmf/fonts/truetype/$family";

    debug(1, "Installation du fichier $family.map dans $texmf/fonts/truetype");
    system "cp ttfonts.map $texmf/fonts/truetype/$family.map";

    # Utiliser kpsewhich pour avoir TOUS les fichiers *.map *******************

    debug(1, "Mise à jour du fichier $texmf/fonts/truetype/ttfonts.map");
    rename "$texmf/fonts/truetype/ttfonts.map", "$texmf/fonts/truetype/ttfonts.map.old";
    open(MAP, ">$texmf/fonts/truetype/ttfonts.map") || die "Cannot open $texmf/fonts/truetype/ttfonts.map for writing: $!";
    opendir(DIR, "$texmf/fonts/truetype") || die "Cannot open directory $texmf/fonts/truetype for reading: $!";
    foreach (readdir(DIR)){
      next unless m/\.map$/;
      next if m/^ttfonts\.map$/;
      open(A, "$texmf/fonts/truetype/$_") || die "Cannot open `$_' for reading: $!";
      while(<A>){ print MAP }
      close A;
    }
    close MAP;

    debug(1, "Installation du fichier $texmf/pdftex/config/$family.pdftex.map");
    system "cp pdftex.map $texmf/pdftex/config/$family.pdftex.map";
    debug(1, "Mise à jour du fichier $pdftexcfg");
    open(CONF, ">>$pdftexcfg") || die "Cannot open $pdftexcfg for appending: $!";
    print CONF "\nmap +$family.pdftex.map\n";
    close CONF;    
    
  } else { # afm
    debug(1, "Installation des fichiers *.afm dans $texmf/fonts/afm/$family");
    system "cp *.afm $texmf/fonts/afm/$family";
    debug(1, "Installation des fichiers *.pfb dans $texmf/fonts/type1/$family");
    system "cp *.pfb *.pfa $texmf/fonts/type1/$family";
    debug(1, "Installation de $family.map dans $texmf/dvips/config/");
    system "cp psfonts.map $texmf/dvips/config/$family.map";

    debug(1, "Mise à jour du fichier $configps");
    open(CONF, ">>$configps") || die "Cannot open $configps for appending: $!";
    print CONF "\np +$family.map\n";
    close CONF;

#    debug(1, "Mise à jour du fichier $texmf/dvips/config/psfonts.map");
#    system "cp $texmf/dvips/config/psfonts.map $texmf/dvips/config/psfonts.map.old";
#    system "cat $texmf/dvips/config/$family.map >> $texmf/fonts/type1/psfonts.map";

  }

  debug(1, "\nRunning mktexlsr");
  system "mktexlsr";

}

######################################################################
#
# Débuguage
#
######################################################################

sub debug {
  my ($level, $message) = @_;
  my $spaces = "";
  for(my $i=1; $i<$level; $i++){ $spaces .= "  "; }
  print STDERR "$spaces$message\n" if $DEBUG > $level;
}

######################################################################
######################################################################
######################################################################

##
## Fichiers
##

sub write_ENC {
  debug(1, "  Writing T1-WGL4.enc");
  open(F, ">T1-WGL4.enc") || die "Cannot open T1-WGL4.enc for writing: $!";
  print F <<EOF;
% T1-WGL4.enc
%
%
% This is LaTeX T1 encoding for WGL4 encoded TrueType fonts
% (e.g. from Windows 95)
%
%
% Note that /hyphen appears twice (for the T1 code points `hyphen' 0x2d
% and `hyphenchar' 0x7f).
%
%
% LIGKERN space l =: lslash ;
% LIGKERN space L =: Lslash ;
% LIGKERN question quoteleft =: questiondown ;
% LIGKERN exclam quoteleft =: exclamdown ;
% LIGKERN hyphen hyphen =: endash ;
% LIGKERN endash hyphen =: emdash ;
% LIGKERN quoteleft quoteleft =: quotedblleft ;
% LIGKERN quoteright quoteright =: quotedblright ;
% LIGKERN comma comma =: quotedblbase ;
% LIGKERN less less =: guillemotleft ;
% LIGKERN greater greater =: guillemotright ;
%
% LIGKERN f i =: fi ;
% LIGKERN f l =: fl ;
% LIGKERN f f =: ff ;
% LIGKERN ff i =: ffi ;
% LIGKERN ff l =: ffl ;
%
%   We blow away kerns to and from spaces (TeX doesn't have a
%   space) and also remove any kerns from the numbers.
%
% LIGKERN space {} * ; * {} space ;
% LIGKERN zero {} * ; * {} zero ;
% LIGKERN one {} * ; * {} one ;
% LIGKERN two {} * ; * {} two ;
% LIGKERN three {} * ; * {} three ;
% LIGKERN four {} * ; * {} four ;
% LIGKERN five {} * ; * {} five ;
% LIGKERN six {} * ; * {} six ;
% LIGKERN seven {} * ; * {} seven ;
% LIGKERN eight {} * ; * {} eight ;
% LIGKERN nine {} * ; * {} nine ;

/T1Encoding [          % now 256 chars follow
% 0x00
  /grave /acute /circumflex /tilde
  /dieresis /hungarumlaut /ring /caron
  /breve /macron /dotaccent /cedilla
  /ogonek /quotesinglbase /guilsinglleft /guilsinglright
% 0x10
  /quotedblleft /quotedblright /quotedblbase /guillemotleft
  /guillemotright /endash /emdash /compwordmark
  /perthousandzero /dotlessi /dotlessj /ff
  /fi /fl /ffi /ffl
% 0x20
  /visualspace /exclam /quotedbl /numbersign
  /dollar /percent /ampersand /quoteright
  /parenleft /parenright /asterisk /plus
  /comma /hyphen /period /slash
% 0x30
  /zero /one /two /three
  /four /five /six /seven
  /eight /nine /colon /semicolon
  /less /equal /greater /question
% 0x40
  /at /A /B /C
  /D /E /F /G
  /H /I /J /K
  /L /M /N /O
% 0x50
  /P /Q /R /S
  /T /U /V /W
  /X /Y /Z /bracketleft
  /backslash /bracketright /asciicircum /underscore
% 0x60
  /quoteleft /a /b /c
  /d /e /f /g
  /h /i /j /k
  /l /m /n /o
% 0x70
  /p /q /r /s
  /t /u /v /w
  /x /y /z /braceleft
  /bar /braceright /asciitilde /hyphen
% 0x80
  /Abreve /Aogonek /Cacute /Ccaron
  /Dcaron /Ecaron /Eogonek /Gbreve
  /Lacute /Lcaron /Lslash /Nacute
  /Ncaron /Eng /Odblacute /Racute
% 0x90
  /Rcaron /Sacute /Scaron /Scedilla
  /Tcaron /Tcedilla /Udblacute /Uring
  /Ydieresis /Zacute /Zcaron /Zdot
  /IJ /Idot /dmacron /section
% 0xA0
  /abreve /aogonek /cacute /ccaron
  /dcaron /ecaron /eogonek /gbreve
  /lacute /lcaron /lslash /nacute
  /ncaron /eng /odblacute /racute
% 0xB0
  /rcaron /sacute /scaron /scedilla
  /tcaron /tcedilla /udblacute /uring
  /ydieresis /zacute /zcaron /zdot
  /ij /exclamdown /questiondown /sterling
% 0xC0
  /Agrave /Aacute /Acircumflex /Atilde
  /Adieresis /Aring /AE /Ccedilla
  /Egrave /Eacute /Ecircumflex /Edieresis
  /Igrave /Iacute /Icircumflex /Idieresis
% 0xD0
  /Eth /Ntilde /Ograve /Oacute
  /Ocircumflex /Otilde /Odieresis /OE
  /Oslash /Ugrave /Uacute /Ucircumflex
  /Udieresis /Yacute /Thorn /Germandbls
% 0xE0
  /agrave /aacute /acircumflex /atilde
  /adieresis /aring /ae /ccedilla
  /egrave /eacute /ecircumflex /edieresis
  /igrave /iacute /icircumflex /idieresis
% 0xF0
  /eth /ntilde /ograve /oacute
  /ocircumflex /otilde /odieresis /oe
  /oslash /ugrave /uacute /ucircumflex
  /udieresis /yacute /thorn /germandbls
] def

% eof

EOF

close F;
}

sub write_fauxgras {
  debug(1, "  Writing fauxgras.mtx");
  open(F, ">fauxgras.mtx") || die "Cannot open fauxgras.mtx for writing: $!";
  print F <<EOF;
\\relax
\\metrics
\\begingroup
  \\catcode`\\{=11
  \\catcode`\\}=11
  \\catcode`\\(=1
  \\catcode`\\)=2
  \\global\\def\\doit#1(%
  \\ifisglyph(#1)\\then
    \\resetglyph(#1)
      \\glyphspecial(ps: gsave 0 30 360 { )
      \\glyphspecial(ps: dup sin $bold mul exch cos $bold mul translate )
      \\push \\glyph(#1)(1000) \\pop
      \\glyphspecial(ps: } for grestore )
      \\glyph(#1)(1000)
    \\endresetglyph
  \\fi)
  \\catcode`\\{=1
  \\catcode`\\}=2
  \\catcode`\\(=12
  \\catcode`\\)=12
\\endgroup
\\input caracteres
\\endmetrics
EOF
  close F;
}

sub write_dotlessj {
  debug(1, "  Writing dotlessj.mt");
  open(F, ">dotlessj.mtx") || die "Cannot open dotlessj.mtx for writing: $!";
  print F <<EOF;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% This is the file dotlessj.mtx, part of the VFinst package
%% (version 0.95, December, 1997) for PostScript Type1 font 
%% installation.  (Author: Alan Hoenig, ajhjj\@cunyvm.cuny.edu)
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\\relax

This fontinst file sets a dotless j glyph for a TeX
math italic font.  The following code is due to Thierry Bouche,
a paper of whose on virtual math fonts can be found at
ftp://fourier.ujf-grenoble.fr/pub/contrib-tex/gut/mathfonts-pk.ps.gz

\\metrics

\\ifisglyph{j}\\then
\\ifisglyph{dotlessi}\\then
  \\resetglyph{dotlessj}
  \\glyphspecial{ps: gsave gsave}
   \\push
     \\moveup{\\neg{\\depth{j}}}
      \\movert{\\neg{\\width{j}}}
    \\glyphspecial{ps: currentpoint /JDepth exch def /JLeft exch def}
     \\moveup{\\add{\\depth{j}}{\\height{dotlessi}}}
     %\\movert{\\mul{\\width{j}}{2}}
     \\movert{\\scale{\\width{j}}{2500}}
     \\glyphspecial{ps: currentpoint /IHeight exch def /JRight exch def grestore}
     \\glyphspecial{ps:   JLeft JDepth moveto JLeft neg JRight add 0 rlineto
  	0 JDepth neg IHeight add rlineto JLeft neg JRight add neg 0
  	rlineto 0 JDepth neg IHeight add neg rlineto closepath  clip}
     \\pop
     \\glyph{j}{1000}
     \\glyphspecial{ps: grestore}
  	   \\resetdepth{\\depth{j}}
  	   \\resetwidth{\\width{j}}
  	   \\resetitalic{\\italic{j}}
  	   \\resetheight{\\height{dotlessi}}
  \\endresetglyph
\\fi
\\fi

\\endmetrics
EOF
  close F;
}


sub write_caracteres {
  debug(1, "  Writing caracteres.tex");
  open(F, ">caracteres.tex") || die "Cannot open caracteres.tex for writing: $!";
  print F <<EOF;
\\input symboles
\\input chiffres
\\input minuscules
\\input majuscules
EOF
  close F;

  open(F, ">symboles.tex") || die "Cannot open symboles.tex for writing: $!";
  print F <<EOF;
\\doit{quotesinglbase}
\\doit{guilsinglleft}
\\doit{guilsinglright}
\\doit{quotedblleft}
\\doit{quotedblright}
\\doit{quotedblbase}
\\doit{guillemotleft}
\\doit{guillemotright}
\\doit{rangedash}
\\doit{punctdash}
\\doit{compwordmark}
\\doit{perthousandzero}
\\doit{visiblespace}
\\doit{exclam}
\\doit{quotedbl}
\\doit{numbersign}
\\doit{dollar}
\\doit{percent}
\\doit{ampersand}
\\doit{quoteright}
\\doit{parenleft}
\\doit{parenright}
\\doit{asterisk}
\\doit{plus}
\\doit{comma}
\\doit{hyphen}
\\doit{period}
\\doit{slash}
\\doit{colon}
\\doit{semicolon}
\\doit{less}
\\doit{equal}
\\doit{greater}
\\doit{question}
\\doit{at}
\\doit{bracketleft}
\\doit{backslash}
\\doit{bracketright}
\\doit{asciicircum}
\\doit{underscore}
\\doit{quoteleft}
\\doit{braceleft}
\\doit{bar}
\\doit{braceright}
\\doit{asciitilde}
\\doit{hyphenchar}
\\doit{section}
\\doit{exclamdown}
\\doit{questiondown}
\\doit{sterling}
EOF
  close F;

  open(F, ">chiffres.tex") || die "Cannot open chiffres.tex for writing: $!";
  print F <<EOF;
\\doit{eight}
\\doit{five}
\\doit{four}
\\doit{nine}
\\doit{one}
\\doit{seven}
\\doit{six}
\\doit{three}
\\doit{two}
\\doit{zero}
EOF
  close F;

  open(F, ">minuscules.tex") || die "Cannot open minuscules.tex for writing: $!";
  print F <<EOF;
\\doit{aacute}
\\doit{abreve}
\\doit{acircumflex}
\\doit{acute}
\\doit{adieresis}
\\doit{ae}
\\doit{agrave}
\\doit{aogonek}
\\doit{aring}
\\doit{atilde}
\\doit{a}
\\doit{breve}
\\doit{b}
\\doit{cacute}
\\doit{caron}
\\doit{ccaron}
\\doit{ccedilla}
\\doit{cedilla}
\\doit{circumflex}
\\doit{c}
\\doit{dbar}
\\doit{dcaron}
\\doit{dieresis}
\\doit{dotaccent}
\\doit{dotlessi}
\\doit{dotlessj}
\\doit{d}
\\doit{eacute}
\\doit{ecaron}
\\doit{ecircumflex}
\\doit{edieresis}
\\doit{egrave}
\\doit{eogonek}
\\doit{eth}
\\doit{e}
\\doit{ffi}
\\doit{ffl}
\\doit{ff}
\\doit{fi}
\\doit{fl}
\\doit{f}
\\doit{gbreve}
\\doit{germandbls}
\\doit{grave}
\\doit{g}
\\doit{hungarumlaut}
\\doit{h}
\\doit{iacute}
\\doit{icircumflex}
\\doit{idieresis}
\\doit{idotaccent}
\\doit{igrave}
\\doit{ij}
\\doit{i}
\\doit{j}
\\doit{k}
\\doit{lacute}
\\doit{lcaron}
\\doit{lslash}
\\doit{l}
\\doit{macron}
\\doit{m}
\\doit{nacute}
\\doit{ncaron}
\\doit{ng}
\\doit{ntilde}
\\doit{n}
\\doit{oacute}
\\doit{ocircumflex}
\\doit{odieresis}
\\doit{oe}
\\doit{ogonek}
\\doit{ograve}
\\doit{ohungarumlaut}
\\doit{oslash}
\\doit{otilde}
\\doit{o}
\\doit{p}
\\doit{q}
\\doit{racute}
\\doit{rcaron}
\\doit{ring}
\\doit{r}
\\doit{sacute}
\\doit{scaron}
\\doit{scedilla}
\\doit{s}
\\doit{tcaron}
\\doit{tcedilla}
\\doit{thorn}
\\doit{tilde}
\\doit{t}
\\doit{uacute}
\\doit{ucircumflex}
\\doit{udieresis}
\\doit{ugrave}
\\doit{uhungarumlaut}
\\doit{uring}
\\doit{u}
\\doit{v}
\\doit{w}
\\doit{x}
\\doit{yacute}
\\doit{ydieresis}
\\doit{y}
\\doit{zacute}
\\doit{zcaron}
\\doit{zdotaccent}
\\doit{z}
EOF
  close F;

  open(F, ">majuscules.tex") || die "Cannot open majuscules.tex for writing: $!";
  print F <<EOF;
\\doit{AE}
\\doit{Aacute}
\\doit{Abreve}
\\doit{Acircumflex}
\\doit{Acute}
\\doit{Adieresis}
\\doit{Agrave}
\\doit{Aogonek}
\\doit{Aring}
\\doit{Atilde}
\\doit{A}
\\doit{Breve}
\\doit{B}
\\doit{Cacute}
\\doit{Caron}
\\doit{Ccaron}
\\doit{Ccedilla}
\\doit{Cedilla}
\\doit{Circumflex}
\\doit{C}
\\doit{Dbar}
\\doit{Dcaron}
\\doit{Dieresis}
\\doit{Dotaccent}
\\doit{D}
\\doit{Eacute}
\\doit{Ecaron}
\\doit{Ecircumflex}
\\doit{Edieresis}
\\doit{Egrave}
\\doit{Eogonek}
\\doit{Eth}
\\doit{E}
\\doit{FFI}
\\doit{FFL}
\\doit{FF}
\\doit{FI}
\\doit{FL}
\\doit{F}
\\doit{Gbreve}
\\doit{Grave}
\\doit{G}
\\doit{Hungarumlaut}
\\doit{H}
\\doit{IJ}
\\doit{Iacute}
\\doit{Icircumflex}
\\doit{Idieresis}
\\doit{Idotaccent}
\\doit{Igrave}
\\doit{I}
\\doit{J}
\\doit{K}
\\doit{Lacute}
\\doit{Lcaron}
\\doit{Lslash}
\\doit{L}
\\doit{Macron}
\\doit{M}
\\doit{Nacute}
\\doit{Ncaron}
\\doit{Ng}
\\doit{Ntilde}
\\doit{N}
\\doit{OE}
\\doit{Oacute}
\\doit{Ocircumflex}
\\doit{Odieresis}
\\doit{Ogonek}
\\doit{Ograve}
\\doit{Ohungarumlaut}
\\doit{Oslash}
\\doit{Otilde}
\\doit{O}
\\doit{P}
\\doit{Q}
\\doit{Racute}
\\doit{Rcaron}
\\doit{Ring}
\\doit{R}
\\doit{SS}
\\doit{Sacute}
\\doit{Scaron}
\\doit{Scedilla}
\\doit{S}
\\doit{Tcaron}
\\doit{Tcedilla}
\\doit{Thorn}
\\doit{Tilde}
\\doit{T}
\\doit{Uacute}
\\doit{Ucircumflex}
\\doit{Udieresis}
\\doit{Ugrave}
\\doit{Uhungarumlaut}
\\doit{Uring}
\\doit{U}
\\doit{V}
\\doit{W}
\\doit{X}
\\doit{Yacute}
\\doit{Ydieresis}
\\doit{Y}
\\doit{Zacute}
\\doit{Zcaron}
\\doit{Zdotaccent}
\\doit{Z}
\\doit{dotlessI}
\\doit{dotlessJ}
EOF
  close F;
}

sub write_template_sty {
  debug(1, "  Writing template.sty");
  open(TEMPLATE, ">template.sty") || die "Cannot open template.sty for writing: $!";
  print TEMPLATE <<EOF;
\\ProvidesFile{toto.sty}
\\ProvidesPackage{toto}
\\expandafter\\newif\\csname if\@toto\@default\\endcsname
\\csname \@toto\@defaultfalse\\endcsname
\\DeclareOption{default}{\\csname \@toto\@defaulttrue\\endcsname}
\\ProcessOptions
\\expandafter\\def\\csname texttoto\\endcsname#1{{\\fontencoding{T1}\\fontfamily{toto}\\selectfont #1}}
  \\DeclareMathVersion{toto}
  \\DeclareMathVersion{totobold}
  \\SetSymbolFont{letters}{toto}{OML}{toto}{m}{it}
  \\SetSymbolFont{letters}{totobold}{OML}{toto}{b}{it}
  \\SetSymbolFont{symbols}{toto}{OMS}{toto}{m}{n}
  \\SetMathAlphabet{\\mathbf}{toto}{OT1}{toto}{b}{n}
  \\SetSymbolFont{operators}{bold}{OT1}{toto}{m}{n}
  \\SetSymbolFont{operators}{totobold}{OT1}{toto}{b}{n}
\\csname if\@toto\@default\\endcsname
  \\def\\encodingdefault{T1}
  \\def\\familydefault{toto}
  \\def\\bfdefault{b}
  \\mathversion{toto}
\\fi
\\endinput
EOF
}

sub old_write_template_sty {
  debug(1, "  Writing template.sty");
  open(TEMPLATE, ">template.sty") || die "Cannot open template.sty for writing: $!";
  print TEMPLATE <<EOF;
\\ProvidesFile{toto.sty}
\\ProvidesPackage{toto}
\\expandafter\\newif\\csname if\@toto\@default\\endcsname
\\csname \@toto\@defaultfalse\\endcsname
\\DeclareOption{default}{\\csname \@toto\@defaulttrue\\endcsname}
\\ProcessOptions
\\expandafter\\def\\csname texttoto\\endcsname#1{{\\fontencoding{T1}\\fontfamily{toto}\\selectfont #1}}
\\csname if\@toto\@default\\endcsname
  \\def\\encodingdefault{T1}
  \\def\\familydefault{toto}
  \\def\\bfdefault{b}

  \\DeclareSymbolFont{totoletters}{T1}{toto}{m}{it}
  \\SetSymbolFont{totoletters}{bold}{T1}{toto}{b}{it}
  \\DeclareMathAlphabet{\\mathbf}{T1}{toto}{b}{n}
  \\DeclareSymbolFont{operators}{T1}{toto}{m}{n}
  \\SetSymbolFont{operators}{bold}{T1}{toto}{b}{n}
     \\DeclareMathSymbol{a}{\\mathalpha}{totoletters}{`a}
     \\DeclareMathSymbol{b}{\\mathalpha}{totoletters}{`b}
     \\DeclareMathSymbol{c}{\\mathalpha}{totoletters}{`c}
     \\DeclareMathSymbol{d}{\\mathalpha}{totoletters}{`d}
     \\DeclareMathSymbol{e}{\\mathalpha}{totoletters}{`e}
     \\DeclareMathSymbol{f}{\\mathalpha}{totoletters}{`f}
     \\DeclareMathSymbol{g}{\\mathalpha}{totoletters}{`g}
     \\DeclareMathSymbol{h}{\\mathalpha}{totoletters}{`h}
     \\DeclareMathSymbol{i}{\\mathalpha}{totoletters}{`i}
     \\DeclareMathSymbol{j}{\\mathalpha}{totoletters}{`j}
     \\DeclareMathSymbol{k}{\\mathalpha}{totoletters}{`k}
     \\DeclareMathSymbol{l}{\\mathalpha}{totoletters}{`l}
     \\DeclareMathSymbol{m}{\\mathalpha}{totoletters}{`m}
     \\DeclareMathSymbol{n}{\\mathalpha}{totoletters}{`n}
     \\DeclareMathSymbol{o}{\\mathalpha}{totoletters}{`o}
     \\DeclareMathSymbol{p}{\\mathalpha}{totoletters}{`p}
     \\DeclareMathSymbol{q}{\\mathalpha}{totoletters}{`q}
     \\DeclareMathSymbol{r}{\\mathalpha}{totoletters}{`r}
     \\DeclareMathSymbol{s}{\\mathalpha}{totoletters}{`s}
     \\DeclareMathSymbol{t}{\\mathalpha}{totoletters}{`t}
     \\DeclareMathSymbol{u}{\\mathalpha}{totoletters}{`u}
     \\DeclareMathSymbol{v}{\\mathalpha}{totoletters}{`v}
     \\DeclareMathSymbol{w}{\\mathalpha}{totoletters}{`w}
     \\DeclareMathSymbol{x}{\\mathalpha}{totoletters}{`x}
     \\DeclareMathSymbol{y}{\\mathalpha}{totoletters}{`y}
     \\DeclareMathSymbol{z}{\\mathalpha}{totoletters}{`z}
     \\DeclareMathSymbol{A}{\\mathalpha}{operators}{`A}
     \\DeclareMathSymbol{B}{\\mathalpha}{operators}{`B}
     \\DeclareMathSymbol{C}{\\mathalpha}{operators}{`C}
     \\DeclareMathSymbol{D}{\\mathalpha}{operators}{`D}
     \\DeclareMathSymbol{E}{\\mathalpha}{operators}{`E}
     \\DeclareMathSymbol{F}{\\mathalpha}{operators}{`F}
     \\DeclareMathSymbol{G}{\\mathalpha}{operators}{`G}
     \\DeclareMathSymbol{H}{\\mathalpha}{operators}{`H}
     \\DeclareMathSymbol{I}{\\mathalpha}{operators}{`I}
     \\DeclareMathSymbol{J}{\\mathalpha}{operators}{`J}
     \\DeclareMathSymbol{K}{\\mathalpha}{operators}{`K}
     \\DeclareMathSymbol{L}{\\mathalpha}{operators}{`L}
     \\DeclareMathSymbol{M}{\\mathalpha}{operators}{`M}
     \\DeclareMathSymbol{N}{\\mathalpha}{operators}{`N}
     \\DeclareMathSymbol{O}{\\mathalpha}{operators}{`O}
     \\DeclareMathSymbol{P}{\\mathalpha}{operators}{`P}
     \\DeclareMathSymbol{Q}{\\mathalpha}{operators}{`Q}
     \\DeclareMathSymbol{R}{\\mathalpha}{operators}{`R}
     \\DeclareMathSymbol{S}{\\mathalpha}{operators}{`S}
     \\DeclareMathSymbol{T}{\\mathalpha}{operators}{`T}
     \\DeclareMathSymbol{U}{\\mathalpha}{operators}{`U}
     \\DeclareMathSymbol{V}{\\mathalpha}{operators}{`V}
     \\DeclareMathSymbol{W}{\\mathalpha}{operators}{`W}
     \\DeclareMathSymbol{X}{\\mathalpha}{operators}{`X}
     \\DeclareMathSymbol{Y}{\\mathalpha}{operators}{`Y}
     \\DeclareMathSymbol{Z}{\\mathalpha}{operators}{`Z}
     \\DeclareMathSymbol{0}{\\mathalpha}{operators}{`0}
     \\DeclareMathSymbol{1}{\\mathalpha}{operators}{`1}
     \\DeclareMathSymbol{2}{\\mathalpha}{operators}{`2}
     \\DeclareMathSymbol{3}{\\mathalpha}{operators}{`3}
     \\DeclareMathSymbol{4}{\\mathalpha}{operators}{`4}
     \\DeclareMathSymbol{5}{\\mathalpha}{operators}{`5}
     \\DeclareMathSymbol{6}{\\mathalpha}{operators}{`6}
     \\DeclareMathSymbol{7}{\\mathalpha}{operators}{`7}
     \\DeclareMathSymbol{8}{\\mathalpha}{operators}{`8}
     \\DeclareMathSymbol{9}{\\mathalpha}{operators}{`9}
\\fi
\\endinput
EOF
  close TEMPLATE;
}

sub write_template_initials_sty {
  debug(1, "  Writing template.initials.sty");
  open(TEMPLATE, ">template.initials.sty") || 
    die "Cannot open template.initials.sty for writing: $!";
  print TEMPLATE '\ProvidesFile{toto.sty}
\ProvidesPackage{toto}
\expandafter\newif\csname if@toto@default\endcsname
\csname @toto@defaultfalse\endcsname
\DeclareOption{default}{\csname @toto@defaulttrue\endcsname}
\ProcessOptions
\def\texttoto#1{{\fontencoding{U}\fontfamily{toto}\selectfont #1}}
\def\symboltoto#1{{\texttoto{\symbol{#1}}}}
\csname if@toto@default\endcsname
  \def\encodingdefault{U}
  \def\familydefault{toto}
\fi
\endinput
';
  close TEMPLATE;
}

sub write_template_symbols_sty {
  debug(1, "  Writing template.symbols.sty");
  open(TEMPLATE, ">template.symbols.sty") || 
    die "Cannot open template.symbols.sty for writing: $!";
  print TEMPLATE '\ProvidesFile{toto.sty}
\ProvidesPackage{toto}
\expandafter\newif\csname if@toto@default\endcsname
\csname @toto@defaultfalse\endcsname
\DeclareOption{default}{\csname @toto@defaulttrue\endcsname}
\ProcessOptions
\def\texttoto#1{{\fontencoding{U}\fontfamily{toto}\selectfont #1}}
\def\symboltoto#1{{\texttoto{\symbol{#1}}}}
\csname if@toto@default\endcsname
  \def\encodingdefault{U}
  \def\familydefault{toto}
\fi
\endinput
';
  close TEMPLATE;
}


sub write_template_tex {
  debug(1, "  Writing template.tex");
  open(TEX, ">template.tex") || die "Cannot open template.tex for writing:. $!";
  print TEX "\\documentclass[12pt]{article}\n";
  print TEX "\\usepackage[default]{toto}\n";
  print TEX "\\usepackage{extendnfss}\n";
  print TEX "\\parindent=0pt\n";
  print TEX "\\begin{document}\n";

  foreach my $s (qw/m c x b bx bc/){
    foreach my $r (qw/n sl it ui sc scit scui scsl/){
      print TEX "\\fontencoding{\\encodingdefault}\\fontfamily{\\familydefault}\\fontseries{\\seriesdefault}\\fontshape{\\shapedefault}\\selectfont\n";
      print TEX "\\usefont{T1}{toto}{$s}{$r}T1/toto/$s/$r\n\n";
      foreach my $a (qw/\mdseries \cseries \xseries \bseries \bxseries \bcseries \bfseries/){
	foreach my $b (qw/\upshape \slshape \itshape \uishape \scshape \scitshape \scuishape \scslshape/){
	  print TEX"{$a $b \\string$a \\string$b}\\par\n";
	}}
      print TEX "\n\\bigskip\\hrulefill\\bigskip\n";
    }}
  
  print TEX '\end{document}';
  close TEX;
}

sub write_template_fontsmpl {
  debug(1, "  Writing template_fontsmpl.tex");
  open(TEX, ">template_fontsmpl.tex") || 
    die "Cannot open template_fontsmpl.tex for writing: $!";
  select TEX;
  print '\documentclass[a4paper,12pt]{article}
\usepackage{fontsmpl}
\usepackage[default]{toto}
\begin{document}
\fontsample
\end{document}' ;
  close TEX;
}

sub write_template_table {
  debug(1, "  Writing template_table.tex");
  open(TEX, ">template_table.tex") || 
    die "Cannot open template_table.tex for writing: $!";
  select TEX;
print '\documentclass[12pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{toto}
\pagestyle{empty}
\begin{document}';
print "\\begin{tabular}{r|c|c|c|c|c|c|c|c}\n";
for(my $i=0; $i<16; $i++){
  print "$i \& ";
  for(my $j=0; $j<8; $j++){
    print "\\symboltoto{". (16*$i+$j) ."} ";
    print "\& " unless $j==7;
  }
  print "\\\\\n & ";
  for(my $j=8; $j<16; $j++){
    print "\\symboltoto{". (16*$i+$j) ."} ";
    print "\& " unless $j==15;
  }
  print "\\\\\n\\hline\n" unless $i==15;
}
print "\\end{tabular}\n";
print '\end{document}';
  close TEX; 
}

sub write_template_initial {
  debug(1, "  Writing template_initial.tex");
  open(TEX, ">template_initial.tex") || 
    die "Cannot open template_initial.tex for writing: $!";
  select TEX;
  print <<EOF;
\\documentclass[a4paper,12pt]{article}
\\usepackage[latin1]{inputenc}
\\usepackage[T1]{fontenc}
\\usepackage{french}
\\pagestyle{empty}
\\usepackage{lettrine}
\\def\\LettrineFontHook{\\fontfamily{toto}\\fontencoding{U}\\selectfont}
\\begin{document}
\\lettrine[ante={\\llap{u/toto/m/n\\qquad}},lines=3]{A}{insi mon dessein}
n'est pas d'enseigner ici la méthode que chacun doit
suivre pour bien conduire sa raison, mais seulement de faire voir en
quelle sorte j'ai tâché de conduire la mienne. Ceux qui se mêlent de
donner des préceptes, se doivent estimer plus habiles que ceux
auxquels ils les donnent ; et s'ils manquent en la moindre chose, ils
en sont blâmables.  Mais, ne proposant cet écrit que comme une
histoire, ou, si vous l'aimez mieux, que comme une fable, eu laquelle,
parmi quelques exemples qu'on peut imiter, on entrouvera peut-être
aussi plusieurs autres qu'on aura raison de ne pas suivre, j'espère
qu'il sera utile à quelques uns, sans être nuisible à personne, et que
tous me sauront gré de ma franchise.
\\kern 1cm\\kern0pt\\hfill\\mbox{(Descartes, \\emph{Discours de la méthode})}

\\bigskip

\\lettrine[lines=3, ante={\\leftguillemets}]{Q}{uoi} ! 
disais-je en moi-même, après avoir tout aujourd'hui parlé
d'une chose, un livre qui peut-être est le seul au monde où cette
matière se traite si particulièrement, voler de ma bibliothèque sur ma
table, devenir capable de raison, pour s'ouvrir justement à l'endroit
d'une aventure si merveilleuse ; entraîner mes yeux dessus, comme par
force, et fournir ensuite à ma fantaisie les réflexions, et à ma
volonté les desseins que je fais !... Sans doute, continuai-je, les
deux vieillards qui apparurent à ce grand homme, sont ceux-là mêmes
qui ont dérangé mon livre, et qui l'ont ouvert sur cette page, pour
s'épargner la peine de me faire la harangue qu'ils ont faite à Cardan.
\\kern 1cm\\kern0pt\\hfill\\mbox{(Cyrano de Bergerac, 
\\emph{Voyage dans la Lune})}
\\end{document}
EOF
  close TEX; 
}

sub write_template_math {
  print "Soit \$f : \\mathbf R \\longrightarrow \\mathbf V\$ une application continue\n";
  print "et \$n \\in \\mathbf N\$ un entier. Notons \$a_{i,j}\$...\n";
  print '$F(\alpha) = 1/2$ ';
  print 'dans $\left[0,\infty\right[$, ';
  print '$1 + \cdots + n$, ';
  print '$\mathrm{Card}\, \aleph_0$, ';
  print '$$ \int_0^1 \dfrac{f(x)}{x^2}dx $$';
}

sub write_template_cm_tex {
  debug(1, "  Writing template.cm.tex");
  open(TEX, ">template.cm.tex") || die "Cannot open template.cm.tex for writing:. $!";
  select TEX;
  print "\\documentclass[12pt]{article}\n";
  print "\\usepackage{amsmath,amssymb}\n";
  print "\\usepackage[default]{toto}\n";
  print "\\usepackage{extendnfss}\n";
  print "\\parindent=0pt\n";
  print "\\begin{document}\n";
  write_template_math();
  print '\end{document}';
  close TEX;
}

sub write_template_cmbright_tex {
  debug(1, "  Writing template.cmbright.tex");
  open(TEX, ">template.cmbright.tex") || die "Cannot open template.cmbright.tex for writing:. $!";
  select TEX;
  print "\\documentclass[12pt]{article}\n";
  print "\\usepackage{amsmath,amssymb}\n";
  print "\\usepackage{cmbright}\n";
  print "\\usepackage[default]{toto}\n";
  print "\\usepackage{extendnfss}\n";
  print "\\parindent=0pt\n";
  print "\\begin{document}\n";
  write_template_math();
  print '\end{document}';
  close TEX;
}

sub write_template_euler_tex {
  debug(1, "  Writing template.euler.tex");
  open(TEX, ">template.euler.tex") || die "Cannot open template.euler.tex for writing:. $!";
  select TEX;
  print "\\documentclass[12pt]{article}\n";
  print "\\usepackage{amsmath,amssymb}\n";
  print "\\usepackage[mathcal]{euler}\n";
  print "\\AtBeginDocument{\\usepackage[default]{toto}}\n";
  print "\\usepackage{extendnfss}\n";
  print "\\parindent=0pt\n";
  print "\\begin{document}\n";
  write_template_math();
  print '\end{document}';
  close TEX;
}

sub write_extendnfss_sty {
  debug(1, "  Writing extendnfss.sty");
  open(STY, ">extendnfss.sty") || die "Cannot open extendnfss.sty for writing: $!"; 
  print STY <<EOF;
\\ProvidesFile{extendnfss.sty}
\\ProvidesPackage{extendnfss}
% Shapes: n it sl ui sc scit scsl scui
\\def\\NFSSscshape{\\fontshape{sc}\\selectfont}
\\def\\NFSSupshape{\\fontshape{n}\\selectfont}
\\def\\NFSSitshape{\\fontshape{it}\\selectfont}
\\def\\NFSSslshape{\\fontshape{sl}\\selectfont}
\\def\\NFSSuishape{\\fontshape{ui}\\selectfont}
\\def\\NFSSscshape{\\fontshape{sc}\\selectfont}
\\def\\NFSSscitshape{\\fontshape{scit}\\selectfont}
\\def\\NFSSscslshape{\\fontshape{scsl}\\selectfont}
\\def\\NFSSscuishape{\\fontshape{scui}\\selectfont}

\\def\\scshape{%
  \\def\\tempa{n}%
  \\ifx\\tempa\\f\@shape\\NFSSscshape\\else
  \\def\\tempa{it}%
  \\ifx\\tempa\\f\@shape\\NFSSscslshape\\else
  \\def\\tempa{sl}%
  \\ifx\\tempa\\f\@shape\\NFSSscslshape\\else
  \\def\\tempa{scsl}%
  \\ifx\\tempa\\f\@shape\\relax\\else
  \\def\\tempa{scui}%
  \\ifx\\tempa\\f\@shape\\relax\\else
  \\def\\tempa{scit}%
  \\ifx\\tempa\\f\@shape\\relax\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\def\\slshape{%
  \\def\\tempa{n}%
  \\ifx\\tempa\\f\@shape\\NFSSslshape\\else
  \\def\\tempa{it}%
  \\ifx\\tempa\\f\@shape\\NFSSslshape\\else
  \\def\\tempa{sl}%
  \\ifx\\tempa\\f\@shape\\relax\\else
  \\def\\tempa{scsl}%
  \\ifx\\tempa\\f\@shape\\relax\\else
  \\def\\tempa{scui}%
  \\ifx\\tempa\\f\@shape\\NFSSscslshape\\else
  \\def\\tempa{scit}%
  \\ifx\\tempa\\f\@shape\\NFSSscslshape\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\def\\itshape{%
  \\def\\tempa{n}%
  \\ifx\\tempa\\f\@shape\\NFSSitshape\\else
  \\def\\tempa{it}%
  \\ifx\\tempa\\f\@shape\\NFSSitshape\\else
  \\def\\tempa{sl}%
  \\ifx\\tempa\\f\@shape\\NFSSitshape\\else
  \\def\\tempa{scsl}%
  \\ifx\\tempa\\f\@shape\\NFSSscitshape\\else
  \\def\\tempa{scui}%
  \\ifx\\tempa\\f\@shape\\NFSSscitshape\\else
  \\def\\tempa{scit}%
  \\ifx\\tempa\\f\@shape\\NFSSscitshape\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\def\\upshape{%
  \\def\\tempa{n}%
  \\ifx\\tempa\\f\@shape\\NFSSupshape\\else
  \\def\\tempa{it}%
  \\ifx\\tempa\\f\@shape\\NFSSupshape\\else
  \\def\\tempa{sl}%
  \\ifx\\tempa\\f\@shape\\NFSSupshape\\else
  \\def\\tempa{scsl}%
  \\ifx\\tempa\\f\@shape\\NFSSscshape\\else
  \\def\\tempa{scui}%
  \\ifx\\tempa\\f\@shape\\NFSSscshape\\else
  \\def\\tempa{scit}%
  \\ifx\\tempa\\f\@shape\\NFSSscshape\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\def\\uishape{%
  \\def\\tempa{n}%
  \\ifx\\tempa\\f\@shape\\NFSSuishape\\else
  \\def\\tempa{it}%
  \\ifx\\tempa\\f\@shape\\NFSSuishape\\else
  \\def\\tempa{sl}%
  \\ifx\\tempa\\f\@shape\\NFSSuishape\\else
  \\def\\tempa{scsl}%
  \\ifx\\tempa\\f\@shape\\NFSSscuishape\\else
  \\def\\tempa{scui}%
  \\ifx\\tempa\\f\@shape\\NFSSscuishape\\else
  \\def\\tempa{scit}%
  \\ifx\\tempa\\f\@shape\\NFSSscuishape\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\let\\scitshape\\NFSSscitshape
\\let\\scslshape\\NFSSscslshape
\\let\\scuishape\\NFSSscuishape

% Series: m c x b bc bx

\\def\\NFSSmseries{\\fontseries{m}\\selectfont}
\\def\\NFSScseries{\\fontseries{c}\\selectfont}
\\def\\NFSSxseries{\\fontseries{x}\\selectfont}
\\def\\NFSSbseries{\\fontseries{b}\\selectfont}
\\def\\NFSSbcseries{\\fontseries{bc}\\selectfont}
\\def\\NFSSbxseries{\\fontseries{bx}\\selectfont}

\\def\\mseries{%
  \\def\\tempa{m}%
  \\ifx\\tempa\\f\@series\\NFSSmseries\\else
  \\def\\tempa{x}%
  \\ifx\\tempa\\f\@series\\NFSSmseries\\else
  \\def\\tempa{c}%
  \\ifx\\tempa\\f\@series\\NFSSmseries\\else
  \\def\\tempa{b}%
  \\ifx\\tempa\\f\@series\\NFSSbseries\\else
  \\def\\tempa{bx}%
  \\ifx\\tempa\\f\@series\\NFSSbseries\\else
  \\def\\tempa{bc}%
  \\ifx\\tempa\\f\@series\\NFSSbseries\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\def\\xseries{%
  \\def\\tempa{m}%
  \\ifx\\tempa\\f\@series\\NFSSxseries\\else
  \\def\\tempa{x}%
  \\ifx\\tempa\\f\@series\\NFSSxseries\\else
  \\def\\tempa{c}%
  \\ifx\\tempa\\f\@series\\NFSSxseries\\else
  \\def\\tempa{b}%
  \\ifx\\tempa\\f\@series\\NFSSbxseries\\else
  \\def\\tempa{bx}%
  \\ifx\\tempa\\f\@series\\NFSSbxseries\\else
  \\def\\tempa{bc}%
  \\ifx\\tempa\\f\@series\\NFSSbxseries\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\def\\cseries{%
  \\def\\tempa{m}%
  \\ifx\\tempa\\f\@series\\NFSScseries\\else
  \\def\\tempa{x}%
  \\ifx\\tempa\\f\@series\\NFSScseries\\else
  \\def\\tempa{c}%
  \\ifx\\tempa\\f\@series\\NFSScseries\\else
  \\def\\tempa{b}%
  \\ifx\\tempa\\f\@series\\NFSSbcseries\\else
  \\def\\tempa{bx}%
  \\ifx\\tempa\\f\@series\\NFSSbcseries\\else
  \\def\\tempa{bc}%
  \\ifx\\tempa\\f\@series\\NFSSbcseries\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\def\\bseries{%
  \\def\\tempa{m}%
  \\ifx\\tempa\\f\@series\\NFSSbseries\\else
  \\def\\tempa{x}%
  \\ifx\\tempa\\f\@series\\NFSSbxseries\\else
  \\def\\tempa{c}%
  \\ifx\\tempa\\f\@series\\NFSSbcseries\\else
  \\def\\tempa{b}%
  \\ifx\\tempa\\f\@series\\NFSSbseries\\else
  \\def\\tempa{bx}%
  \\ifx\\tempa\\f\@series\\NFSSbxseries\\else
  \\def\\tempa{bc}%
  \\ifx\\tempa\\f\@series\\NFSSbcseries\\else
  \\relax
  \\fi\\fi\\fi\\fi\\fi\\fi}
\\let\\bxseries\\NFSSbxseries
\\let\\bcseries\\NFSSbcseries
EOF
  close STY;
}
  
sub write_no_unfakable_greek {
  debug(1, "  Writing no_unfakable_greek.mtx");
  open(MTX, ">no_unfakable_greek.mtx") || die "Cannot open no_unfakable_greek.mtx for writing:. $!";
  print MTX <<EOF;
\\relax
\\metrics

\\RequirePackage{ifthen}

\\def\\reallyunfakable#1{
   \\setglyph{#1}
      \\ifisglyph{#1-not}\\then
         \\moveup{\\neg{\\depth{#1-not}}}
         \\glyphrule{
            \\width{#1-not}
         }{
            \\add{\\depth{#1-not}}{\\height{#1-not}}
         }
         \\resetitalic{\\italic{#1-not}}
         \\moveup{\\depth{#1-not}}
      \\else
         \\glyphrule{500}{500}
      \\fi
      \\glyphwarning{missing glyph `#1'}
   \\endsetglyph
}
\\def\\unfakable#1{
  \\let\\iftmp\\iftrue
  \\ifthenelse{\\equal{#1}{Gamma}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Theta}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Lambda}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Xi}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Pi}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Sigma}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Upsilon}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Delta}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Upsilon1}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Phi}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Psi}}{\\let\\iftmp\\iffalse}{}
  \\ifthenelse{\\equal{#1}{Omega}}{\\let\\iftmp\\iffalse}{}
  \\iftmp\\reallyunfakable{#1}\\fi
}

\\endmetrics
EOF
  close MTX
}
