#! /bin/sh
:
# Copyright (c) 1993-1994, S.R. van den Berg, The Netherlands
#$Id: unsubscribe,v 1.25 1994/06/28 14:49:16 berg Exp $

test=test		# /bin/test
echo=echo		# /bin/echo
cat=cat			# /bin/cat
sed=sed			# /bin/sed
expr=expr		# /bin/expr
formail=formail		# /usr/local/bin/formail
multigram=multigram	# ../SedBinDir/multigram

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

tmprequest=tmp.request
tmpfrom=tmp.from
unsubscribetxt=unsubscribe.txt
dist=dist

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

$formail -i"From: $listreq" -rtA"X-Loop: $listaddr" -I"Precedence: junk" \
 <$tmprequest

$test ! -z "$wrongaddress" && $echo "$wrongaddress" && wrongaddress=""

fromaddr=`$cat $tmpfrom`

remtext=`
 if $multigram -b1 -l$off_threshold -x$listreq -x$listaddr -d $dist 2>/dev/null
 then
   $echo ""
   $echo "You have been removed from the list."
 else
   $echo "You have not been removed, I couldn't find your name on the list."
   if test ! -z "$unsub_assist" -a 0 != "$unsub_assist"
   then
      $echo "What I did find were the following approximate matches:"
      $echo ""
      $multigram -b$unsub_assist -l-32767 -x$listreq -x$listaddr $dist \
       <$tmprequest
      $echo ""
      $echo "If you recognise one of these addresses as being the one you"
      $echo "wanted to unsubscribe, send in a new unsubscribe request"
      $echo "containing the text:  unsubscribe the_address_you_meant."
   fi
 fi`

$echo "$remtext" | $sed -e '1 s/^ *[0-9]* [^ ]* \(.*\)$/\1/' \
  -e '2 s/^Removed: \(.*\)$/\1/w '$tmpfrom

if $test ! -z "$subscribe_log"
then
  if $expr "X$remtext" : ".*You have been removed" >/dev/null
  then
     $echo "unsubscribe: `$echo \"$remtext\" | $sed -e '2,$d'` by:$fromaddr"
  else
     $echo "unsubscribe: attempt `$multigram -b1 -l0 -x$listreq -x$listaddr \
      $dist <$tmprequest` by:$fromaddr"
  fi >>$subscribe_log
fi

$echo "$fromaddr" >>$tmpfrom

$echo ""
$cat $unsubscribetxt

$sed -e 's/^/>/' $tmprequest
