#! /bin/sh
. /usr/local/news/lib/innshellvars

########################################################################
# controlbatch - Run controlchan against a batch file.
#
# Command usage: controlbatch [feedsite batchfile]
# Defaults are feedsite: controlchan!, batchfile: ${BATCH}/controlchan!
########################################################################
#
# This script will run controlchan against a batch file.  You can use
# it to clear occasional backlogs while running controls from a
# channel, or even skip the channel and run control messages as a file
# feed.
#
########################################################################
#
# If you're doing the channel thing, you might want to put something
# like this in your crontab to do a cleanup in the wee hours:
#
#       00 04 * * *     <pathbin>/controlbatch
#
########################################################################
#
# If you would rather skip the channel and just process controls each
# hour in a batch, use this newsfeeds entry instead of the "stock"
# version:
#
# controlchan!\
#       :!*,control,control.*,!control.cancel\
#       :Tf,Wnsm:
#
# And, a crontab entry something like this:
#
#       30 * * * *     <pathbin>/controlbatch
#
########################################################################

batchlock="${LOCKS}/LOCK.controlbatch"
mypid=$$

# A concession to INN 1.x
if [ me${PATHBIN}ow = meow ]; then
    PATHBIN=${NEWSBIN}
    export PATHBIN
fi

# See if we have no arguments and should use the defaults. If there are
# arguments, make sure we have enough to attempt something useful.
if [ me${1}ow != meow ]; then
    if [ me${2}ow = meow ]; then
        echo "Usage: ${0} [feedsite batchfile]" >&2
        exit 1
    else
        feedsite=${1}
        batchfile=${2}
    fi
else
    feedsite=controlchan\!
    batchfile=controlchan\!
fi

# Check if any other copies of controlbatch are running.  If we are not
# alone, give up here and now.
"${PATHBIN}/shlock" -p "$mypid" -f "${batchlock}" || exit 0

cd "${BATCH}" || {
    echo "${0}: cannot cd to ${BATCH}" >&2
    rm -f "${batchlock}"
    exit 1
}

if [ -s "${batchfile}.work" ]; then
    cat "${batchfile}.work" >>"${batchfile}.doit"
    rm -f "${batchfile}.work"
fi

if [ -s "${batchfile}" ]; then
    mv "${batchfile}" "${batchfile}.work"
    if "${PATHBIN}/ctlinnd" -s -t30 flush "${feedsite}"; then
        cat "${batchfile}.work" >>"${batchfile}.doit"
        rm -f "${batchfile}.work"
    fi
fi

if [ -s "${batchfile}.doit" ]; then
    # Only remove the batch file on success.  If controlchan exits with
    # a non-zero status (for instance because it died while processing
    # one of the control articles), leave it in place so that it is
    # retried on the next run instead of silently losing the control
    # articles it did not get to process.
    if "${PATHBIN}/controlchan" \
        <"${batchfile}.doit" >>"${MOST_LOGS}/controlbatch.log" 2>&1; then
        rm -f "${batchfile}.doit"
    fi
fi

rm -f "${batchlock}"
