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

REMOTE_HOST=news.cis.dfn.de
LOCAL_HOST=localhost
USER=meinuser
PASSWORD=meinpasswort

SPOOLDIR=/var/spool/news		# base directory for articles to be rposted
NEWSDIR=/usr/lib/news			# base directory for news binaries 
BASEDIR=/var/spool/news/tmp		# base directory for scripts and data files

CTLINND=${NEWSDIR}/bin/ctlinnd		# location of binary
SHLOCK=${NEWSDIR}/bin/shlock		# location of binary

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

OUTGOING=${SPOOLDIR}/outgoing/${SITE}  # location of the list of articles to upload
OUTGOINGNEW=${OUTGOING}.new            # file to contain the list temporarily

LOCKFILE=${BASEDIR}/getnews.lock	# lock file to prevent multiple instances of script

TESTHOST=testhost
RPOST=/usr/bin/rpost
SUCK=suck

# if we are already running, abort 

# Echo current date!
echo -n "The current date is as follows: "
date

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 messages we download?
${TESTHOST} ${LOCAL_HOST} -s
LOCAL_RESULT=$?

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

if [ ${REMOTE_RESULT} -eq 0 -a ${LOCAL_RESULT} -eq 0 ]; then
	
	# download messages
	${SUCK} ${REMOTE_HOST} -q -c -A -bp -hl ${LOCAL_HOST} -dt ${TMPDIR} -dm ${MSGDIR} -dd ${BASEDIR} -U $USER -P $PASSWORD
	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 messages
	if [ -s ${OUTGOING}  -o -s ${OUTGOINGNEW} ]; then

		${TESTHOST} ${REMOTE_HOST} -s

		if [ $? -ne 0 ]; then
			echo "Remote posting host not responding"
		else	
			/usr/lib/news/bin/nntpsend
		fi
	fi	
	
	echo "You can hang up the modem now"

fi

rm -f ${LOCKFILE}
