#!/bin/sh
#
# Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ===
#
# txt2html.sh
#
# Converts specially-formatted plain-text to html. 
# Uses "expand.sh", "txt2mu.sh" and "mu2html.sh".
# See also "index.sh".
#
# The "-t" (text-mode) switch can be used for
# non-technical input text.
#
# The "-x" (exclude) switch excludes html header
# and footer from the output.
#
# usage: txt2html.sh [-a <awk-binary>] [-v] [-x] [-t] [<input-file> [<stylesheet> [<graphics-dir> [<title>]]]]
#

awk=`( gawk 'END {print "gawk"}' ; nawk 'END {print "nawk"}' ; echo awk ) 2>/dev/null < /dev/null | head -1`
if test "${1}" = "-a"
then
	shift
	if test "${1}" != "" ; then awk="${1}" ; fi
	shift
fi

v=""
if test "${1}" = "-v"
then
	shift
	v="-v"
fi

x=""
if test "${1}" = "-x"
then
	x="-x"
	shift
fi

t=""
if test "${1}" = "-t"
then
	t="-t"
	shift
fi

file="${1}"
stylesheet="${2}"
graphics_dir="${3}"
title="${4}"

if test "${v}" != "" -a "${file}" != ""
then
	echo `basename $0`: processing ${file} >&2
fi

if test "${file}" != "" -a \! -f "${file}"
then
	echo `basename $0`: no such file: ${file} >&2
	exit 1
fi

if test "${stylesheet}" != "" -a \! -f "${stylesheet}"
then
	: # echo `basename $0`: warning: missing stylesheet: "${stylesheet}" >&2
fi

if test "${graphics_dir}" = ""
then
	graphics_dir="graphics"
fi

txt2mu="`dirname $0`/txt2mu.sh"
mu2html="`dirname $0`/mu2html.sh"
expand="`dirname $0`/expand.sh"
cat "${file}" | "${expand}" -a "${awk}" "${t}" | "${txt2mu}" -a "${awk}" "${t}" | "${mu2html}" -a "${awk}" ${x} "${title}" "${stylesheet}" | "${expand}" -a "${awk}"

