#!/bin/sh

# This shell script "mgc_font_convert" converts Mentor Graphics fonts from the
# standard portable Bitmap Distribution Format (BDF) to the non-portable
# displayable format required by the specified X server.  The command also
# builds a catalog for the X server that maps each logical font name to the
# name of the file in which it can be found.

echo "Version 2,  Copyright (c) Mentor Graphics Corp. 1992. All rights reserved." 1>&2

arg_cnt=0
help=0
osversion=999999
uflag=0
usage=0
utilitydir=""
verbose=:

if [ $# -eq 0 ]; then
   usage=1
else
   for arg in $*; do
      case $arg in
         -h) help=1 ;;
          *) ;;
        esac
   done
fi

if [ $help -eq 1 ] ; then
   cat << \End_of_Help

   NAME
      mgc_font_convert

   PURPOSE
      Converts collected MGC fonts to the font format of the specified X server.

   COMMAND USAGE
      mgc_font_convert [options] x_server source_directory destination_directory

   REQUIRED ARGUMENTS
      x_server
         Identifies the type of X server for which to produce converted fonts.

         Choose one from the following list:
            xnews       Open Windows X11R4 X server for SunOS 4.1.3 and Solaris
                           2.2 workstations (There is no 2.3 xnews server)
            mitsss      MIT X11R4 X server for Sun workstations under SunOS 4.1.3
            mitss5      MIT X11R4 X server for Solaris workstations under
                           SunOS 5.3+ (Same as Solaris 2.3+)
            hppa        X11R4 X server for HP Series 700 workstations
            700rx       X11R4 X server for HP 700/RX X terminals
            xdomain     X11R4 X server for HP/Apollo workstations
            tekxp       X11R4 X server for Tek Xpress Series 330
            x11r5       any X11R5 X server, including hppa HP-UX 9.0 or later,
                           IBM, DEC, NCD X terminals, SUN-mc68000, etc.

      source_directory
         Identifies the location from which the command reads the BDF fonts.

      destination_directory
         Identifies the location where the command places the converted fonts.

   OPTIONS
      -h
         help: Displays online help and exits the command.

      -u utilities_directory
         utilities: Overrides the default location of the font processing
                    utilities.  Please see the Description for defaults.

      -v
         verbose: Displays the name of each font file as it is converted.

   DESCRIPTION
      The mgc_font_convert command converts Mentor Graphics fonts from the
      standard portable Bitmap Distribution Format (BDF) to the non-portable
      displayable format required by the specified X server.  The command also
      builds a catalog for the X server that maps each logical font name to the
      name of the file in which it can be found.

      The environment variable MGC_HOME should be set prior to running this
      command, since it identifies the root of the Mentor Graphics tree.  If the
      command cannot find a valid Mentor Graphics tree, it displays an error
      message and exits.

      The x_server argument identifies the X server program for which this
      command produces displayable fonts.  The converted fonts can be used later
      with this X server, but not generally with other X servers.  However, all
      X11R5 X servers are able to use the same displayable fonts.  If the
      command does not recognize an argument value, it displays an error message
      and exits.

      Before you execute the command, the source directory must exist and must
      contain readable BDF font files named with the suffix ".bdf".  If either
      of these conditions is not met, the command displays an error message and
      exits.

      Before you execute the command, the destination directory must exist and
      must be empty, and you must have write permission to it.  If any of these
      conditions is not met, the command displays an error message and exits.

      This command uses two utility programs supplied by your X Windows vendor
      to prepare the fonts.  The converter reads the BDF font files as input and
      creates converted font files that are in the format the X server program
      needs in order to display the fonts.  The cataloguer scans all the
      converted font files in a directory for the logical font names that the
      X server expects when it receives a request to display text.  It extracts
      these logical names and puts each into a special catalog file (you could
      call it an index or table) along with the name of the file where the font
      itself is located.

      The names and default locations of the utility programs themselves varies
      depending upon which X server they relate to.  The command assumes that
      both utilities are located in the same directory.  The following table
      describes these relationships.

         X server        Converter       Cataloguer       Default location
            xnews      convertfont        bldfamily       $OPENWINHOME/bin
           mitsss         bdftosnf        mkfontdir           /usr/bin/X11
           mitss5         bdftosnf        mkfontdir       /usr/openwin/bin
             hppa         bdftosnf        mkfontdir           /usr/bin/X11
            700rx       xtbdftosnf      xtmkfontdir      /usr/bin/X11/700X
          xdomain         bdftosnf        mkfontdir           /usr/bin/X11
            tekxp         bdftosnf        mkfontdir           /usr/bin/X11
            x11r5         bdftopcf        mkfontdir           /usr/bin/X11

      The command's -u option overrides these default locations for the
      utilities programs.

      The simplest application of this command relates to just one machine.  You
      login to that machine, execute this command there, specify an X server
      that runs there, and locate the destination directory there.  Chances are
      good that the default utilities are located there as well.  For an X
      terminal, you login to its boot host and work there.

      However, there is no inherent requirement or restriction limiting you to
      this straightforward scenario.  Whatever machine you login to, you must
      use utilities that can execute on that machine, even if they are
      physically located on some other machine.  You can perform conversions for
      any type of X server whose utilities you have available, even if that
      server runs on another machine.  You can also place the converted font
      files at any valid location, even on a machine different from any that run
      the related X server.

      This command is dependent upon your network configuration and network file
      system.  It attempts to comply with all requests that it cannot
      distinguish as invalid.  You must guarantee that it uses the proper
      utilities for the task you need to accomplish, and you must place the
      converted font files where the targeted X server can find them.

   EXAMPLES
      The following example converts Mentor Graphics BDF font files for the
      Open Windows X server.  It assumes that the font utility programs are in
      the default location.

      mgc_font_convert xnews /usr/local/mgc_fonts_bdf /usr/local/mgc_fonts_xnews

      The following example converts Mentor Graphics BDF font files for the
      xdomain X server of an HP/Apollo workstation.  The xapollo X server on
      HP/Apollos does not work with Mentor Graphics applications.  The command
      also provides an alternate location for the font utilities and requests
      full command execution information on the screen.  The example uses
      command line continuation.

      mgc_font_convert -v -u /usr/X11/bin xdomain /usr/local/mgc_fonts_bdf \
                       /usr/local/mgc_fonts_xdomain

End_of_Help
   exit 0
fi

while [ $# -gt 0 -a $usage -ne 1 ]; do
   case $1 in
      -u) if [ $uflag = 1 ] ; then
             usage=1;
          else
             uflag=1;
          fi ;;
      -v) if [ $uflag = 1 ] ; then
             usage=1;
          else
             verbose=echo;
          fi ;;
      -*) usage=1 ;;
       *) if [ $uflag = 1 ] ; then
             utilitydir=$1;
             uflag=0;
          else
             case $arg_cnt in
                0) x_server=$1; arg_cnt=1 ;;
                1) srcdir=$1;   arg_cnt=2 ;;
                2) destdir=$1;  arg_cnt=3 ;;
                *) usage=1 ;;
             esac
          fi ;;
   esac
   shift
done

if [ $usage -eq 1 -o $arg_cnt -ne 3 ] ; then
   echo "Usage: mgc_font_convert [-h] [-v] [-u util_dir ] x_server src_dir dest_dir" 1>&2
   exit 1
fi


if [ ! -d $srcdir ]; then
   echo "Source directory \"$srcdir\" does not exist.  Please correct and try again." 1>&2
   exit 1
fi
if [ ! -r $srcdir ]; then
   echo "Source directory \"$srcdir\" is not readable.  Please correct and try again." 1>&2
   exit 1
fi

ls -d $srcdir/*.bdf > /tmp/mgc_srcdir_bdf_names.$$ 2>/dev/null
if [ ! -s /tmp/mgc_srcdir_bdf_names.$$ ]; then
   echo "Source directory \"$srcdir\" does not contain any .bdf files." 1>&2
   rm /tmp/mgc_srcdir_bdf_names.$$
   exit 1
fi
for i in `cat /tmp/mgc_srcdir_bdf_names.$$`; do
   if [ ! -r $i ]; then
      echo "Source file \"$srcdir/$i\" is not readable.  Please correct and try again." 1>&2
      rm /tmp/mgc_srcdir_bdf_names.$$
      exit 1
   fi
   if [ ! -s $i ]; then
      echo "Warning: Source file \"$srcdir/$i\" is empty.  Please correct and try again." 1>&2
      rm /tmp/mgc_srcdir_bdf_names.$$
      exit 1
   fi
done

if [ ! -d $destdir ]; then
   echo "Destination directory \"$destdir\" does not exist.  Please correct and try again." 1>&2
   exit 1
fi
if [ ! -w $destdir ]; then
   echo "Destination directory \"$destdir\" is not writable.  Please correct and try again." 1>&2
   exit 1
fi
ls $destdir > /tmp/destdir.$$
if [ -s /tmp/destdir.$$ ]; then
   echo "Destination directory \"$destdir\" is not empty.  Please use an empty directory." 1>&2
   rm /tmp/destdir.$$
   exit 1
fi
rm /tmp/destdir.$$

if [ $x_server = "xnews" ]; then
   font_catalog_file=Families.list
   if [ "$utilitydir" = "" ] ; then
      utilitydir=$OPENWINHOME/bin
   else
      convert_tool=$utilitydir/$convert_name
      catalog_tool=$utilitydir/$catalog_name
      if [ ! -x $convert_tool ]; then
         echo "###Warning: given utilities directory does not contain needed tools, trying default..." 1>&2
         utilitydir=$OPENWINHOME/bin
      fi
   fi
   convert_tool=$utilitydir/convertfont
   catalog_tool=$utilitydir/bldfamily

   if [ ! -x $convert_tool ]; then
      echo "Cannot find executable \"$convert_tool\" to convert fonts." 1>&2
      exit 1
   fi
   if [ ! -x $catalog_tool ]; then
      echo "Cannot find executable file \"$catalog_tool\" to build font catalog file." 1>&2
      exit 1
   fi
   convert()
   {
      base=`basename $1 .bdf`
      cp $1 /tmp/$base.bdf
      (cd /tmp; $convert_tool $base.bdf; rm $base.bdf)
      mv /tmp/$base*.fb $destdir/$base.fb
   }
#
# For xnews servers, bldfamily generates the warnings: 
# "sort: can't open .Synonyms.list: No such file or directory"
# "cat: ./Compat.list: No such file or directory"
# So I got rid of them by using stderr redirected to /dev/null
#
   catalog()
   {
      cd $destdir
      $catalog_tool 2>/dev/null
   }
elif [ $x_server = "700rx" ]; then
   font_catalog_file=fonts.dir
   convert_name=xtbdftosnf
   catalog_name=xtmkfontdir
   if [ "$utilitydir" = "" ] ; then
      utilitydir=/usr/bin/X11/700X
   else
      convert_tool=$utilitydir/$convert_name
      catalog_tool=$utilitydir/$catalog_name
      if [ ! -x $convert_tool ]; then
         echo "###Warning: given utilities directory does not contain needed tools, trying default..." 1>&2
         utilitydir=/usr/bin/X11/700X
      fi
   fi
   convert_tool=$utilitydir/$convert_name
   catalog_tool=$utilitydir/$catalog_name
   if [ ! -x $convert_tool ]; then
      echo "Cannot find executable \"$convert_tool\" to convert fonts." 1>&2
      exit 1
   fi
   if [ ! -x $catalog_tool ]; then
      echo "Cannot find executable file \"$catalog_tool\" to build font catalog file" 1>&2
      exit 1
   fi
   convert()
   {
      outputfile=$destdir/`basename $1 .bdf`.snf
      $convert_tool <$1 >$outputfile
   }
   catalog()
   {
      cd $destdir
      $catalog_tool
   }
elif [ $x_server = "x11r5" ]; then
   font_catalog_file=fonts.dir
   convert_name=bdftopcf
   catalog_name=mkfontdir
   if [ "$utilitydir" = "" ] ; then
      utilitydir=/usr/bin/X11
   else
      convert_tool=$utilitydir/$convert_name
      catalog_tool=$utilitydir/$catalog_name
      if [ ! -x $convert_tool ]; then
         echo "###Warning: given utilities directory does not contain needed tools, trying default..." 1>&2
         utilitydir=/usr/bin/X11
      fi
   fi
   convert_tool=$utilitydir/$convert_name
   catalog_tool=$utilitydir/$catalog_name
   if [ ! -x $convert_tool ]; then
      echo "Cannot find executable \"$convert_tool\" to convert fonts." 1>&2
      exit 1
   fi
   if [ ! -x $catalog_tool ]; then
      echo "Cannot find executable file \"$catalog_tool\" to build font catalog file" 1>&2
      exit 1
   fi
   convert()
   {
      outputfile=$destdir/`basename $1 .bdf`.pcf
      $convert_tool <$1 >$outputfile
   }
   catalog()
   {
      cd $destdir
      $catalog_tool
   }
else #All X11R4 X servers go thru this else
   font_catalog_file=fonts.dir
   catalog_name=mkfontdir
   if [ "$utilitydir" = "" ] ; then
      if [ "$x_server" = "mitss5" ]; then
	 # Solaris doesn't support $OPENWINHOME AND
	 # There's no /usr/bin/X11...
         utilitydir=/usr/openwin/bin
   	 convert_name=bdftopcf
   	 bdftrailer=".pcf"
      else
         utilitydir=/usr/bin/X11
   	 convert_name=bdftosnf
   	 bdftrailer=".snf"
      fi
   else
      convert_tool=$utilitydir/$convert_name
      catalog_tool=$utilitydir/$catalog_name
      if [ ! -x $convert_tool ]; then
         echo "###Warning: given utilities directory does not contain needed tools, trying default..." 1>&2
         utilitydir=/usr/bin/X11
      fi
   fi
   convert_tool=$utilitydir/$convert_name
   catalog_tool=$utilitydir/$catalog_name
   if [ ! -x $convert_tool ]; then
      echo "Cannot find executable \"$convert_tool\" to convert fonts." 1>&2
      exit 1
   fi
   if [ ! -x $catalog_tool ]; then
      echo "Cannot find executable file \"$catalog_tool\" to build font catalog file" 1>&2
      exit 1
   fi
   convert()
   {
      outputfile=$destdir/`basename $1 .bdf`$bdftrailer
      $convert_tool <$1 >$outputfile
   }
   catalog()
   {
      cd $destdir
      $catalog_tool
   }
fi

$verbose "   Converting all .bdf files found in directory \"$srcdir\" to a form suitable for"
$verbose "   use with the \"$x_server\" X server, and placing the resulting font files into"
$verbose "   the directory \"$destdir\"."             

# Convert the fonts found in srcdir, one by one.
# put the results directly into destdir, with the same leafnames
# except for the suffixes, which change.

$verbose ""
echo "Converting the fonts, please wait..." 1>&2
ls -d $srcdir/*.bdf |
while read inputfile; do
   $verbose "   Converting $inputfile"
   convert $inputfile
done

$verbose ""
echo "Building font catalog file \"$font_catalog_file\"." 1>&2
catalog

rm /tmp/mgc_srcdir_bdf_names.$$

$verbose "Done."
exit 0








