#! /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 = "@(#)order	@(#)order	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 place an order for paper reports
#
# usage: order "destination/addr" "username/from" "date" "Badge" "input file" "args

Addr="$1"; shift
From="$1"; shift
Date="$1"; shift
Badge="$1"; shift
FileName="$1"; shift

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

#if [ ! -z "$Badge" ]; then
#    eval `echo $Addr | awk '
#	/~[@!%]/    {next}
#	/@.*@/ {next}
#	/!~[!%@]/ {printf "HELPFILE=${DIRECTORY}/Status.DEC\n"}
#	/@.*\.dec\.com/ {printf "HELPFILE=${DIRECTORY}/Status.DEC\n"}
#	/::/	    {printf "StatusDir=${DIRECTORY}/Status.DEC\n"}' `
#fi

cd $DIRECTORY/`setdir $Addr`

Unique=`date+ 0 hours %S"`.$$
WorkName=${ORDDIR}/in.$Unique
FileName=${ORDDIR}/status.$Unique

mv ${ORDDIR}/$FileName $WorkName

cd Status
None=true
for i in $* ;do
  j=`echo $i | tr / .`
  File=`ls -1 | grep -i \^${j}\$`
  if [ $? -eq 0 ] ;then
    None=false
    File=`ls -1 | grep -i \^${j}\$ | $ONELINE`
    cat $File >> $TmpFile
    Args="$Args $i"
  else
    echo Report "$i" does not exist >> $TmpFile
  fi
done

if [ -z "$*" ] ;then
  /usr/lib/sendmail -ba "$Addr" << ---%%%end_message%%%---
Subject: Your technical report order
From: NSL tech report service <nsl-techreports>
In-reply-to: Request from $From dated $Date
To: $Addr

We've received your order for technical reports.  But you didn't
specify any reports, notes or videos.  If you need help send another
request with help as the subject.
---%%%end_message%%%---

  rm -f $FileName $WorkName
  echo "`$DATE`" order: $Addr "(no files)" >> ${REQDIR}/out.log
  exit
fi

echo ADDR $Addr > $FileName
echo ARGS $Args >> $FileName
echo STATUS >> $FileName
echo `date` received >> $FileName

/usr/lib/sendmail -ba "$Addr" << ---%%%end_message%%%---
Subject: ack receipt of your technical report order
From: NSL tech report service <nsl-techreports>
In-reply-to: Request from $From dated $Date
To: $Addr

We've received your order for technical reports.  These orders are filled by
hand as quickly as possible; sometimes there are unavoidable delays because,
for example, a report is out of print.

The status of your order is:

`cat $TmpFile`

You can check on the status of your order using the "status" command of the
tech report server. The id of this order is `echo $Unique`

You can check on the status of individual reports using the "send status"
command of the server. If you need an explanation of either of these commands,
send a message with the single word "help" to the server for complete details.
---%%%end_message%%%---

if [ -z "$Args" ] ;then
  rm -f $FileName $WorkName
fi

if $None ;then
  rm -f $FileName $WorkName
fi

echo -n "`$DATE`" order: $Addr"(`setdir $Addr`)" >> ${REQDIR}/out.log

if $None ;then
  echo " (Invalid items)" >> ${REQDIR}/out.log
else
 echo "" >> ${REQDIR}/out.log
fi



