#!/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 sfont.log sfont.dvi sfont.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 a:c:d:u:D:P:hv --long at:,dvips-arg:,map-file:,resolution:,printer-cfg:,column:,help,version -a -n 'sfont' -- "$@"`

# 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
    -a|--at) at=$2 ; shift 2 ;;
    -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 ;;
    -D|--resolution) dvips_opt=1 dvips_arg="$dvips_arg -D$2" ; shift 2 ;;
    -P|--printer-cfg) dvips_opt=1 dvips_arg="$dvips_arg -P$2" ; shift 2 ;;
    -c|--column) columns=$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 "  -c --column=NUM        typeset in NUM columns\n" >&2
  printf "  -a --at=SIZE           scale font FILE at SIZE\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 "  -D --resolution=NUM    set resolution to NUM (10--10000)\n" >&2
  printf "                         this affects the positioning of letters\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 at %s in %s columns\n" $font $at $columns >&2

tex \\def\\F{$font}\\def\\C{$columns}\\def\\AT{$at} \\input sfont.tex

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