#!/bin/sh -f
cd /usr/users/staff/aids
#
# How this crazy thing works...
#
# You should place in the aids .forward file, the following line:
#
#   \aids, "|/u/aids/filter dgreen@cs.ucla.edu"
#
# ...if 'dgreen@cs.ucla.edu' is the current chief moderator.
#
# Instructions are sent with each posting, so the moderator's life shouldn't 
# be too difficult.
#
chiefmoderator=$1
cat - > /tmp/aidsin.$$ 
if { tr "[a-z]" "[A-Z]" < /tmp/aidsin.$$ \
    | grep '^AIDS.MOD.COM ' > /dev/null ;}; then
  processcom $chiefmoderator < /tmp/aidsin.$$
elif egrep -i '^From[ :].*(MAILER-DAEMON)|(mmdf@)' /tmp/aidsin.$$ > /dev/null ; then

# Something weird came in

  echo "\nMAILER-DAEMON errors occured on a mailout.  Here is what came back."
  cat /tmp/aidsin.$$
  exit 0
else

# Has this message been previously received (oh please god, let there be a
# message-id!

  mid=`egrep '^Message-Id:' < /tmp/aidsin.$$`
  if [ "${mid:-nomessageid}" != "nomessageid" ] ; then 
    if fgrep "$mid" ARCHIVE.IN/message-ids > /dev/null ; then
      echo "$mid" rejected.  Previously received.
      exit 0
    else
      echo "$mid" >> ARCHIVE.IN/message-ids
      fi
    fi

# First compute the next available number.

  if ls ARCHIVE.IN/INDEX > /dev/null 2>&1 ; then
    in_last=`tail -1 ARCHIVE.IN/INDEX`
    set -- $in_last
    nextfreenum=$1
  else
    nextfreenum=1
  fi
  nextfreenum=`(echo $nextfreenum 1 + p | dc -)`
  while ls ARCHIVE.IN/$nextfreenum > /dev/null 2>&1 ; do 
    nextfreenum=`(echo $nextfreenum 1 + p | dc -)`
    done
  if fgrep -e '------ Message requiring your approval (' /tmp/aidsin.$$ > /dev/null; then
      /usr/users/staff/aids/filter $nextfreenum $chiefmoderator yes \
  	  < /tmp/aidsin.$$ > ARCHIVE.IN/$nextfreenum
    else
      /usr/users/staff/aids/filter $nextfreenum $chiefmoderator no \
  	  < /tmp/aidsin.$$ > ARCHIVE.IN/$nextfreenum
    fi
  eval set -- `awk '\
        /^From: / { $1 = "" ; printf( "%c%s%c ", 39, $0, 39 ) } \
	/^Subject: / {  $1 = "" ;
			printf( "\"%s\"\n", $0 ) }
	/.*/ { next } \
	' \
	ARCHIVE.IN/$nextfreenum`
  from=$1
  subject=$2

# Process submissions from special people (Dodell, in particular).

  if fgrep -x "$from" /usr/users/staff/aids/superusers > /dev/null ; then
    echo "aids.mod.com post $nextfreenum" | processcom $chiefmoderator
    echo "Article $nextfreenum autoposted from" $from
    exit 0
    fi
  echo "$nextfreenum	PENDING	$from	$subject" >> ARCHIVE.IN/INDEX
  header="\
-----------------  SCI.MED.AIDS ARCHIVE NUMBER $nextfreenum  -----------------"
  echo "From:$from\nSubject:  [SMA $nextfreenum]$subject\n\n\
Article submitted to sci.med.aids.  To accept, reject, or modify, mail\n\
commands to aids@cs.ucla.edu.  Reject articles containing expletives,\n\
unusual medical claims with no references, derogatory comments about\n\
other readers, or copyrighted material which has not been excerpted or \n\
summarized.  If minor modifications will render a submission acceptable,\n\
make them and post.  When rejecting, notify the author with reasons.\n\n\
Moderator commands (remove the preceding '>'):\n\n\
>  aids.mod.com post $nextfreenum          \n\
>  aids.mod.com reject $nextfreenum        \n\
>  aids.mod.com modify $nextfreenum        \n\
  (follow modify command with revised submission, you must include \n\
  '------' header and trailer lines).\n\n$header"\
\
  | (cat - ARCHIVE.IN/$nextfreenum; echo "$header") \
  | mail $chiefmoderator
fi
