#!/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: @(#)lj.prologue.sh,v 1.2.8.2 1994/08/04 21:14:18 mse Exp $
#
#   lj.prologue [-B] [-b] [-d mask] [-i #columns] [-l] [-n] [-p] [-w #columns] \
#       #lines
#
#   A script to generate prologues for the HP LaserJet.
#

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}
lines=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
    lines="$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
    HEIGHT=105
    WIDTH=80
else
    HEIGHT=80
    WIDTH=105
fi
pitch=`expr \( ${width}0 + ${WIDTH} - 1 \) / ${WIDTH}`
height=`expr \( ${HEIGHT} \* 72 \) / ${lines}0`
lpi=`expr \( ${lines}0 + ${HEIGHT} - 1 \) / ${HEIGHT}`

# Only certain pitches are supported.
case ${pitch}
in
    ?)
	pitch=10
	typeface=3
	;;
    10| 11| 12)
	pitch=12
	typeface=3
	;;
    *)
	pitch=16.66
	typeface=0
	;;
esac

width=`expr ${width} - ${left}`

# Only certain LPI values supported.
case ${lpi}
in
    1| 2| 3| 4| 6| 8| 12| 16| 24| 48)
	# lpi already set ok
	;;
    0)
	lpi=1
	;;
    5)
	lpi=6
	;;
    7)
	lpi=8
	;;
    9| 10| 11)
	lpi=12
	;;
    13| 14| 15)
	lpi=16
	;;
    1?| 20| 21| 22| 23)
	lpi=24
	;;
    *)
	lpi=48
	;;
esac

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 \& l ${lpi} D		# Vertical line spacing
${CHR} ESC \& a			# set left margin
    ${ECHO} `expr 6 + ${left}`
    ${CHR} L
${CHR} ESC \& a			# set right margin
    ${ECHO} `expr 5 + ${width}`
    ${CHR} M
