#! /bin/sh

# Copyright 1990 University of Toronto.  All rights reserved.
# Written by Henry Spencer.
# This software is not subject to any license of the American Telephone
# and Telegraph Company or of the Regents of the University of California.
#
# Permission is granted to anyone to use this software for any purpose on
# any computer system, and to alter it and redistribute it freely, subject
# to the following restrictions:
#
# 1. The author is not responsible for the consequences of use of this
#    software, no matter how awful, even if they arise from flaws in it.
#
# 2. The origin of this software must not be misrepresented, either by
#    explicit claim or by omission.  Since few users ever read sources,
#    credits must appear in the documentation.
#
# 3. Altered versions must be plainly marked as such, and must not be
#    misrepresented as being the original software.  Since few users
#    ever read sources, credits must appear in the documentation.
#
# 4. This notice may not be removed or altered.

PATH=/bin:/usr/bin ; export PATH
AWFLIB=${AWFLIB-/usr/lib/awf}
TMPDIR=${TMPDIR-/usr/tmp}

tmp=$TMPDIR/awp$$			# tempfile for building pass 2
errs=$TMPDIR/awe$$		# error messages (awk can't send to stderr)

case "$1" in
-ms)	mac=ms	;;
-man)	mac=man	;;
*)	echo "$0: must specify -ms or -man" >&2
	exit 2
	;;
esac
shift

dev="$AWFLIB/dev.$TERM"
if test ! -r $dev
then
	dev="$AWFLIB/dev.dumb"
fi

trap "rm -f $tmp $errs ; exit 0" 0 1 2

# build the full, macro-set-dependent, pass-2 awk program
(
	sed -n '1,/^#include/p' $AWFLIB/pass2.base
	cat $AWFLIB/pass2.$mac
	sed -n '/^#include/,$p' $AWFLIB/pass2.base
) >$tmp

# do it
(
	echo ".^x $errs"
	echo ".^b"
	echo ".^# 1 <prolog>"
	cat $dev $AWFLIB/common $AWFLIB/mac.$mac
	if test " $*" = " "
	then
		echo ".^# 1 <stdin>"
		cat
	else
		for f
		do
			echo ".^# 1 $f"
			cat $f
		done
	fi
	echo ".^e"
) | awk -f $AWFLIB/pass1 | awk -f $tmp | awk -f $AWFLIB/pass3

# error messages, if any
if test -s $errs
then
	cat $errs >&2
	exit 1
else
	exit 0
fi
