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

NOT_PROG=d
SEP=`echo | tr '\012' '\001'`
BEG_COMMENT=d
END_COMMENT=d
BEG_STR=
END_STR=

while true; do
	case "$1" in

	-c)	NOT_PROG="s$SEP^$SEP$2$SEP"
		shift
		;;

	-B)	BEG_COMMENT="s$SEP$SEP$2$SEP"
		END_COMMENT="s$SEP$SEP$3$SEP"
		BEG_STR="$2"
		END_STR="$3"
		test "$NOT_PROG" = d && NOT_PROG=b
		shift
		shift
		;;

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

	*)	break

	esac
	shift
done

ERRORS=

test -n "$BEG_STR" && echo "$BEG_STR"
sed -e "/^{{{$/,/^}}}$/! $NOT_PROG" \
    -e "/^}}}\$/$BEG_COMMENT" \
    -e "/^{{{\$/$END_COMMENT" $@ || ERRORS=yes
test -n "$END_STR" && echo "$END_STR"

test -z "$ERRORS" || usage 1

