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

LOCAL_HOST=localhost

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

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

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

SCRIPT=${BASEDIR}/put.news.sm.pl	# 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
NEWSGROUP=news				# which group owns the file in out.going, typically either news or uucp.

TESTHOST=${NEWSDIR}/testhost
RPOST=${NEWSDIR}/rpost
SUCK=${NEWSDIR}/suck

if [ $# -gt 1 ]; then
  echo "Usage: getnew [<sitename>]"
  exit
fi

if [ -n "$1" ]; then
  CONFIG=/etc/news/suck.d/$1
else
  CONFIG=/etc/news/suck.d/default
fi

# 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

if [ -f $CONFIG ]; then
    . $CONFIG
else
    echo "Configuration file doesn't exist"
    exit
fi

if [ -z $SITE ]; then
    echo "Site definition was not defined"
    exit
fi

if [ -z $NNTP_HOST ]; then
    echo "Remote host was not defined"
   exit
fi

if [ -n "$NNTP_USER" -a -n "$NNTP_PASS" ]; then
    AUTHINFO="-u -Q"
fi

if [ -z $NNTP_SSL ]; then
    NNTP_SSL="yes"
fi

if [ "$NNTP_SSL" == "yes" ]; then
    SUCKSSL="-ssl"
    RPOSTSSL="-z"
elif [ "$NNTP_SSL" != "no" ]; then
    echo "Invalid value for SSL connection"
    exit
fi

if [ -z $POST ]; then
  POST="yes"
fi

if [ $POST != "yes" -a $POST != "no" ]; then
  echo "Invalid value for post"
  exit
fi

export NNTP_USER
export NNTP_PASS

OUTGOING=${SPOOLDIR}/outgoing/${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

# 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} ${NNTP_HOST} -s
REMOTE_RESULT=$?

if [ ${REMOTE_RESULT} -eq 0 -a ${LOCAL_RESULT} -eq 0 ]; then
	
	# download messages
	${SUCK} ${NNTP_HOST} -c -A -bp -hl ${LOCAL_HOST} -dt ${TMPDIR} -dm ${MSGDIR} -dd ${BASEDIR} ${AUTHINFO} ${SUCKSSL}
	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 [ $POST = "yes" ]; then

	    if [ -s ${OUTGOING}  -o -s ${OUTGOINGNEW} ]; then

		${TESTHOST} ${NNTP_HOST} -s

		if [ $? -ne 0 ]; then
			echo "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} ${NNTP_HOST} -d -b ${OUTGOINGNEW} -F ${SCRIPT} ${AUTHINFO} ${RPOSTSSL}

			ERRLEV=$?
			if [ ${ERRLEV} -eq 0 ]; then
				echo "Remotely posted articles"
			elif [ ${ERRLEV} -eq 1 ]; then
				echo "Error posting a message"
			elif [ ${ERRLEV} -eq 2 ]; then
				echo "Unable to do NNTP authorization with remote server"
			elif [ ${ERRLEV} -eq 3 ]; then
				echo "Unexpected answer from remote server to a command while doing NNTP authorization"
			elif [ ${ERRLEV} -eq -1 ]; then
				echo "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
	
	    fi	
	
	fi

	echo "You can hang up the modem now"

fi

rm -f ${LOCKFILE}
