#!/bin/sh

#
# Copyright 1993 Network Computing Devices, Inc.
#
# Permission to use, copy, modify, distribute, and sell this software and
# its documentation for any purpose is hereby granted without fee, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name Network Computing Devices, Inc. not be
# used in advertising or publicity pertaining to distribution of this 
# software without specific, written prior permission.
# 
# THIS SOFTWARE IS PROVIDED `AS-IS'.  NETWORK COMPUTING DEVICES, INC.,
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
# LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL NETWORK
# COMPUTING DEVICES, INC., BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA,
# OR PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
# WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# $NCDId: @(#)dj.prologue.sh,v 1.2.8.2 1994/08/04 21:14:30 mse Exp $
#
#   dj.prologue [-B] [-b] [-d mask] [-i #columns] [-l] [-n] [-p] [-w #columns]
#
#   A script to generate prologues for the HP DeskJet.
#

CHR=ncdchr
ECHO=ncdecho

# Make note of the name of this script.
IAM=$0

# Define standard error codes.
EXIT_OK=0
EXIT_USAGE=1

# Define paper orientations.
PORTRAIT=0
LANDSCAPE=1

# Define stroke weights.
EXTRA_BOLD=7
BOLD=3
NORMAL=0

# Define font characteristics that do not change.
style=0			# upright
spacing=0		# fixed

# Define default values for the optional arguments.
debug="0"
weight=normal
left=0
width=80
orientation=${PORTRAIT}
length=60
reset=false

# Process the arguments.
set XXX `getopt Bbd:i:lnprw: $*`
if [ $? != 0 ]
then
    echo $IAM: Usage: $IAM [-B] [-b] [-d mask] [-i #columns] [-l] [-n] [-p] [-r] [-w #columns] [page-length] >&2
    exit $EXIT_USAGE
fi
shift # shift off XXX

for i in $*
do
    case "$i" in
	-B)
	    weight=${EXTRA_BOLD}
	    shift
	    ;;
	-b)
	    weight=${BOLD}
	    shift
	    ;;
	-d)
	    debug="$2"
#	    if [ ${debug} -gt 0 ]
#	    then
#		set -x
#	    fi
	    shift; shift
	    ;;
	-i)
	    left=`expr X"$2" : 'X\([0-9][0-9]*\)$' \| 0`
	    shift; shift
	    ;;
	-l)
	    orientation=${LANDSCAPE}
	    shift
	    ;;
	-n)
	    weight=${NORMAL}
	    shift
	    ;;
	-p)
	    orientation=${PORTRAIT}
	    shift
	    ;;
	-r)
	    reset=true
	    shift
	    ;;
	-w)
	    width=`expr X"$2" : 'X\([0-9][0-9]*\)$' \| 0`
	    shift; shift
	    ;;
	--)
	    shift
	    break
	    ;;
    esac
done

if [ $# -ne 0 ]
then
    length="$1"
    shift
fi

# If there are any arguments left, this is wrong!
if [ $# -ne 0 ]
then
    echo $IAM: Usage: $IAM [-B] [-b] [-d mask] [-i #columns] [-l] [-n] [-p] [-r] [-w #columns] [page-length] >&2
    exit $EXIT_USAGE
fi

# Now determine pitch, height, and typeface from what we know.
if [ "${orientation}" = ${PORTRAIT} ]
then
    pitch=`expr ${width}0 / 75`
    height=`expr 792 / ${length}`
else
    pitch=`expr ${width}0 / 100`
    height=`expr 540 / ${length}`
fi
# Only certain pitches are supported.
if [ ${pitch} -le 10 ]
then
    pitch=10
    typeface=3
else
    if [ ${pitch} -le 12 ]
    then
	pitch=12
	typeface=3
    else
	pitch=16.66
	typeface=0
    fi
fi
width=`expr ${width} - ${left}`

if ${reset}
then
    ${CHR} ESC E			# reset
fi
${CHR} ESC \& k 2 G		# CR=>CR; LF=>CR+LF; FF=>CR+FF
${CHR} ESC 9			# clear margins
${CHR} ESC \& l ${orientation} O	# orientation
${CHR} ESC '(' 0 N			# Symbol Set = ECMA-94 Latin 1
${CHR} ESC '(' s			# Specify remaining font characteristics
    ${ECHO} ${spacing}  p		# Spacing
    ${ECHO} ${pitch}    h		# Pitch
    ${ECHO} ${height}   v		# Height (Point Size)
    ${ECHO} ${style}    s		# Style
    ${ECHO} ${weight}   b		# Stroke Weight
    ${ECHO} ${typeface} T		# Typeface
${CHR} ESC \& a			# set left margin
    ${ECHO} `expr 6 + ${left}`
    ${CHR} L
${CHR} ESC \& a			# set right margin
    ${ECHO} `expr 5 + ${width}`
    ${CHR} M
