#!/bin/sh
#	m2n [mbox]
# This unpacks the user's mail, then scans each message for evidence  that  it
# belongs  in  an  assortment of local newsgroups, and if so, posts them there
# and deletes them from the mail directory.  Unrecognized messages are left in
# the user's Mail directory, and their names are output.
# BUG: No file locking is done; incoming mail may be lost.
# BUG: We destroy the input file; if there was new mail, we destroy it, too.
NL=/u/news/lib

if [ $# -lt 1 ];then set /usr/mail/$LOGNAME;fi
if [ ! -s $1 ];then
	echo "No mail."
	exit 0
fi
if [ -d $HOME/Mail ];then M=$HOME/Mail;else M=$HOME/M;fi
F=`unpackmail $HOME/$M/ <$1`
if [ $? = 0 ];then
	>$1
fi
for f in $F 
do	echo $f ...
	T=`grep -s '^To:' $f`
	S=`grep -s '^Subject:' $f`
	if grep -s '^To:.*esperanto.*' $f
	then	
#		$NL/inews <$f -t "$S" -n mail.esperanto 
		$NL/recnews <$f mail.esperanto 
		rm $f
		continue
	elif grep -s '^Subject: Newsgroup .* created' $f
	then	
		$NL/inews <$f -t "$S" -n mail.news
		rm $f
		continue
	fi
	N=$N' '$f
done
echo Leftovers: $N
if [ -n "$N" ];then
	vi $N
fi
