#!/bin/sh
#set -vx

PROG=`basename $0`
INPUT=$1
TMP1="/tmp/${PROG}.$$.1"
TMP2="/tmp/${PROG}.$$.2"

if [ "$#" -ne "1" ]; then
        printf "Usage: $PROG <full-path-to-html-manual>\n"
        printf "Swaps tabs for '&nbsp;' where needed to make the HTML manual's code snippets look correct.\n\n"
        printf "NOTE: This script overwrites the original file.\n\n"
        printf "Example: $PROG ../docs/AdministratorsManual.html\n\n"
        printf "Report bugs to <sella@digital-genesis.com>.\n"
        exit 1
fi

if [ ! -r "${INPUT}" ]; then
	printf "Either ${INPUT} isn't a file or you don't have permissions to it.\n\n"
	exit 1
fi

cp ${INPUT} ${TMP2}

X=12
while [ $X -gt 0 ]; do
	Y=$X
	TAB="\\t"
	SPACE="\\&nbsp;\\&nbsp;\\&nbsp;\\&nbsp;"
	while [ $Y -gt 1 ]; do
		TAB="${TAB}\\t"
		SPACE="${SPACE}\\&nbsp;\\&nbsp;\\&nbsp;\\&nbsp;"
		let Y--
	done

	COMMAND1=`printf 's/\\(code-western[^\\\\t]*>[#\\\\/-]*\\)%s/\\\\1%s/' "${TAB}" "${SPACE}"`
	COMMAND2=`printf 's/\\(<B>[#\\\\/-]*\\)%s/%s\\\\1/' "${SPACE}" "${SPACE}"`

	sed "${COMMAND1}" ${TMP2} | sed "${COMMAND2}" | sed 's/[‘’]/\&#146;/g' | sed 's/[“”]/\&quot;/g' | sed 's/[–]/\&#150;/g' > ${TMP1}

	mv ${TMP1} ${TMP2}

	let X--
done

mv ${TMP2} ${INPUT}
