#!/bin/sh
#
# da' o devido encaminhamento a cada mensagem que chega
#


# Adapted on April 17, 1997: T.K.

msg=/tmp/msg.$$

cat >$msg

home=/proj/dicio/ortho
ortho=$home/remote
log=$ortho/log/ortho-log
#lib=$home/PUB/notPUB
lib=$ortho/lib
redlist=$lib/redlist
greenlist=$lib/greenlist

PATH=$ortho/bin:$PATH

export PATH

# separa cabeca e corpo

cabeca=/tmp/cabeca.$$
corpo=/tmp/corpo.$$
minhead=/tmp/minhead.$$

cat $msg |
sed -e '
/^$/ q' 2>&1 >$cabeca

cat $cabeca |
sed -n -e '
/^From: / p
/^Date: / p
/^To: / p
/^Subject: / p' >$minhead

cat $msg |
sed -n -e '
/^$/,$ p' >$corpo

# obtem from, subject, to, sender

## From

from=/tmp/from.$$

cat $minhead |
sed -n -e '
/^From: / p' >$from

################
#
# 	checkpoint
#	
#	verifica se remetente tem
#	autorizacao para usar servico
#

if 
  cat $from | egrep -is -f $redlist
then
  # esta na lista vermelha... ignora mensagem
  rm /tmp/*.$$
  exit 1
fi

if
  cat $from | egrep -vs -f $greenlist
then
  # nao est'a na lista verde...  avisa remetente...
  reject-ortho $minhead
  rm /tmp/*.$$
  exit 2
fi

# continua fluxo normal...

## Subject
 
subject=/tmp/subject.$$
  
cat $minhead |
sed -n -e '
/^Subject: / p' >$subject
 
## To, Cc
 
to=/tmp/to.$$
 
cat $minhead |
sed -n -e '
/^To: / p' >$to

# prepara um pequeno log

( cat $minhead ) >>$log

# agora comeca o processamento propriamente dito

if
	/usr/bin/fgrep -si "tex" $subject
then
	echo "--- subject	tex" >>$log
	cat $msg |
	any-ortho tex $minhead $corpo
	rm -f /tmp/*.$$
	exit
fi
if
	/usr/bin/fgrep -si "tt" $subject
then
	echo "--- subject	tt" >>$log
	cat $msg |
	any-ortho tt $minhead $corpo
	rm -f /tmp/*.$$
	exit
fi
if
	/usr/bin/fgrep -si "iso" $subject
then
	echo "--- subject	iso" >>$log
	cat $msg |
	any-ortho "" $minhead $corpo
	rm -f /tmp/*.$$
	exit
fi
if
	/usr/bin/fgrep -si "info" $subject
then
	echo "--- subject	info" >>$log
	cat $msg |
	info-ortho $minhead $corpo $lib/remote.info
	rm -f /tmp/*.$$
	exit
fi
	echo "--- subject	default" >>$log
	cat $msg |
	info-ortho $minhead $corpo $lib/remote.info
	rm -f /tmp/*.$$
