#!/bin/sh
#
# $Id: cleanbatch,v 1.6 1993/06/05 18:26:10 alden Exp $
#
# cleanbatch - process unsent batchfiles from "nntplink -q ..."
#
# $Log: cleanbatch,v $
# Revision 1.6  1993/06/05  18:26:10  alden
# Fixed bug in cleanbatch which could lose batchfiles, also added "-v" option
#
# Revision 1.5  1993/05/26  12:24:01  root
# Modified to work with a cleanbatch-list of the same form as links-list
#
# Revision 1.4  1993/05/14  13:40:37  alden
# If ctlinnd returns an error, don't run nntplink
#
# Revision 1.3  1993/05/14  12:07:32  alden
# Added sysname to ctlinnd flush
#
# Revision 1.2  1993/05/11  22:08:48  root
# Took out "-d" option to nnptlink.  :-)
# Modified not to "exit -1" if a batchfile.old still exists - just go on
#     to the next site
#
# Revision 1.1  1993/05/05  17:28:31  root
# Initial revision
#
#
#
# Please read through the Configure section making the appropriate changes
#
# *** Configuration Section ***
#
# If you are running INN, then you need to change the first line
# to include the path to your INN config script.
#
# =()<. ${_PATH_SHELLVARS-@<_PATH_SHELLVARS>@}>()=
. ${_PATH_SHELLVARS-/usr/spool/news/lib.news/innshellvars}
export PATH
#

datafile=/tmp/l.$$
trap "rm -f $datafile; exit 0" 0 1 2 3 9 15

#
# Where is the list which contains the sites that we should startup nntplink
# NOTE: NEWSLIB is defined in the INN config script
sitelist=$NEWSLIB/cleanbatch-list

# Where are the batchfiles?
batchdir=$BATCH

#
# What goes on the end of "${batchdir}/${site}" to get the batchfile?
# (ie: should we use "${batchdir}/${site}/togo" or should we just use
# "${batchdir}"?)
rest=/togo
# rest=

#
# *** End of Configure Section ***
#

prog=`basename $0`
orig_IFS=$IFS

lockfile=/tmp/$prog-lock
verbose=

if test -r $lockfile
then
    pid=`cat $lockfile`
    kill -0 $pid 2> /dev/null
    if test $? -eq 1
    then
	rm -f $lockfile
    else
	echo "$prog: Sorry, but $prog($pid) is already running"
	exit 1
    fi
fi

trap "rm -f $lockfile; exit 0" 0 1 2 3 9 15

echo $$ > $lockfile

if test $# -gt 0
then
    case $1 in
    -v)
	verbose="true"
	;;
    *)
	echo "usage: $prog [-v]"
	exit 1
	;;
    esac
fi

while read pri site opts
do
    if [ "$pri" = "#" ]; then
	continue
    fi

#
# If $opts specify a batchfile name, batch directory or sysname, go ahead
# and extract it
    set - $opts

    bfile=
    bdir=
    sysname=

    while test $# -gt 0
    do
	case "$1" in
	    -b) shift ; bfile=$1 ; break ;;
	    -B) shift ; bdir=$1 ; break ;;
	    -s) shift ; sysname=$1 ; break ;;
	esac
	shift
    done

    bdir=${bdir:-$batchdir}
    bfile=${bfile:-$site$rest}
    sysname=${sysname:-$site}

    cd $bdir

    if test -f $bfile.old
    then
	nice -$pri nntplink $opts -i batchfile -o -B $bdir -b $bfile.old $site

	if test -f $bfile.old
	then
	    if test -n "$verbose"
	    then
		echo "$prog: The batchfile $bfile.old still exists."
		echo "$prog: There may be a problem with the site $site."
		echo " "
	    fi
	    continue
	fi
    fi

    if test -f $bfile
    then
	mv $bfile $bfile.old
	ctlinnd -s flush $sysname && \
	    nice -$pri nntplink $opts -i batchfile -o -B $bdir -b $bfile.old $site
    fi

    for file in $bfile*
    do

	IFS=$IFS.

	set - $file

	while test $# -gt 1
	do
	    pid=$1
	    shift
	done

	IFS=$orig_IFS

#
# If <bfile.pid.tmp> exists - make sure that the process which created the
# file is still around - just in case it crashed.
	if test "X$1" = "Xtmp"
	then
	    kill -0 $pid 2> /dev/null
	    if test $? -eq 0
	    then
		continue
	    fi
	else
	    pid=$1
	fi

	if test $pid -gt 0
	then

	    mv $file $bfile.old
	    nice -$pri nntplink $opts -i batchfile -o -B $bdir -b $bfile.old $site
	fi

	if test -f $bfile.old
	then
	    if test -n "$verbose"
	    then
		echo "$prog: The batchfile $bfile.old still exists."
		echo "$prog: There may be a problem with the site $site."
		echo " "
	    fi
	    break
	fi

    done
done < $sitelist

