#!@_PATH_SH@
##  $Revision: 1.13.2.1 $
##  SH script to send UUCP batches out.  By Mike Cooper and Rich $alz.
##  Based on B News sendbatch 1.22 10/29/89.
. @LIBDIR@/innshellvars

##  Place to run a df, and amount of space needed.
UUSPOOL=/var/spool/uucp
SPOOLFREE=20000
##  Program names and flags.  Most can be set on the command-line.
C7=
COMP=
COMPFLAGS=
# Use the COMPRESS variable as it is in innshellvars
# COMPRESS=/usr/ucb/compress
ECHO=
##  Not a config param since this is the remote rnews.
RNEWS=rnews
UUX=uux
UUXFLAGS="- -r -n -gd"
##  Desired size of each batch, total number of bytes we want to queue.
DEFBYTES=50000
BYTESQUEUED=1000000000

PROG=`basename $0`

##  Go to where the action is.
cd ${BATCH}


##  Loop over command line.
for SITE
do
    ##  Is this a flag?
    case "${SITE}" in
    -s*)
	DEFBYTES=`expr "${SITE}" : '-s\(.*\)'`
	continue
	;;
    -m*)
	BYTESQUEUED=`expr "${SITE}" : '-m\(.*\)'`
	continue
	;;
    +m)
	BYTESQUEUED=''
	continue
	;;
    -p*)
	BYTESPERRUN=`expr "${SITE}" : '-p\(.*\)'`
	continue
	;;
    +p)
	BYTESPERRUN=''
	continue
	;;
    -r*)
	RNEWS=`expr "${SITE}" : '-r\(.*\)'`
	continue
	;;
    -U*)
	UUX=`expr "${SITE}" : '-U\(.*\)'`
	continue
	;;
    -u*)
	UUXFLAGS=`expr "${SITE}" : '-u\(.*\)'`
	continue
	;;
    -c7)
	COMP="; exec ${COMPRESS} ${COMPFLAGS} | $LIB/encode"
	ECHO="echo '#! c7unbatch'"
	continue
	;;
    -c)
	COMP="; exec ${COMPRESS} ${COMPFLAGS}"
	ECHO="echo '#! cunbatch'"
	continue
	;;
    +c)
	COMP=''
	C7=''
	ECHO=''
	COMPFLAGS=''
	continue
	;;
    -[bBC]*)
	COMPFLAGS="${COMPFLAGS} ${SITE}"
	continue
	;;
    -o*)
	ECHO=`expr "${SITE}" : '-o\(.*\)'`
	RNEWS='cunbatch'
	continue
	;;
    +o)
	ECHO=''
	RNEWS=rnews
	continue
	;;
    -D*)
	UUSPOOL=`expr "${SITE}" : '-D\(.*\)'`
	continue
	;;
    +D)
	UUSPOOL=''
	continue
	;;
    -f*)
	SPOOLFREE=`expr "${SITE}" : '-f\(.*\)'`
	continue
	;;
    esac
    test -z "${BYTESPERRUN}" && BYTESPERRUN=${BYTESQUEUED}

    ##  We have a site; lock it.
    LOCK=${LOCKS}/LOCK.${SITE}
    trap 'rm -f ${LOCK} ; exit 1' 1 2 3 15
    shlock -p $$ -f ${LOCK} || {
	echo "${PROG}:  ${SITE} Locked by `cat ${LOCK}`."
	continue
    }

    ##  Flush the batchfile.
    BATCHFILE=${BATCH}/${SITE}.uucp
    if [ -f ${SITE}.work ] ; then
	cat ${SITE}.work >>${BATCHFILE}
	rm -f ${SITE}.work
    fi
    mv ${SITE} ${SITE}.work
    ctlinnd -s -t30 flush ${SITE} || {
	echo "${PROG}:  Can't flush ${SITE}."
	rm -f ${LOCK}
	continue
    }
    cat ${SITE}.work >>${BATCHFILE}
    rm -f ${SITE}.work
    if [ ! -s ${BATCHFILE} ] ; then
	echo "${PROG}:  No articles for ${SITE}."
	rm -f ${LOCK}
	continue
    fi

    ##  Check free space on the partition?
#    if [ -n "${UUSPOOL}" ] ; then
#	FREE=`(cd ${UUSPOOL}; df . | ${AWK} '
#		$6 == "'${UUSPOOL}'" { print $4 ; exit }
#		$1 == "'${UUSPOOL}'" && NF == 7 { print $4 ; exit }
#		$2 == "'${UUSPOOL}'" { print $5 ; exit }
#		$1 == "'${UUSPOOL}'" { print $3 ; exit }
#	    ')`
## = ()<	POS=@<INNWATCH_BLOCKS>@>()=
	POS=4
	DOLLAR="$"
	FREE=`${INNDF} ${UUSPOOL}`
	if [ -n "${FREE}" ]; then
	  if [ "${FREE}" -lt ${SPOOLFREE} ] ; then
	    echo "${PROG}:  No space on ${UUSPOOL} for ${SITE} (${FREE})."
	    rm -f ${LOCK}
	    continue
	  fi
	fi
#    fi

    ##  Check the host's queue size?
    QUEUE=0
    if [ -n "${BYTESQUEUED}" ] ; then
	if [ -d ${UUSPOOL}/${SITE} ] ; then
	    # Get queue size from directory size
	    QUEUE=`du -s "${UUSPOOL}/${SITE}" \
		    | ${AWK} '{ printf("%s000\n", $1); }'`

	else
	    ##  Get queue size from uuq command.
	    QUEUE=`QUEUE=`uustat -s${SITE} -Sq 2>/dev/null | ${AWK} 'BEGIN {printf "expr "} /queued/ {printf "%s + ", $(NF-1)} END {printf "0\n"}' | sh`
	fi
	test -z "${QUEUE}" && QUEUE=0
	if [ "${QUEUE}" -gt ${BYTESQUEUED} ] ; then
	    echo "${PROG}:  ${SITE} has ${QUEUE} bytes queued."
	    rm -f ${LOCK}
	    continue
	fi
    fi

    ##  Set how many bytes we can queue this time.
    if [ -z "${BYTESQUEUED}" ] ; then
	BATCHBYTES=${BYTESPERRUN}
    else
	BATCHBYTES=`expr ${BYTESQUEUED} - "${QUEUE}"`
	test ${BATCHBYTES} -gt ${BYTESPERRUN} && BATCHBYTES=${BYTESPERRUN}
    fi

    # Say how big each batch should be.  Assume 50% compression.
    if [ -n "${COMP}" ] ; then
	MAXBYTES=`expr ${DEFBYTES} \* 2`
    else
	MAXBYTES=${DEFBYTES}
    fi

    ##  Assemble uux command to have batcher(1) run.
    if [ -f ${SITE}.cmd ] ; then
	UUXCOM="`cat ${SITE}.cmd`"
    elif [ -n "${ECHO}" -o -n "${COMP}" ]; then
	UUXCOM="( ${ECHO} ${COMP} ) | ${UUX} - ${UUXFLAGS} ${SITE}!${RNEWS}"
    else
	UUXCOM="${UUX} - ${UUXFLAGS} ${SITE}!${RNEWS}"
    fi

    ## Create batches.
    if [ ${BATCHBYTES} -gt 0 ] ; then
	${PATHBIN}/batcher -B ${BATCHBYTES} -b ${MAXBYTES} -p "${UUXCOM}" \
	    ${SITE} ${BATCHFILE}
    fi

    rm -f ${LOCK}
done
