#!/bin/sh
#
# mailgateway news.group recipients
#
# bidirectional gatewaying utility.
#
# - forward a news article to recipients (typically a single alias in
#   /usr/lib/aliases or equivalent, to avoid long lists of recipients in 
#   messages)
# - filter out news control message.
# - messages fed to the newsgroup from the mailing list are not sent back.
#   we assume that "distribute" did the feed, and we look for the tell-tale
#   "Approved" line it tacks on to messages.
#
# Jean-Francois Lamy (lamy@ai.toronto.edu) 88-02-27

#magic="@mail.ai.toronto.edu"  # what distribute uses in Approved: line
address="funi.gagaga.ac.jp"
mail="/usr/lib/sendmail"
article=/tmp/art$$

case $# in
0) echo "$0: no argument (check /usr/lib/news/sys)" | $mail news
   exit 1 ;;
#1) echo "$0: no enough argument (check /usr/lib/news/sys)" | $mail news
#   exit 1 ;;
*) ;;
esac
cat >$article

check=`awk '
   /^Subject: cmsg/		{ print "nope" ; exit }
   /^Control:/			{ print "nope" ; exit }
   /^$|^[ \t][ \t]*$/		{ exit }
				{ next }
   ' $article`

newsgroups=`awk '
   /^Newsgroups: /		{ print $2 }
   /^$|^[ \t][ \t]*$/		{ exit }
   ' $article`


if [ ! "$check" ] ; then
#  if /bin/grep -s $magic $article ; then
#	   : came from distribute
#  else
          (
#	   echo "rcvdfrom USENET"  # zmailer feature, delete for sendmail.
	   echo "Resent-From: mailing-service@$address"
#	   echo "To: NetNews-System:;"
	   echo "To: $newsgroups"
	   echo "Resent-To: $1-readers:;"
	   echo "Reply-To: $newsgroups"
	   echo "Precedence: junk"
	   cat $article ) | $mail $1
#  fi
fi
/bin/rm -f $article
