#!/usr/bin/perl -w
#################################################################
# corelfonts                                                    #
# Tony McClelland   11-Feb-2003                                 #
# Copy  the  PostScript  Type1  fonts from the Corel Ventura 5  #
# CD into  the local TeXMF tree and install these fonts for use #
# with TeX and LaTeX. These fonts are from Bitstream.           #
#################################################################

use strict;
use Cwd 'chdir', 'cwd';
use File::Copy;

#################################################################
# Global variables                                              #
#################################################################
my $texmfdir;       # Path to local TeXMF directory
my $coreldir;       # Where the Corel fonts are
my $scriptdir;      # Where this script and support files are
my $afmdir;         # Where subdirectories for .afm files are
my $tfmdir;         # Where subdirectories for .tfm files are
my $vfdir;          # Where subdirectories for .vf files are
my $pfbdir;         # Where subdirectories for .pfb files are
my $latexdir;       # Where the .sty and .fd files go
my $latexbindir;	# Path to the latex/pltotf/vptovf binaries

#################################################################
# init_paths                                                    #
# Set up various paths for the TeXMF tree etc                   #
#################################################################
sub init_paths {
    print "Enter the path for the local TeXMF tree [/usr/local/share/texmf]: ";
    my $response = <STDIN>;
    chomp $response;
    if ($response eq "") {
        $texmfdir = "/usr/local/share/texmf";
    }
    else {
        $texmfdir = $response;
    }

    print "Enter the path for the Corel fonts [/cdrom/Fonts/Type1]: ";
    $response = <STDIN>;
    chomp $response;
    if ($response eq "") {
        $coreldir = "/cdrom/Fonts/Type1";
    }
    else {
        $coreldir = $response;
    }

    print "Enter the full path of the latex executables [/usr/local/bin/]: ";
    $response = <STDIN>;
    chomp $response;
    if ($response eq "") {
        $latexbindir = "/usr/local/bin";
    }
    else {
        $latexbindir = $response;
    }

    $scriptdir = cwd();
    $afmdir = "$texmfdir/fonts/afm/bitstrea";
    $tfmdir = "$texmfdir/fonts/tfm/bitstrea";
    $vfdir = "$texmfdir/fonts/vf/bitstrea";
    $pfbdir = "$texmfdir/fonts/type1/bitstrea";
    $latexdir = "$texmfdir/tex/latex/bitstrea";
    
    # Create the directories if necessary
    mkdir $afmdir unless -e $afmdir ;
    mkdir $tfmdir unless -e $tfmdir;
    mkdir $vfdir  unless -e $vfdir;
    mkdir $pfbdir  unless -e $pfbdir;
    mkdir $latexdir unless -e $latexdir;
}


#################################################################
# Copy fonts from the CD, renaming according to the fontname    #
# scheme. The fonts are arranged on the CD in subdirectories    #
# under $coreldir named by the first letter of the font name.   #
#################################################################
sub copy_fonts {
    # Start off with fonts beginning with 'A'
    my $oldsubdir = "A";
    chdir("$coreldir/$oldsubdir");

    # Prepare a map file for dvips
    open(MAPFILE, ">$texmfdir/dvips/config/bitstrea.map")
    	or die "Unable to open the map file: $!";

    # Use the font name information in fontnames.dat (taken from
    # the file bitstrea.map in the fontinst info on CTAN) to copy
    # the afm and pfb files and build the dvips map file.
    open(FONTNAMES, "$scriptdir/fontnames.dat")
	    or die "Unable to open the fontnames.dat file: $!";

    while (<FONTNAMES>) {
	    chomp;
	    my ($fontname, $prefix, $kbname, $shortname, $shape) = split;
	    my $subdir = substr($fontname, 0, 1);
	
        # Only change directory when the first letter changes
	    if ($subdir ne $oldsubdir) {
		    $oldsubdir = $subdir;
		    chdir("$coreldir/$subdir");
	    }

	    # Set up the name of the AFM and PFB files used by Corel
	    my $fontfile = "${prefix}A___";
	    my $afmfile = "$fontfile.afm";
	    my $pfbfile = "$fontfile.pfb";

	    # Not all the files in the bitstrea.map are on the CD.
	    # Only set up texmf directories for those that are.		
	    if (-e $afmfile) {
		    # Create subdirectories in the texmf tree
		    mkdir "$afmdir/$shortname" unless -e "$afmdir/$shortname";
		    mkdir "$tfmdir/$shortname" unless -e "$tfmdir/$shortname";
		    mkdir "$vfdir/$shortname" unless -e "$vfdir/$shortname";
		    mkdir "$pfbdir/$shortname" unless -e "$pfbdir/$shortname";
		
		    # Copy the font using the fontname scheme
		    copy($afmfile, "$afmdir/$shortname/$kbname.afm");
		    copy($pfbfile, "$pfbdir/$shortname/$kbname.pfb");
		
		    # Put an entry for this font in the map file
		    my $reencode = $kbname;
		    $reencode =~ s/8a/8r/;
		    print MAPFILE "$reencode $fontname \"TeXBase1Encoding ReEncodeFont\" <8r.enc <$kbname.pfb\n";

            # If this font is not already italic or oblique, add a fake
            # slanted variant to the map file
            unless (($fontname =~ /[Oo]blique/) || ($fontname =~ /[Ii]talic/)) {
                $reencode =~ s/8r/o8r/;
                print MAPFILE "$reencode $fontname \".167 SlantFont TeXBase1Encoding ReEncodeFont\" <8r.enc <$kbname.pfb\n";
            }
	    }
    }

    close(FONTNAMES);
    close(MAPFILE);
}


#################################################################
# install_fonts                                                 #
# Install font families by running fontinst on the afm files    #
#################################################################
sub install_fonts {
    my %families;       # font families and rm, sf or tt shapes
    my %famcodes;       # 3 letter codes for font families
    my %btnames;        # Bitstream names for font families

    # Get a list of all the subdirectories containing *.afm files
    # organised by font name, for fontinst to work on
    open(FONTNAMES, "$scriptdir/fontnames.dat")
        or die "Unable to open the file fontnames.dat: $!";

    while (<FONTNAMES>) {
        chomp;
	    my ($fontname, $prefix, $kbname, $shortname, $shape) = split;
        if (-e "$afmdir/$shortname") {
            $families{$shortname} = $shape;	
            $famcodes{$shortname} = substr($kbname, 0, 3);
            $fontname =~ s/BT.*//;      # strip off everything after BT
            $btnames{$shortname} = $fontname;
        }
    }

    close(FONTNAMES);
    
    # Run fontinst in each subdirectory
    foreach my $font (sort keys %families) {
	    chdir "$afmdir/$font";

	    # Create a tex file to allow latex to be run on fontinst with
	    # this family of fonts as the \latinfamily
	    copy("$scriptdir/fontinst.sty", "$afmdir/$font/fontinst.sty");
	    open(FONTINST, ">$afmdir/$font/corelfont.tex")
		    or die "Cannot open the LaTeX file: $!";
	    print FONTINST <<EOF;
\\input fontinst.sty
\\latinfamily{$famcodes{$font}}{} \\bye
EOF
	    system("$latexbindir/latex corelfont.tex");
	
	    # Now create .tfm and .vf files from the fontinst output
	    while (my $vpl = glob("*.vpl")) {
		    system("$latexbindir/vptovf $vpl");
	    }
	    while (my $pl = glob("*.pl")) {
		    system("$latexbindir/pltotf $pl");
	    }
	
	    # Move the .tfm, .vf and .fd files to their destinations
	    while (my $tfm = glob("*.tfm")) {
		    rename($tfm, "$tfmdir/$font/$tfm");
	    }
	    while (my $vf = glob("*.vf")) {
		    rename($vf, "$vfdir/$font/$vf");
	    }
	    while (my $fd = glob("*.fd")) {
		    rename($fd, "$latexdir/$fd");
	    }
	
	    # Clean up the files created by fontinst
	    unlink <*.vpl>;
	    unlink <*.pl>;
	    unlink <*.mtx>;
	    unlink <*.tex>;
	    unlink <*.sty>;
	
	    # Create a LaTeX style file for this font
	    open(LATEXFILE, ">$latexdir/$font.sty");
	    print LATEXFILE "% LaTeX package file for using the Bitstream font $btnames{$font}\n";
	    print LATEXFILE "\\ProvidesPackage{$font}[2003/02/11 Bitstream font $btnames{$font}]\n";
	    print LATEXFILE "\\renewcommand{\\$families{$font}default}{$famcodes{$font}}\n";
	    close(LATEXFILE);
	
	    chdir $afmdir;
    }
}


#################################################################
# Main program                                                  #
#################################################################
init_paths();
copy_fonts();
install_fonts();

print "Done.\n";
