:
# text2html -- convert a text file to html
#	-i = skip hidden and comment lines
#	-c = allow hidden lines, skip comment lines
#	-a = allow hidden and comment lines
#	-d = show diffs
#	-u = check for used images

do_diff=yes
text2html_opt=
do_recursive=no
do_used=no

root="/$HNODE/u/william"

while [ ! -z "$1" ]
do
	name="$1"
	case "$name" in
	-x)	set -x ;;
	-d)	do_diff=yes ;;
	+d)	do_diff=no ;;
	-a|-i|-c) text2html_opt="$text2html_opt $name" ;;
	-u)	do_used=yes ;;
	*)	break ;;
	esac
	shift
done

if [ -z "$*" ]
then
	name=`pwd`
	name=`basename "$name"`
	if [ -f "${name}.html" -o -f "${name}.1" ]
	then
		set -- "$name"
	fi
fi

case "`pwd`" in
*hidden*) text2html_opt="$text2html_opt -i" ;;
*/u/william/wb/*) text2html_opt="-c $text2html_opt" ;;
*/william/camera/*) text2html_opt="-a $text2html_opt" ;;
esac

for name in "$@"
do
	case "$name" in
	-x)	set -x ; continue ;;
	-d)	do_diff=yes ; continue ;;
	+d)	do_diff=no ; continue ;;
	-a|-i|-c) text2html_opt="$text2html_opt $name" ; continue ;;
	-r)	do_recursive=yes ; continue ;;
	-u)	do_used=yes ; continue ;;
	-*)	echo "$0: Unknown option $name" ; continue ;;
	esac

	if [ -d "$name" ]
	then
		if [ "$do_recursive" = yes ]
		then
			echo "Entering $name"
			( cd "$name" ; text2html.sh *.html )
		else
			echo "$0: Skipping directory $name"
		fi
		continue
	fi

	if [ "$do_recursive" = yes ]
	then
		continue
	fi

	if [ -r "$name" -a ! -f "$name" ]
	then
		echo "$0: $name is not a normal file"
		continue
	fi

	base=`basename $name`

	name_ok=no

	case "$name" in
	*.)	base=`basename $base .` ;;
	*.1)	base=`basename $base .1` ; name_ok=yes ;;
	*.doc)	base=`basename $base .doc` ; name_ok=yes ;;
	*.txt)	base=`basename $base .txt` ; name_ok=yes ;;
	*.html)	base=`basename $base .html` ;;
	*.*)	echo "$0: $name has invalid extension" ; continue ;;
	esac

	infile=

	case "$name" in
	/*)	first_dir="" ;;
	*)	first_dir="." ;;
	esac

	for dir in "$first_dir" $root/txt \
		/$HNODE/u1/william/chuck/restore7
	do
		if [ "$name_ok" = yes -a -f "$dir/$name" ]
		then
			infile="$dir/$name"
			break
		fi
		for ext in ".1" ".doc" ".txt"
		do
			if [ -f "$dir/$base$ext" ]
			then
				infile="$dir/$base$ext"
				break
			fi
		done
		if [ ! -z "$infile" ]
		then
			break
		fi
	done

	if [ -z "$infile" ]
	then
		echo "$0: $name not found"
		continue
	fi

	outfile="${base}.html"

	echo "Converting $infile to $outfile with text2html $text2html_opt"

	if [ -f "$outfile" ]
	then
		mv "$outfile" "${outfile}-"
		ls -l "${outfile}-"
	fi

	if [ "$do_used" = yes ]
	then
		rm -f junk.tmp junk1.tmp
		text2html $text2html_opt < "$infile" > "$outfile" 2>junk1.tmp
		grep Warning junk1.tmp | grep image | grep "not used" | awk '{ print $3 }' > junk.tmp
		rm -f junk1.tmp
		ls -l junk.tmp
	else
		text2html $text2html_opt < "$infile" > "$outfile"
	fi

	ls -l "$infile" "$outfile"

	if [ "$do_diff" = yes ]
	then
		if [ -f "${outfile}-" ]
		then
			diff "$outfile" "${outfile}-"
			diff "$outfile" "${outfile}-" | spell
		else
			echo "No previous version of $outfile"
		fi
	fi
done

# Check for work files

for base in junk test x xx y z
do
	for ext in 1 jpg gif tmp
	do
		name="$base.$ext"
		if [ -f "$name" ]
		then
			echo "$0: Warning: found $name"
			ls -l "$name"
		fi
	done
done
