#!/bin/rc
# Writes a PostScript program on standard output that builds a width
# table or typesetter description file. The program uses PostScript's
# print procedure, which means the table comes back on the printer's
# serial port. Try hardcopy if you don't have access to the port.
#

POSTLIB=/sys/lib/postscript/prologues
FONTDIR=/sys/lib/troff/font

PROLOGUE=$POSTLIB/trofftable.ps
DPOSTPROLOGUE=$POSTLIB/dpost.ps

COPYFILE=()
HOSTFONTDIR=()
DEVICE=()
LIBRARY=()
TEMPLATE=-R

SLOWDOWN=25
STARTCOMMENTS=256

NONCONFORMING='%!PS'
ENDPROLOG='%%EndProlog'
BEGINSETUP='%%BeginSetup'
ENDSETUP='%%EndSetup'
TRAILER='%%Trailer'

while (! ~ $#* 0 && ~ $1 -*) {
	switch ($1) {
	case -C;  shift; COPYFILE=$COPYFILE' '$1
	case -C*; COPYFILE=$COPYFILE' '`{echo $1 | sed s/-C//}

	case -F;  shift; FONTDIR=$1
	case -F*; FONTDIR=`{echo $1 | sed s/-F//}

	case -H;  shift; HOSTFONTDIR=$1
	case -H*; HOSTFONTDIR=`{echo $1 | sed s/-H//}

	case -L;  shift; PROLOGUE=$1
	case -L*; PROLOGUE=`{echo $1 | sed s/-L//}

	case -S;  shift; LIBRARY=$1
	case -S*; LIBRARY=`{echo $1 | sed s/-S//}

	case -T;  shift; DEVICE=$1
	case -T*; DEVICE=`{echo $1 | sed s/-T//}

	case -c;  shift; STARTCOMMENTS=$1
	case -c*; STARTCOMMENTS=`{echo $1 | sed s/-c//}

	case -o;  shift; OCTALESCAPES=$1		# only for Latin1 tables
	case -o*; OCTALESCAPES=`{echo $1 | sed s/-o//}

	case -s;  shift; SLOWDOWN=$1
	case -s*; SLOWDOWN=`{echo $1 | sed s/-s//}

	case -t;  shift; TEMPLATE=$1
	case -t*; TEMPLATE=`{echo $1 | sed s/-t//}

	case -*;  echo $0: illegal option $1 >[1=2]; exit 1
	}
	shift
}

if (! ~ $#DEVICE 0  && ! ~ $#LIBRARY 0) {
    echo $0: no device or shell library >[1=2]
    exit 1
}

if (! ~ $#* 1 2) {
    echo $0: bad argument count >[1=2]
    exit 1
}

if (test -d $HOSTFONTDIR -a -f $HOSTFONTDIR/$1) {
    COPYFILE=$COPYFILE' '$HOSTFONTDIR/$1
}

#
# Include the shell library and get the command used to build the table.
# Make awk call a separate library function??
#
if (~ $#LIBRARY 0 || ~ $LIBRARY '') LIBRARY=$FONTDIR/dev$DEVICE/shell.lib

. $LIBRARY

if (~ $#* 1) TEMPLATE=$1

CMD=`{BuiltinTables | awk '$2 == template"" {
	if ( pname == "" )
		pname = $3
	printf "%s %s %s", $1, tname, pname
	exit 0
}' template=$TEMPLATE tname=$1 pname=$2}

if (! $CMD) {
    echo $0: $TEMPLATE not found >[1=2]
    exit 1
}

#
# Build the PostScript font table program.
#

echo $NONCONFORMING
cat $PROLOGUE
echo /DpostPrologue 100 dict dup begin
cat $DPOSTPROLOGUE
echo end def
echo $ENDPROLOG

echo $BEGINSETUP
if (~ $COPYFILE '') COPYFILE=/dev/null
cat $COPYFILE
echo /slowdown $SLOWDOWN def
echo /startcomments $STARTCOMMENTS def
echo $ENDSETUP

$CMD

echo $TRAILER

