#!/bin/sh
# This file is copyright (c) 2004,2005,2006 by Panu Kalliokoski
# and released under the license in ../LICENSE

BASE=.
usage() {
	cat $BASE/messages/stx2any.usage 1>&2
	exit $1
}

DEVICE=html
TEMPLDEVICE=
SECURE=
QUOTE_AFTER=
NUMBERING=
TABLE_OF_CONTENTS=
MAKE_TITLE=on
LINKABBREVS=
EMDASH_SEP=" "
LATEX_PARAMS=
HTML_PARAMS=
PIC_SUFFIX=
PREPROC=""
POSTPROC=cat
M4OPTIONS="-I$BASE/site-packages"
SOURCES=

while test -n "$1"; do
	case "$1" in

	-T)	DEVICE="$2"
		shift
		;;

	-T*)	DEVICE=`echo $1 | cut -c3-`
		;;

	--quote)
		QUOTE_AFTER="-f $BASE/common/quote.sed"
		;;

	--quote-me-harder)
		PREPROC="$PREPROC -f $BASE/common/quote_us.sed"
		;;

	--more-secure)
		SECURE="$BASE/common/secure.m4"
		;;

	--numbering)
		NUMBERING="$2"
		shift
		;;

	--table-of-contents)
		TABLE_OF_CONTENTS="$2"
		shift
		;;

	--make-title)
		MAKE_TITLE="$2"
		shift
		;;

	--link-abbrevs)
		PREPROC="$PREPROC -f $BASE/common/linking.sed"
		M4OPTIONS="$M4OPTIONS -Dw_do_link_abbr=true"
		LINKABBREVS=on
		;;

	--no-template)
		TEMPLDEVICE=common
		;;

	--symmetric-crossrefs)
		M4OPTIONS="$M4OPTIONS -Dw_symmetric_crossrefs=t"
		;;

	--latex-params)
		LATEX_PARAMS="$2"
		shift
		;;

	--html-params)
		HTML_PARAMS="$2"
		shift
		;;

	--picture-suffix)
		PIC_SUFFIX="$2"
		shift
		;;

	--no-emdash-separate)
		EMDASH_SEP=
		;;

	--sed-preprocessor)
		PREPROC=" -f \"$2\" $PREPROC"
		shift
		;;

	--help|-\?) usage 0 ;;
	--version|-V)
		cat $BASE/messages/version.msg
		exit 0
		;;

	-)	SOURCES="$SOURCES -"
		;;

	-*)	M4OPTIONS="$M4OPTIONS \"$1\""
		;;

	*)	SOURCES="$SOURCES $1"

	esac
	shift
done

if test -n "$SECURE"; then
	if echo "$M4OPTIONS" | grep '[;|\\`&>]' >/dev/null; then
		echo "Unsecure characters in the options: $M4OPTIONS"
		exit 1
	fi
	if echo "$PREPROC" | grep '[;|\\`&>]' >/dev/null; then
		echo "Unsecure characters in the preprocessor script: $M4OPTIONS"
		exit 1
	fi
fi

case "$DEVICE" in
ps)
	POSTPROC='groff -Tps -t -man'
	DEVICE=man
	;;
text)
	POSTPROC='w3m -dump -T text/html -cols 80'
	DEVICE=html
	;;
xhtml)
	POSTPROC='tidy -q -asxml'
	DEVICE=html
	;;
esac

if test ! -d "$BASE/$DEVICE"; then
	echo "unknown output format: $DEVICE" 1>&2
	exit 1
fi

. $BASE/$DEVICE/settings.sh

test -z "$TEMPLDEVICE" && TEMPLDEVICE="$DEVICE"
test -z "$TABLE_OF_CONTENTS" && TABLE_OF_CONTENTS="$NUMBERING"

set_onoff_option() {
case "$1" in
y*|on) M4OPTIONS="$M4OPTIONS -D$2=true" ;;
n*|off) ;;
*)
	echo "Unknown $3 setting: $1" 1>&2
	exit 1
esac
}

set_onoff_option "$NUMBERING" w_do_numbering numbering
set_onoff_option "$TABLE_OF_CONTENTS" w_make_toc table-of-contents
set_onoff_option "$MAKE_TITLE" w_make_title make-title

test -z "$SOURCES" && SOURCES=-

trap 'echo "Subprocess m4 exited with an error" 1>&2 && echo "Try \"$0 --help\" for help on command line options" 1>&2' 13
PARENT=$$; export PARENT

for i in $SOURCES; do
	echo -n "define(\`w_real_file', \`$i')"
	echo "define(\`w_line_base', not yet in file)dnl"
	test -n "$LINKABBREVS" -a "X$i" != "X-" && \
		sed -f $BASE/common/gatherlinkmap.sed "$i"
	echo "define(\`w_line_base', __line__)dnl"
	cat "$i"
done | \
sed -f $BASE/common/quote_quotes.sed | \
eval sed -f $BASE/common/header.sed -f $BASE/common/wrapcalls.sed $PREPROC | \
sed -f $BASE/common/emphasis.sed | \
sed -f $BASE/common/inline.sed -f $BASE/common/block.sed $QUOTE_AFTER | \
( eval m4 "\"-Dw_outputfmt=$DEVICE\"" "\"-Dw_picture_suffix=$PIC_SUFFIX\"" \
   "\"-Dw_emdash_separate=$EMDASH_SEP\"" "\"-D@w_bodytag_params=$HTML_PARAMS\"" \
   "$M4OPTIONS" $SECURE \
	$BASE/common/common.m4 \
	$BASE/common/markup.m4 \
	$BASE/$DEVICE/make.m4 \
	$BASE/common/begin.m4 - $BASE/common/end.m4 \
	$BASE/$TEMPLDEVICE/templ.m4 \
	$BASE/common/cleanup.m4 || kill -13 $PARENT ) | \
$POSTPROC

