#! /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 = "@(#)enqueue	@(#)enqueue	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.
# It error-checks and enqueues an archive retrieval request.
#
# usage: enqueue < chunkfile

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

echo '#! /bin/sh' > $TmpFile

eval `awk '
  BEGIN {Dest="x"}
  $1 == "ADDR" {Addr=$2; for(i=3;i<=NF;i++) Addr = Addr " " $i; next}
  $1 == "ARGS" {Args=$2; for(i=3;i<=NF;i++) Args = Args " " $i; next}
  $1 == "FROM" {From=$2; for(i=3;i<=NF;i++) From = From " " $i; next}
  $1 == "VERB" {Verb=$2; next}
  $1 == "DATE" {Date=$2; for(i=3;i<=NF;i++) Date = Date " " $i; next}
  $1 == "DEST" {Dest=$2; next}
  END {
    printf "Addr='\''%s'\'' ",Addr;
    printf "Dest=\"%s\" ",Dest;
    printf "From='\''%s'\'' ",From;
    printf "Verb=\"%s\" ",Verb;
    printf "Date=\"%s\" ",Date;
    if (index(Addr,"!") > 0) 
      printf "ViaUUCP=%s",1;
    print "set ",Args
  }'`

cd $DIRECTORY/`setdir $Addr`

GFiles=""
Cumsize=0
BFiles=""
NumFiles=0
NSharFiles=0
TooBig=0
Dir="Abstracts"
OneFile=false

for j in $* ;do
  case $j in
    abstract)  Dir="Abstracts";	OneFile=false;;
    abstracts) Dir="Abstracts";	OneFile=false;;
    ps)        Dir="PS";	OneFile=false;;
    postscript) Dir="PS";	OneFile=false;;
    status)    Dir="Status";	OneFile=true;;
    *) case $j in
         index) j="Index";;
       esac;
# tech report numbers have / in them, but we don't want it in a subdirectory
       j=`echo $j | sed s-/-.-`
# this permits the user to ask for the file in any case, and we fold
# to match the truth:
       File=`ls -1 $Dir | grep -i \^${j}\$ | $ONELINE`
       if [ ! -z "$File" ] ;then
         Fsize=`wc -c $Dir/$File | colrm 9 | tr -d ' '`
         Fsize=`expr $Fsize + 125`;
         TrialCumSize=`expr $Cumsize + $Fsize`
         if [ "$ViaUUCP" -gt 0 -a "$TrialCumSize" -gt 100000 ] ;then
           TooBig=1;
           BFiles="$BFiles $Dir/$File"
         else
           case $j in
             *.shar) NSharFiles=`expr $NSharFiles + 1`
                     if [ $NSharFiles -le 1 ] ;then
                       Cumsize=$TrialCumSize
                       GFiles="$GFiles $Dir/$File"
                       NumFiles=`expr $NumFiles + 1`;
                     else
                       BFiles="$BFiles $Dir/$File"
                     fi;;
             *)  Cumsize=$TrialCumSize
                 GFiles="$GFiles $Dir/$File"
                 NumFiles=`expr $NumFiles + 1`;;
           esac
         fi
       else
	 BFiles="$BFiles $Dir/$j"
       fi;;
  esac
  shift
done

if  [ $NSharFiles -gt 1 ] ;then
    FStat="multishar $Cumsize";
else
  if [ "$BFiles" = "" ] ;then
    if [ "$GFiles" = "" ] ;then
	   FStat="null 0"
         else
	   FStat="allgood $Cumsize"
    fi
  else
    if [ "$GFiles" = "" ] ;then
      if [ "$TooBig" = 0 ] ;then
	FStat="allbad 0"
      else
	FStat="toobiguucp $Cumsize"
      fi
    else
      if [ "$TooBig" = 0 ] ;then
	FStat="somegood $Cumsize"
      else
        FStat="toobiguucp $Cumsize"
      fi
    fi
  fi
fi

if $OneFile ;then
  cat $GFiles > /tmp/ZO.$$
  GFiles=/tmp/ZO.$$
  NumFiles=1
fi

if ack "$Addr" "$From" "$Date" $FStat "$BFiles" "$GFiles" ;then
  Temp1="000000000$Cumsize"
  Unique=`date+ 0 hours "%S"`
  Filename=`expr $Temp1 : '.*\(.......\)$'`.$Unique
  cd $REQDIR
  cat << something > $Filename 
cat << nothing > /tmp/ZQ.\$\$
From: NSL tech report service <nsl-techreports>
Subject: per your request
In-reply-to: Request from $From dated $Date
To: $Addr

\`cat ${DIRECTORY}/copyright\`


nothing
cd $DIRECTORY/`setdir $Addr`
something
    if [ $NumFiles -eq 1 ] ;then
    echo 'cat /tmp/ZQ.$$ '$GFiles' | /usr/lib/sendmail -ba '\'$Addr\' >> $Filename
  else
    echo 'rshar '$GFiles' | cat /tmp/ZQ.$$ - | /usr/lib/sendmail -ba '\'$Addr\' >> $Filename
  fi
  cat << logentry >> $Filename
rm -f /tmp/ZQ.\$\$
echo "\`$DATE\`" sent $GFiles"($Cumsize)" to '$Addr'"(`setdir $Addr`)" >> ${REQDIR}/out.log
logentry
else
  rm -f /tmp/ZO.$$
fi
