#! /bin/sh
:
#$Id: arch_retrieve,v 1.0 1995/01/15 15:48:25 john Exp $

test=test		# /bin/test
echo=echo		# /bin/echo
sed=sed			# /bin/sed
cat=cat			# /bin/cat
egrep=egrep		# /bin/egrep
rel=rel			# /usr/local/bin/rel
date=date		# /bin/date
touch=touch		# /bin/touch
chmod=chmod		# /bin/chmod
sort=sort		# /bin/sort
find=find		# /bin/find
sed=sed			# /bin/sed
sleep=sleep		# /bin/sleep
formail=formail		# /usr/local/bin/formail

grep_sort()
{
    $find "$2" -type f -print | $sort | while read filename
    do
        $egrep -i -l -e "$1" $filename
    done
}

reverse_grep_sort()
{
    $find "$2" -type f -print | $sort -r | while read filename
    do
        $egrep -i -l -e "$1" $filename
    done
}

date_sort()
{
    $find "$2" -type f -print | $sort | $egrep "$2/[0-9][0-9][0-9][0-9]/$1"
}

reverse_date_sort()
{
    $find "$2" -type f -print | $sort -r | $egrep "$2/[0-9][0-9][0-9][0-9]/$1"
}

$test -z "$listaddr" &&
 $echo "Don't start this script directly, it is used in rc.request" && exit 64

tmprequest=tmp.request
tmpfrom=tmp.from

$chmod u+w tmp.lock		# so that we can touch it

archivetxt=archive.txt		# the helpfile
archivedir=archive

standalone=$1

from="arch_retrieve:  requested"
$echo "arch_retrieve: processing for `$cat $tmpfrom`  `$date`" >$tmpfrom

case "$X_ENVELOPE_TO" in
  *$list-request*) wrongaddress="" ;;
  *) wrongaddress="WARNING:
	Please try to use '$listreq'
	the next time when issuing archive server requests." ;;
esac

$formail -k -xSubject: |
 $sed -e '/^[^a-zA-Z	 ]/,$ d' -e 's/^[	 ]*archive/ARCHIVE/' \
  -e 's/[	 ]archive\/\/*/ /g' |
 (  oldwrongaddress="$wrongaddress"
    wrongaddress="WARNING:
	Please make sure to start the Subject: of requests to the archive-
	server with the word archive.${wrongaddress:+
$wrongaddress}"
   sendhelp=""
   ILLEGAL=""
   while read line
   do
      set `cd $archivedir; $echo dummy $line`
      shift
      case "$1" in
	 archive|ARCHIVE|archive:|ARCHIVE:) shift
	    $test ! -z "$wrongaddress" && wrongaddress="$oldwrongaddress";;
      esac
      #
      # Now call up the local extension file (if any)...
      #
      if test -f retrieve.local
      then
	 . retrieve.local
      elif test -f ../.bin/retrieve.local
      then
	 . ../.bin/retrieve.local
      elif test -f ../.bin/retrieve.local
      then
	 . ../.bin/retrieve.local
      fi
      #
      # The extension file should have used "set" to clear $1 and tell that
      # it has processed the current command
      #
      case "$1" in
	 search|rel|relevance|\
	 SEARCH|REL|RELEVANCE)
	    shift
	    $echo $from "$line" >>$tmpfrom
	    ( $formail -rt -I"Subject: archive retrieval: $line" \
	      -i"From: $listreq" -A"X-Loop: $listaddr" \
	      -i"Content-Type: multipart/digest; boundary=\"-----------------------------------------------------\""\
	      -I"Precedence: bulk" <$tmprequest
	      $test ! -z "$wrongaddress" && $echo "$wrongaddress"
	      $echo "-------------------------------------------------------"
	      $echo ""
	      $echo "Content-Type: text/plain"
	      $echo ""
	      $echo "Relevance Digest $*"
	      for file in `$rel "$*" ${archivedir}`
	      do
		  $echo ""
		  $echo "-------------------------------------------------------"
		  $echo ""
		  $sed "s/^-/- -/"  "./$file" | $formail -I "From "
	      done
	      $echo "---------------------------------------------------------"
	      $echo "End of Relevance Digest $*"
	      $echo "**********************************************************"
	    ) | $SENDMAIL $sendmailOPT -t ;;
	 grep|egrep|fgrep|\
	 GREP|EGREP|FGREP)
	    shift
	    $echo $from "$line" >>$tmpfrom
	    ( $formail -rt -I"Subject: archive retrieval: $line" \
	      -i"From: $listreq" -A"X-Loop: $listaddr" \
	      -i"Content-Type: multipart/digest; boundary=\"-----------------------------------------------------\""\
	      -I"Precedence: bulk" <$tmprequest
	      $test ! -z "$wrongaddress" && $echo "$wrongaddress"
	      $echo "-------------------------------------------------------"
	      $echo ""
	      $echo "Content-Type: text/plain"
	      $echo ""
	      $echo "Regular Expression Digest $* (Chronological Order)"
	      for file in `grep_sort "$*" ${archivedir}`
	      do
		  $echo ""
		  $echo "-------------------------------------------------------"
		  $echo ""
		  $sed "s/^-/- -/"  "./$file" | $formail -I "From "
	      done
	      $echo "---------------------------------------------------------"
	      $echo "End of Regular Expression Digest $* (Chronological Order)"
	      $echo "**********************************************************"
	    ) | $SENDMAIL $sendmailOPT -t ;;
	 rgrep|regrep|rfgrep|\
	 RGREP|REGREP|RFGREP)
	    shift
	    $echo $from "$line" >>$tmpfrom
	    ( $formail -rt -I"Subject: archive retrieval: $line" \
	      -i"From: $listreq" -A"X-Loop: $listaddr" \
	      -i"Content-Type: multipart/digest; boundary=\"-----------------------------------------------------\""\
	      -I"Precedence: bulk" <$tmprequest
	      $test ! -z "$wrongaddress" && $echo "$wrongaddress"
	      $echo "-------------------------------------------------------"
	      $echo ""
	      $echo "Content-Type: text/plain"
	      $echo ""
	      $echo "Regular Expression Digest $* (Reverse Chronological Order)"
	      for file in `reverse_grep_sort "$*" ${archivedir}`
	      do
		  $echo ""
		  $echo "-------------------------------------------------------"
		  $echo ""
		  $sed "s/^-/- -/"  "./$file" | $formail -I "From "
	      done
	      $echo "---------------------------------------------------------"
	      $echo "End of Regular Expression Digest $* (Reverse Chronological Order)"
	      $echo "**********************************************************"
	    ) | $SENDMAIL $sendmailOPT -t ;;
	 date|chron|chronological|\
	 DATE|CHRON|CHRONOLOGICAL)
	    shift
	    $echo $from "$line" >>$tmpfrom
	    ( $formail -rt -I"Subject: archive retrieval: $line" \
	      -i"From: $listreq" -A"X-Loop: $listaddr" \
	      -i"Content-Type: multipart/digest; boundary=\"-----------------------------------------------------\""\
	      -I"Precedence: bulk" <$tmprequest
	      $test ! -z "$wrongaddress" && $echo "$wrongaddress"
	      $echo "-------------------------------------------------------"
	      $echo ""
	      $echo "Content-Type: text/plain"
	      $echo ""
	      $echo "Chronological Digest $* (Chronological Order)"
	      for file in `date_sort "$*" ${archivedir}`
	      do
		  $echo ""
		  $echo "-------------------------------------------------------"
		  $echo ""
		  $sed "s/^-/- -/"  "./$file" | $formail -I "From "
	      done
	      $echo "---------------------------------------------------------"
	      $echo "End of Chronological Digest $* (Chronological Order)"
	      $echo "**********************************************************"
	    ) | $SENDMAIL $sendmailOPT -t ;;
	 rdate|rchron|rchronological|\
	 RDATE|RCHRON|RCHRONOLOGICAL)
	    shift
	    $echo $from "$line" >>$tmpfrom
	    ( $formail -rt -I"Subject: archive retrieval: $line" \
	      -i"From: $listreq" -A"X-Loop: $listaddr" \
	      -i"Content-Type: multipart/digest; boundary=\"-----------------------------------------------------\""\
	      -I"Precedence: bulk" <$tmprequest
	      $test ! -z "$wrongaddress" && $echo "$wrongaddress"
	      $echo "-------------------------------------------------------"
	      $echo ""
	      $echo "Content-Type: text/plain"
	      $echo ""
	      $echo "Chronological Digest $* (Reverse Chronological Order)"
	      for file in `reverse_date_sort "$*" ${archivedir}`
	      do
		  $echo ""
		  $echo "-------------------------------------------------------"
		  $echo ""
		  $sed "s/^-/- -/"  "./$file" | $formail -I "From "
	      done
	      $echo "---------------------------------------------------------"
	      $echo "End of Chronological Digest $* (Reverse Chronological Order)"
	      $echo "**********************************************************"
	    ) | $SENDMAIL $sendmailOPT -t ;;
	 version|VERSION)
	       $touch tmp.lock
		( $formail -rt -I"Subject: archive retrieval: $line" \
		   -i"From: $listreq" -A"X-Loop: $listaddr" \
		   <$tmprequest
		  $test ! -z "$wrongaddress" && $echo "$wrongaddress"
		  flist -v 2>&1 | sed -e "/^User:/,$ d"
		  echo ""
		  procmail -v 2>&1
		) | $SENDMAIL $sendmailOPT -t ;;
	 ""|\#*) ;;
	 *) $test -z "$sendhelp" && sendhelp="$1" ;;
      esac
   done
   if $test ! -z "$sendhelp" -o ! -z "$ILLEGAL"
   then
       ( $formail -rt -I"Subject: archive retrieval info" \
	  -i"From: $listreq" -A"X-Loop: $listaddr" -I"Precedence: bulk" \
	  <$tmprequest
	 $test ! -z "$wrongaddress" && $echo "$wrongaddress"
	 case "X$sendhelp" in
	    X[Hh][Ee][Ll][Pp]|X[Ii][Nn][Ff][Oo]|X)
	       $echo "$from $sendhelp." >>$tmpfrom ;;
	    *) $echo "$from unknown command $sendhelp." >>$tmpfrom
	       $echo "Unknown command $sendhelp." ;;
	 esac
	 $test ! -z "$ILLEGAL" && $echo "Illegal filename $ILLEGAL requested."
	 $echo ""
	 $cat $archivetxt
	 if $test ! -z "$standalone"
	 then
	    $echo ""
	    $test -f help.txt && $cat help.txt
	    $test -f info.txt && $cat info.txt
	 fi
       ) | $SENDMAIL $sendmailOPT -t
   fi
 )

if $test ! -z "$archive_log"
then
  $echo "arch_retrieve: done  `$date`" >>$tmpfrom
  $cat $tmpfrom >>$archive_log
fi

if $test ! -z "$sendmailQflush"
then
  $sendmailQflush 2>/dev/null &
fi
