#! /bin/sh

############################################################################
#              Copyright 1992 Digital Equipment Corporation
#                         All Rights Reserved
#
# Permission to use, copy, and modify this software and its documentation is
# hereby granted only under the following terms and conditions.  Both the
# above copyright notice and this permission notice must appear in all copies
# of the software, derivative works or modified versions, and any portions
# thereof, and both notices must appear in supporting documentation.
#
# Users of this software agree to the terms and conditions set forth herein,
# and hereby grant back to Digital a non-exclusive, unrestricted, royalty-
# free right and license under any changes, enhancements or extensions 
# made to the core functions of the software, including but not limited to 
# those affording compatibility with other hardware or software 
# environments, but excluding applications which incorporate this software.
# Users further agree to use their best efforts to return to Digital any
# such changes, enhancements or extensions that they make and inform Digital
# of noteworthy uses of this software.  Correspondence should be provided
# to Digital at:
#
#                       Director of Licensing
#                       Western Research Laboratory
#                       Digital Equipment Corporation
#                       100 Hamilton Avenue
#                       Palo Alto, California  94301
#
# This software may be distributed (but not offered for sale or transferred
# for compensation) to third parties, provided such third parties agree to
# abide by the terms and conditions of this notice.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
# CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
############################################################################

# sccsid = "@(#)status	@(#)status	2.0	Network Systems Lab 4/15/92"
#
# Author: Geoff Mulligan (mulligan@pa.dec.com)
#	Please send any modifications or comments to me.
#
# Original code from Brian Reid
#
# This program is called from scanqueue to get the status of an order
#
# usage: status "destination/addr" "username/from" "orderId"

TmpFile=/tmp/status.$$
trap "rm -f $TmpFile" 0 1 2 3 15

Who="$2"
Addr="$1"
Order="$3"

cd ${ORDDIR}

if [ -r dstatus.$Order -a "$Order" ] ;then
	Status="filled"
elif [ -r in.$Order -a "$Order" ] ;then
	Status="pending"
	if [ "$Addr" != "`grep ADDR status.$Order | awk '{print $2}'`" ]
	then
		Status="bogus"
	fi
else
	Status="mia"
	Files=`grep -l "$Addr" status.* | sed s/status\.//`
	if [ $? -eq 0 ]; then
		Status="alt"
	fi
fi

case $Status in
    filled) cat <<a1 > $TmpFile
According to the logs, your order has been filled and mailed. The log
is attached. If you haven't received your order in what seems to be a 
reasonable amount of time, either resubmit your order or send a message to 
nsl-techreports-management (instead of nsl-techreports) and an actual human will
answer (though perhaps not instantly).

a1
            sed -e '1,/STATUS/d' < dstatus.$Order >> $TmpFile ;;

    pending) cat <<a2 > $TmpFile
Your order is in the queue to be filled. A log of what action has been taken
so far is attached.

a2
            sed -e '1,/STATUS/d' < status.$Order >> $TmpFile ;;

    bogus) cat <<a3 > $TmpFile
Order $Order is in the queue to be filled, but you aren't the
recorded originator.
a3
           ;;

    mia)
	if [ -z "$Order" ]; then
	  cat <<a4a > $TmpFile
You don't have any orders pending.
a4a
	else
	  cat <<a4 > $TmpFile
Order $Order can't be found. Either the order has been filled and removed 
(instead of just logged) or it was never placed properly. You didn't record
any of the orders in the queue.
a4
	fi
	;;

    alt)
	if [ -z "$Order" ]; then
	  cat <<a5a > $TmpFile
You have the following orders pending:

a5a
	else
	  cat <<a5 > $TmpFile
Order $Order doesn't exist. Either the order has been filled 
and removed, it was never placed properly, or you've misremembered 
the order ID. The following orders from you are still pending:

a5
	fi
	for i in $Files ; do
	  echo ${i}: >> $TmpFile
	  awk '
            BEGIN {InStatus = 0}
	      /^ARGS/ {
                printf "\tOrder"
		for (i = 2; i <= NF; i++)
		  printf " %s", $i;
		printf "\n"
	      }
	      /^STATUS/ {InStatus = 1; next}
	      InStatus == 1 {print "\t", $0}
	  ' < status.$i	>> $TmpFile
	done
#	echo $Files | tr '\040' '\n' >> $TmpFile
	;;
esac

cat - $TmpFile << funfilled | /usr/lib/sendmail -ba "$Addr"
Subject: status of your order ID $Order
From: NSL tech report service <nsl-techreports>
In-reply-to: message from $Who
To: $Addr

funfilled

echo "`$DATE`" status: $Order $Addr >> ${REQDIR}/out.log
