#!/usr/bin/perl

# INSTALLTEX.pl original em ~/339/apresentacoes-em-pdf/mattwelsh

# Time-stamp: <Sun Oct 07 16:20:10 2001  nho@giga.simon>

# nho@giga 30set01
# T1-WGL4.enc : puxei de http://www.radamir.com/tex/ttf-tex.htm

# Installs .ttf fonts for use by TeX/PDFTeX
# By Matt Welsh, mdw@cs.berkeley.edu

# DO NOT SIMPLY RUN THIS SCRIPT! You should read through it to
# understand how it works first. I take no responsibility for damage to
# your TeX installation (or anything else) through use of this script.

# Main TeX installation root
$TEXHOME = "/usr/share/texmf";
# Location of T1-WGL4.enc file
$ENCFILE = "/usr/src/freetype-1.2/contrib/ttf2pk/data/T1-WGL4.enc";

open (CMD, "ls|") || die "Can't run ls\n";
while (<CMD>) {
  chop;
  if ((/(\S+)\.ttf$/) ||
      (/(\S+)\.TTF$/)) {
    $fontnames[$i] = $1;
    $fonts[$i] = $_;
    $i++;
  }
}
close(CMD);

# Places to put TTF font information in TeX installation 
$TTFDIR = "$TEXHOME/fonts/truetype/mdw";
$AFMDIR = "$TEXHOME/fonts/afm/mdw";
$TFMDIR = "$TEXHOME/fonts/tfm/mdw";
$FDDIR = "$TEXHOME/pdftex/ttfonts/";

`mkdir -p $TTFDIR`;
`mkdir -p $AFMDIR`;
`mkdir -p $TFMDIR`;
`mkdir -p $FDDIR`;

$MAPFILE = "$TEXHOME/pdftex/config/ttfonts-mdw.map";

`rm -f $MAPFILE`;

for ($i = 0; $i <= $#fontnames; $i++) {
  print "Processing $fonts[$i]...\n";
  $fontnames[$i] = lc($fontnames[$i]);

  `ttf2afm -e $ENCFILE -o $AFMDIR/rec$fontnames[$i].afm $fonts[$i] 2>/dev/null`;

  open(PIPE, "afm2tfm $AFMDIR/rec$fontnames[$i].afm -T $ENCFILE $TFMDIR/rec$fontnames[$i].tfm|") || die "Can't run afm2tfm\n";
  while (<PIPE>) {
    chop;
    $OUTPUT = $_;
  }
  close(PIPE);
  $OUTPUT2 = "$OUTPUT < $fonts[$i]\n";
  `echo \"$OUTPUT2\" >> $MAPFILE`;

  open(OUT, ">$FDDIR/t1$fontnames[$i].fd") || die "Can't open fd file for writing\n";
  print OUT "\\ProvidesFile{t1$fontnames[$i].fd}[$fontnames[$i]]\n";
  print OUT "\\DeclareFontFamily{T1}{$fontnames[$i]}{}\n";
  print OUT "\\DeclareFontShape{T1}{$fontnames[$i]}{m}{n}{<-> rec$fontnames[$i]}{}\n";
  print OUT "\\endinput\n";
  close OUT;

  `cp $fonts[$i] $TTFDIR`;
}

print "Updating kpathsea database...\n";
# nho@giga 30set01 : linha abaixo d'a pau
#`cd $TEXHOME; mv -f ls-R ls-R.bak; ls -R > ls-R`;
# debian usa:
#`/usr/bin/mktexlsr /usr/share/texmf`;
# para atualizar o database
#ademais, $TEXHOME/ls-R 'e um link e nao um arquivo
if ( -x "/usr/bin/mktexlsr" ) {
  `/usr/bin/mktexlsr /usr/share/texmf`
}
else {
  `cd $TEXHOME; mv -f ls-R ls-R.bak; ls -R > ls-R`
}

print "\nEdit $TEXHOME/pdftex/config/pdftex.cfg and add\n";
print "  map +ttfonts-mdw.map\n";
print "If not already there.\n";

print "Done\n";


