#!/bin/bash
#
# This file belongs to MTYPE13 package by Wlodek Bzyl <matwb@univ.gda.pl>
#

script_name=$(basename $0)
version=1.3

# Default font
font=cmmi10
# Default number of columns
columns=6
# Default size
at=36bp
# Dvips options
dvips_opt=0
dvips_arg=
dvips_arg_default=""
# Default is no output
help_opt=0
version_opt=0

trap 'rm -f sfont2.log sfont2.dvi sfont2.ps ; exit' 1 2 15

# Note that we use `"$@"' to let each command-line parameter expand to a 
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o d:u:P:hv --long dvips-arg:,map-file:,printer-cfg:,help,version -a -n 'sfont2' -- "$@"`

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

if [ $? != 0 -o $# -eq 1 ] ; then 
    printf "Try \`$script_name --help' for more information\n" >&2
    printf "Terminating...\n" >&2
    exit 1
fi

while true ; do
  case "$1" in
    -d|--dvips-arg) dvips_opt=1 dvips_arg="$dvips_arg $2" ; shift 2 ;;
    -u|--map-file) dvips_opt=1 dvips_arg="$dvips_arg -u$2" ; shift 2 ;;
    -P|--printer-cfg) dvips_opt=1 dvips_arg="$dvips_arg -P$2" ; shift 2 ;;
    -h|--help) help_opt=1 ; shift ;;
    -v|--version) version_opt=1 ; shift ;;
    --) shift ; break ;;
     *) echo "Getopt internal error!" ; exit 1 ;;
  esac
done

if [ $version_opt != 0 ] ; then
  printf "%s (MTYPE13) %s\n" $script_name $version >&2
  printf "Written by Wlodek Bzyl.\n\n" >&2
  printf "Copyright (C) 2002 Wlodek Bzyl.\n" >&2
  printf "This is free software; see the source for copying conditions.  There is NO\n" >&2
  printf "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" >&2
  exit 1
fi

if [ $help_opt != 0 ] ; then
  printf "Usage:    %s [OPTION]... FILE\n" $script_name >&2
  printf "Examples: %s cmmi10\n" $script_name >&2
  printf "          %s -u +dragon.map dragon-000\n" $script_name >&2
  printf "Typeset font table for FILE.\n\n" >&2
  printf "  -d --dvips-arg=STR     STR passed to dvips\n" >&2
  printf "  -u --map-file=NAME     load PostScript map file\n" >&2
  printf "  -P --printer-cfg=NAME  read the configuration file \`config.NAME'\n" >&2 
  printf "  -h --help              display this help and exit\n" >&2
  printf "  -v --version           output version information and exit\n\n" >&2
  printf "Report bugs to <matwb@univ.gda.pl>.\n" >&2
  exit 1
fi

font=$1

printf "Typesetting font %s\n" $font >&2

tex --jobname=$font \\def\\myfont{$font} \\input sfont2.tex

if [ $? -eq 0 ] ; then 
    dvips $dvips_arg $font -o
fi
