#!/bin/sh
#
# BEFORE USING - check to ensure all the paths defined below are correct!!
#
# NOTE: this script probably needs to be run by root.  Most systems will
# not let a normal user run ctlinnd 
#
# Changed for multi news-host ability by Peter Sobisch <petersob@gmx.net>
# added "site:" an each message
#
# Syntax: get.news <site> <remote_host>

SITE=$1					# name of site (newsfeeds)
REMOTE_HOST=$2				# name of remote host

LOCAL_HOST=localhost
SPOOLDIR=/var/spool/news		# base directory for articles to be rposted
BASEDIR=/var/lib/news/suck		# base directory for scripts and data files

NEWSDIR=/usr/lib/news                   # base directory for news binaries
LOCALPOST=${NEWSDIR}/bin/innxmit	# location of binary
CTLINND=${NEWSDIR}/bin/ctlinnd		# location of binary
SHLOCK=${NEWSDIR}/bin/shlock		# location of binary

TMPDIR=${BASEDIR}			# location for suck.* files
MSGDIR=${BASEDIR}/Msgs.${SITE}		# where to put MultiFile messages when getting them
BATCHFILE=${TMPDIR}/batch.${SITE}	# Name of batchfile to build for rnews or innxmit
OUTGOING=${SPOOLDIR}/out.going/${SITE}	# location of the list of articles to upload
OUTGOINGNEW=${OUTGOING}.new		# file to contain the list temporarily
OUTGOINGFAIL=${OUTGOINGNEW}.fail	# file with failed xfers
SCRIPT=${BASEDIR}/put.news		# my filter for rpost
OUTFILE=/tmp/tmp$$.${SITE}		# used by rpost as article after it is filtered
LOCKFILE=${BASEDIR}/getnews.${SITE}.lock	# lock file to prevent multiple instances of script
NEWSGROUP=news				# which group owns the file in out.going, typically either news or uucp.

TESTHOST=testhost
RPOST=rpost
SUCK=suck

# if we are already running, abort 

trap 'rm -f ${LOCKFILE} ; echo "$SITE: Aborting" ; exit 1' 1 2 3 15
${SHLOCK} -p $$ -f ${LOCKFILE}
if [ $? -ne 0 ]; then
	echo "$SITE: Already running, can't run two at one time"
	exit
fi

# is the local host up and running so we can post messages we download?
${TESTHOST} ${LOCAL_HOST} -s
LOCAL_RESULT=$?
if [ ${LOCAL_RESULT} -ne 0 ]; then
	echo "$SITE: Local Host not responding"
fi

# is the remote host up and running so we can download messages?
${TESTHOST} ${REMOTE_HOST} -s
REMOTE_RESULT=$?

if [ ${REMOTE_RESULT} -ne 0 ]; then
	echo "$SITE: Remote Host not responding"
fi

if [ ${REMOTE_RESULT} -eq 0 -a ${LOCAL_RESULT} -eq 0 ]; then
	
 # download messages
 echo "$SITE: download articles in progress"
 ${SUCK} ${REMOTE_HOST} -c -S ${BASEDIR}/suck.log -bi ${BATCHFILE} -dt ${TMPDIR} -dm ${MSGDIR} -dd ${BASEDIR} -p .${SITE}
 SUCK_STATUS=$?

	if [ ${SUCK_STATUS} -eq 0 ]; then
		echo "$SITE: download articles finished"
	elif [ ${SUCK_STATUS} -eq 1 ]; then
		echo "$SITE: no articles to download"
	elif [ ${SUCK_STATUS} -eq 2 ]; then
		echo "$SITE: unexpected answer from remote server to an issued command"
	elif [ ${SUCK_STATUS} -eq 4 ]; then
		echo "$SITE: can't do NNTP authorization"
	elif [ ${SUCK_STATUS} -eq -1 ]; then
		echo "$SITE: general failure"
	fi

	# now upload messages
	if [ -s ${OUTGOING}  -o -s ${OUTGOINGNEW} ]; then

                echo "$SITE: upload articles in progress"

		${TESTHOST} ${REMOTE_HOST} -s

		if [ $? -ne 0 ]; then
			echo "$SITE: Remote posting host not responding"
		else
			# this is needed by INND so that the outgoing file will be
			# properly flushed and we have a new blank file to work with
			# when we are done
			# First mv the current one to a new file name
			# Since innd already has the file open, it doesn't care 
			# about the rename.
			# The flush will ensure that all messages to be posted have
			# been written out, close off the old one (already renamed)
			# and create a new one.

			# if the outgoingnew already exists, it means we aborted last time
			# so don't try to do it again
			if [ ! -s ${OUTGOINGNEW} ]; then
				mv ${OUTGOING} ${OUTGOINGNEW}
				${CTLINND} flush ${SITE}
			fi

			# outgoing messages to post

			${RPOST} ${REMOTE_HOST} -d -S ${BASEDIR}/rpost.log -b ${OUTGOINGNEW} -p ${SPOOLDIR} -f \$\$o=${OUTFILE} ${SCRIPT} \$\$i ${OUTFILE}

			ERRLEV=$?
			if [ ${ERRLEV} -eq 0 ]; then
				echo "$SITE: articles uploaded"
				rm ${OUTFILE}
			elif [ ${ERRLEV} -eq 1 ]; then
				echo "$SITE: error posting a message"
			elif [ ${ERRLEV} -eq 2 ]; then
				echo "$SITE: unable to do NNTP authorization with remote server"
			elif [ ${ERRLEV} -eq 3 ]; then
				echo "$SITE: unexpected answer from remote server to a command while doing NNTP authorization"
			elif [ ${ERRLEV} -eq -1 ]; then
				echo "$SITE: fatal error"
			fi

			if [ -f ${OUTGOINGFAIL} ]; then
				mv ${OUTGOINGFAIL} ${OUTGOINGNEW}	# so we can re do it
				chown news.${NEWSGROUP} ${OUTGOINGNEW}
				chmod 664 ${OUTGOINGNEW}
			fi
		fi
        else
                echo "$SITE: no articles to upload"
	fi	
	
	if [ ${SUCK_STATUS} -eq 0 ]; then	
		# locally post articles
		${LOCALPOST} ${LOCAL_HOST} ${BATCHFILE}
		
		if [ $? -eq 0 ]; then
			echo "$SITE: downloaded articles posted locally"
			rm -rf ${MSGDIR}
		fi	
	fi	
fi

rm -f ${LOCKFILE}

