#!/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 rnews


REMOTE_HOST=news.pixi.com
LOCAL_HOST=localhost

SPOOLDIR=/usr/spool/news		# base directory for articles to be rposted
NEWSDIR=/usr/lib/news			# base directory for news binaries 
BASEDIR=/home/boby/doNews		# base directory for suck rpost and scripts

SHLOCK=${NEWSDIR}/bin/shlock

TMPDIR=${BASEDIR}			# location for suck.* files
MSGDIR=${BASEDIR}/Msgs			# where to put MultiFile articles when getting them

OUTGOING=${SPOOLDIR}/out.going/pixi	# location of the list of articles to upload
SCRIPT=${BASEDIR}/put.news		# my filter for rpost
OUTFILE=/tmp/tmp$$			# used by rpost as article after it is filtered
LOCKFILE=${BASEDIR}/getnews.lock	# lock file to prevent multiple instances of script

TESTHOST=testhost
RPOST=rpost
SUCK=suck
TESTHOST=testhost

# if we are already running, abort

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

# is the local host up and running so we can post articles we download?
${TESTHOST} ${LOCAL_HOST} -s
LOCAL_RESULT=$?

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

if [ ${REMOTE_RESULT} -eq 0 -a ${LOCAL_RESULT} -eq 0 ]; then

	# download articles
	${SUCK} ${REMOTE_HOST} -c -A -bp -hl ${LOCAL_HOST} -dt ${TMPDIR} -dm ${MSGDIR} -dd ${BASEDIR}
	SUCK_STATUS=$?

	if [ ${SUCK_STATUS} -eq 0 ]; then
		echo "Downloaded Articles"
	elif [ ${SUCK_STATUS} -eq 1 ]; then
		echo "No articles to download"
	elif [ ${SUCK_STATUS} -eq 2 ]; then
		echo "Unexpected answer from remote server to an issued command"
	elif [ ${SUCK_STATUS} -eq 4 ]; then
		echo "Can't do NNTP authorization"
	elif [ ${SUCK_STATUS} -eq -1 ]; then
		echo "General failure"
	fi

	# now upload articles
	if [ -s ${OUTGOING} ]; then
		# outgoing articles to post

		${TESTHOST} ${REMOTE_HOST} -s

		if [ $? -ne 0 ]; then
			echo "Remote posting host not responding"
		else
			${RPOST} ${REMOTE_HOST} -d -b ${OUTGOING} -p ${SPOOLDIR} -f \$\$o=${OUTFILE} ${SCRIPT} \$\$i ${OUTFILE}

			if [ $? -ne 0 ]; then
				echo "Error remote posting"
			else
				echo "Remotely posted articles"
				rm ${OUTFILE}
			fi 
		fi
	fi	
	
	echo "You can hang up the modem now"

fi

rm -f ${LOCKFILE}
