#! /bin/sh -f
#
# Take all of the Type 1 .ps files in a directory and generate a fonts.scale
# file containing each of the fonts with scalable entries.
#
# $NCDId: @(#)mkfdir,v 1.3 1994/02/25 23:32:19 lemke Exp $
#
outfile=fonts.scale

PATH=/bin:/usr/bin:/usr/ucb

TMP=/tmp/mkfdir.temp
TMPFILES=/tmp/mkfdir.tempfiles

destdir=$1
if test $# -lt 1; then
  destdir="."
fi

/bin/ls -1 | grep '\.ps$' > $TMPFILES
/bin/ls -1 | grep '\.pfa$' >> $TMPFILES
# some binary files make it barf
#/bin/ls -1 | grep '\.pfb$' >> $TMPFILES

#
# shell functions would be nicer, but some OSes still don't support
# them
#
#Addfont()
#{
#    echo $font with $encoding
#    echo $file" -adobe-"$facename"-"$weight"-"$italic"-"$sWidth"--0-0-0-0-"$pitch"-0-"$registry"-"$encoding >> $TMP
#}
#addfont=Addfont

sayfont='echo $font with $encoding'
addfont='echo $file" -adobe-"$facename"-"$weight"-"$italic"-"$sWidth"--0-0-0-0-"$pitch"-0-"$registry"-"$encoding >> $TMP'

# Clear the temporary file.
echo -n > $TMP

# Get the first line to prime the loop.
line=1
file=`sed -n -e ${line}p $TMPFILES`

while ( test $file ) ; do

  font=`fgrep FontName $file | sed -e 's#.* /##g' -e 's# .*##g'`

  # See if this is an italic or oblique font.
  #
  italic="r"

  temp=`echo $font | sed -e 's/[iI]ta//g'`
  if test "$temp" != "$font" ; then
    italic="i"
  fi

  temp=`echo $font | sed -e 's/[oO]bl//g'`
  if test "$temp" != "$font" ; then
    italic="o"
  fi

  # See if this is a fixed pitch font.
  #
  pitch="P"
  
  temp=`fgrep isFixedPitch $file`
  if test "$temp[2]" = "true" ; then
    pitch="m"
  fi

  # Separate the facename and get the weight.
  #
  facename=`echo $font | sed -e 's/-.*//g'`
  weight=`fgrep Weight $file | sed -e 's/.*(//g' -e 's/).*//g'`

  # Look for variations on Condensed
  #
  sWidth=""
  comp=`echo $font | sed -e "s/$weight//g"`

  # Special prefixes.
  temp=`echo $comp | sed -e 's/[Uu]lt//g'`
  if test "$temp" != "$comp" ; then
    sWidth='ultra '
  fi
  temp=`echo $comp | sed -e 's/[Ee]xt//g'`
  if test "$temp" != "$comp" ; then
    sWidth='extra '
  fi

  # The meat.
  temp=`echo $comp | sed -e 's/[Cc]ond//g'`
  if test "$temp" != "$comp" ; then
    sWidth="${sWidth}condensed"
  else 
    temp=`echo $comp | sed -e 's/[Cc]omp//g'`
    if test "$temp" != "$comp" ; then
      sWidth="${sWidth}compressed"
    else
      temp=`echo $comp | sed -e 's/[Nn]arr//g'`
      if test "$temp" != "$comp" ; then
        sWidth="${sWidth}narrow"
      fi
    fi
  fi

  if test -z "$sWidth" ; then
    sWidth='normal'
  fi

  # Echo the stuff out to std out.

  if `fgrep -s StandardEncoding $file` ; then

    registry="iso8859"
    encoding="1"

    eval $sayfont
    eval $addfont

    encoding="adobe"

    eval $sayfont
    eval $addfont
  
  else

    registry="adobe"
    encoding="fontspecific"

    eval $sayfont
    eval $addfont

  fi

# Grab the next line from the file
line=`expr $line + 1`
file=`sed -n -e ${line}p $TMPFILES`
  
done

#lines=`cat $TMP | wc -l`
#echo $lines > $outfile

lines=`wc $TMP`
set $lines
echo $1 > $outfile

cat $TMP >> $outfile
rm -f $TMP $TMPFILES

