#!/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/gather_stx_titles.usage 1>&2
	exit $1
}

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

	-f)	FROM_SUFFIX="$2"
		shift
		;;

	-f*)	FROM_SUFFIX=`echo $1 | cut -c3-`
		;;

	-t)	TO_SUFFIX="$2"
		shift
		;;

	-t*)	TO_SUFFIX=`echo $1 | cut -c3-`
		;;

	-p)	PREFIX="$2"
		shift
		;;

	-p*)	PREFIX=`echo $1 | cut -c3-`
		;;

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

	*)	break

	esac
	shift
done

for i in $@; do
	echo -n "define(\`@w_file_exists_"
	echo -n "$i" | sed -e "s#$FROM_SUFFIX\$#$TO_SUFFIX#" -e "s#^$PREFIX/*##" | tr -d '\012\015'
	echo "', t)dnl"
done

( cat << DEF
divert(-1)
define(\`w_quotewrap', \`\`\`\$1''')
define(\`w_makesubst',
\`patsubst(patsubst(\`\$1', \`$FROM_SUFFIX\$', \`$TO_SUFFIX'), \`^$PREFIX/*',)')
define(\`w_file', \`w_quotewrap(w_makesubst(w_real_file))')
define(\`w_title',
\`ifelse(\`\$1',,,\`divert(0)dnl')
\`define'(\`\`@w_title_of_''w_file, \`\`\$1'')\`dnl'
divert(-1)')
define(\`w_doc_id',
\`divert(0)dnl
\`define'(\`\`@w_filename_of_\$1'', w_file)\`dnl'
divert(-1)')
DEF
for i in $@; do
	echo "define(\`w_real_file', \`$i')"
	sed -f $BASE/common/header.sed $i
done
) | m4 || usage 1

