#!/bin/sh
# procnews: send out and bring in our news.
# Copyright 1995 John A. Phillips - john@linux.demon.co.uk
# usage: procnews [news_server]

# Set up the defaults
default_server="news.demon.co.uk"
default_prefix=".demon.co.uk"
slurp_tries=3

# Set the news server:
# * If you don't name a news_server it defaults to $default_server
# * If you give a name without a "." $default_prefix is added
# * If you give a name including a "." the name is used literally
news_server=${1:-${default_server}}
if [ `echo $news_server | grep -Fc "."` -eq 0 ]; then
    news_server=${news_server}${default_prefix}
fi

# Send out the news we have ready to go
su news -c "/usr/lib/newsbin/batch/sendbatches demon" &

# Pick up the news via slurp.
slurp_result=4
while [ $slurp_result -eq 4 -a $slurp_tries -gt 0 ]; do
    su news -c "/usr/local/sbin/slurp -x $news_server"
    slurp_result=$?
    sleep 3
    slurp_tries=`expr $slurp_tries - 1`
done
if [ $slurp_result -ne 0 ]; then
    echo slurp failed with exit code $slurp_result
    if [ $slurp_result -eq 4 -o $slurp_result -eq 3 ]; then
        exit $slurp_result
    fi
fi

# Wait for the outgoing news processes to complete if required.
wait

# Unbatch and store the news that came in; then update the nn threading 
# database; then update the nn subject database (all in the background).
# Remove the last two commands in the braces below if you don't use nn.
{   su news -c /usr/lib/newsbin/input/newsrun; \
    su news -c /usr/lib/nn/nnmaster; \
    su news -c /usr/lib/nn/nnspew } &

exit $slurp_result
