#!/bin/sh

prog=`basename $0`

[ $# -eq 0 ] &&
{
	echo "usage: ${prog} [-<tx>] command"
	echo "       execute \`command' on the sourcefiles"
	echo
	echo "flag -t means: tcemp files only"
	echo "     -x means: xemp  files only"
	echo
	echo "or     ${prog} [-<tx>] -<gw> word"
	echo "       grep \`word' from the sourcefiles and edit those files."
	echo
	echo "flag -w means: words only (surrounded by \\< and \\>)"
	echo
	echo "or     ${prog} -s"
	echo "       set the envirenment variables: CALL, X_CALL & TC_CALL"
	exit
}

wd=`pwd`
[ ! `basename ${wd}` = xemp ] && cd ${XEMPDIR-${HOME}/xemp}

case x$1 in

x-t)
	shift
	com=tcdo
	;;
	
x-x)
	shift
	com=xdo
	;;
	
x*)
	com=do
	;;

esac

[ x$1 = x-s ] && {
	COMMAND="echo -n \${FILES} >> .setenv"
	export COMMAND
	echo -n "CALL='" > .setenv
	make do
	echo "'" >> .setenv
	echo -n "X_CALL='" >> .setenv
	make xdo
	echo "'" >> .setenv
	echo -n "TC_CALL='" >> .setenv
	make tcdo
	echo "'" >> .setenv
	echo "export CALL X_CALL TC_CALL" >> .setenv
	echo "Now type: . .setenv"
	exit
}


COMMAND="$* \${FILES}"

case x$1 in

x-w)
	shift
	COMMAND="vi '+/\\<$1\\>' \`grep -w -l '$1' \${FILES}\`"
	;;

x-g)
	shift
	COMMAND="vi '+/$1' \`grep -l '$1' \${FILES}\`"
	;;
	
*)
	;;

esac

export COMMAND

exec make ${com}
