#! /bin/sh
# grep for stuff in each message from mailboxes, sending
# matching messages to stdout.
# 

regexp=${1?}
maild=${2:-$HOME/Mail}

tmp=$HOME/tmp
if test ! -d $tmp; then
	mkdir $tmp || exit 1
fi

cat <<'EOF' | sed -e "s!@XregexpX@!$regexp!" -e "s!@XtmpX@!$tmp!g" > $tmp/grepmail.sh
cat > @XtmpX@/m-$FILENO
if test "`egrep -i '@XregexpX@' @XtmpX@/m-$FILENO`"; then
	printf "+" 1>&2
	cat @XtmpX@/m-$FILENO
else
	printf "-" 1>&2
fi
EOF

cat <<'EOF' | sed -e "s!@XtmpX@!$tmp!g" > $tmp/gm2.sh
cat $* | formail -s sh @XtmpX@/grepmail.sh
EOF
find $maild -type f -print0 | xargs -0 -n 20 sh $tmp/gm2.sh
echo 1>&2
