#!/bin/sh
# NOTE: this script needs to be run by the news user.
# Debian systems will not allow normal users to run ctlinnd and innxmit or rnews.
# Script distributed with suck but modified by Christophe Le Bars for the Debian package.
# authinfo compatibility by Brian Mays <brian@debian.org>.

COMMAND=$0
ETCDIR=/etc/suck			# location of sucknewsrc* and killfile*
GETNEWSCONF=${ETCDIR}/get-news.conf	# defaults for this script

if [ -z "${PAGER}" ]; then
        PAGER=more
fi

LOCAL_SERVER=`grep ^server: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c8-`
REMOTE_SERVER=`grep ^remoteserver: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c14-`
REMOTE_PORT=`grep ^remoteport: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c12-`
SITE=`grep ^outgoingfile: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c14-`
USER=`grep ^userid: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c8-`
PASSWD=`grep ^password: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c10-`
OPTS=`grep ^suckoptions: ${GETNEWSCONF} | cut -c13-`

REMOTE_SERVER_DEFAULT=${REMOTE_SERVER}

if [ -z "${LOCAL_SERVER}" ]; then
        LOCAL_SERVER=localhost
fi

if [ -z "${REMOTE_PORT}" ]; then
	REMOTE_PORT=119
fi

while [ ! -z "$1" ]; do
case "$1" in
  -outgoingfile)
                shift
                SITE_OPTION=$1
                shift
    ;;
  -server)
                shift
                LOCAL_SERVER=$1
                shift
    ;;
  -userid)
		shift
		USER=$1
		shift
    ;;
  -password)
		shift
		PASSWD=$1
		shift
    ;;
  -noauth)
		shift
		USER=""
		PASSWD=""
    ;;
  -port)
		shift
		REMOTE_PORT=$1
		shift
    ;;
  -q)
		shift
		OPTS="$OPTS -q"
    ;;
  -h)
	$PAGER <<__END__
Usage:  $COMMAND [-option parm] [<sitename>]

    <sitename>		The NNTP server you will connect to.
                        (if nothing, $COMMAND read ${GETNEWSCONF})

Options:
    -outgoingfile	<filename>  	The file of your remote server
					outgoing articles.
                            		(default = the remote server name)
    -userid		<userid>	The userid to send to the remote
					server.
    -password		<password>	The password to send to the remote
					server.
    -noauth				Do not send userid and password.
					(even if they are specified in
					${GETNEWSCONF})
    -port               <port>          Set remote port number.
    -server    		<sitename>  	Your local NNTP server.
                            		(default = "$LOCAL_SERVER")
    -q                                  Do not display the BPS and article
                                        count messages during download.
__END__
	exit 0
    ;;
  *)
                REMOTE_SERVER=$1
                shift
    ;;
esac
done

if [ -z "${SITE}" ]; then
        SITE=${REMOTE_SERVER}
fi

if [ ! "${REMOTE_SERVER_DEFAULT}" = "${REMOTE_SERVER}" ]; then
        SITE=${REMOTE_SERVER}
fi

if [ ! -z "${SITE_OPTION}" ]; then
        SITE=${SITE_OPTION}
fi

if [ ! -z "${USER}" ]; then
        AUTHOPTS="-U ${USER}"
else
        AUTHOPTS=""	
fi
if [ ! -z "${PASSWD}" ]; then
        AUTHOPTS="$AUTHOPTS -P ${PASSWD}"
fi

BINDIR=/usr/bin				# base directory for suck rpost and testhost
SBINDIR=/usr/sbin			# base directory for scripts
SPOOLDIR=/var/spool/news		# base directory for articles to be rposted

# location of the list of articles to upload
[ -d ${SPOOLDIR}/.outgoing ] && OUTGOING=${SPOOLDIR}/.outgoing/${SITE}
[ -d ${SPOOLDIR}/out.going ] && OUTGOING=${SPOOLDIR}/out.going/${SITE}
OUTGOINGNEW=${OUTGOING}.new		# temporarily used during upload of articles

NEWSDIR=/usr/lib/news			# base directory for INN news binaries 
INNXMIT=${NEWSDIR}/bin/innxmit		# location of innxmit
CTLINND=${NEWSDIR}/bin/ctlinnd		# location of ctlinnd
RNEWS=/usr/lib/news/input/rnews   	# location of rnews

SUCKDIR=/var/lib/suck				# location for suck files
LASTDIR=${SUCKDIR}/last.${REMOTE_SERVER}	# last get-news suck files
MSGDIR=${SUCKDIR}/msg.${REMOTE_SERVER}		# where to put MultiFile articles when getting them
OUTFILE=${SUCKDIR}/rpost.${REMOTE_SERVER}.$$	# used by rpost as article after it is filtered
BATCHFILE=${SUCKDIR}/batch.${REMOTE_SERVER}.$$	# Name of batchfile to build for rnews or innxmit

RPOST=${BINDIR}/rpost			# my rpost
SUCK=${BINDIR}/suck			# my suck
TESTHOST=${BINDIR}/testhost		# my testhost
SCRIPT=/usr/lib/suck/put-news		# my filter for rpost

LOG=/var/log/suck/suck-status

# is the local host up and running so we can post articles we download?
${TESTHOST} ${LOCAL_SERVER} -s -e
LOCAL_RESULT=$?
if [ ${LOCAL_RESULT} -ne 0 ]; then
	echo "Bad status for the ${LOCAL_SERVER} local NNTP news server"
	exit 1
fi

# is the remote host up and running so we can download articles?
${TESTHOST} ${REMOTE_SERVER} -N ${REMOTE_PORT} ${AUTHOPTS} -s -e
REMOTE_RESULT=$?
if [ ${REMOTE_RESULT} -ne 0 ]; then
	echo "Bad status for the ${REMOTE_SERVER} remote NNTP news server"
	exit 2
fi

[ -d ${LASTDIR} ] || mkdir ${LASTDIR}

if [ "${REMOTE_SERVER_DEFAULT}" = "${REMOTE_SERVER}" ]; then
	date >> $LOG
	echo 'get-news: download articles with the default remote server' >> $LOG
	if [ -f "${INNXMIT}" ]; then
		${SUCK} ${REMOTE_SERVER} ${OPTS} ${AUTHOPTS} -bi ${BATCHFILE} -dt ${SUCKDIR} -dm ${MSGDIR} -dd ${ETCDIR} -N ${REMOTE_PORT}
	else
		${SUCK} ${REMOTE_SERVER} ${OPTS} ${AUTHOPTS} -br ${BATCHFILE} -dt ${SUCKDIR} -dm ${MSGDIR} -dd ${ETCDIR} -N ${REMOTE_PORT}
	fi
	SUCK_STATUS=$?
	if [ ${SUCK_STATUS} -eq 0 ]; then
		echo "Downloaded Articles"
		mv ${ETCDIR}/sucknewsrc ${ETCDIR}/sucknewsrc.old
		mv ${SUCKDIR}/suck.newrc ${ETCDIR}/sucknewsrc
		mv ${SUCKDIR}/suck.sorted ${LASTDIR}
		if [ -f ${SUCKDIR}/suck.killlog ]; then	
			mv ${SUCKDIR}/suck.killlog ${LASTDIR}
		fi
		if [ -f ${ETCDIR}/suckothermsgs ]; then
			mv ${ETCDIR}/suckothermsgs ${LASTDIR}
		fi
	fi
else
	date >> $LOG
	echo 'get-news: download articles' >> $LOG
	if [ -f "${INNXMIT}" ]; then
		${SUCK} ${REMOTE_SERVER} ${AUTHOPTS} -bi ${BATCHFILE} -dt ${SUCKDIR} -dm ${MSGDIR} -dd ${ETCDIR} -p .${REMOTE_SERVER} -N ${REMOTE_PORT}
	else
		${SUCK} ${REMOTE_SERVER} ${AUTHOPTS} -br ${BATCHFILE} -dt ${SUCKDIR} -dm ${MSGDIR} -dd ${ETCDIR} -p .${REMOTE_SERVER} -N ${REMOTE_PORT}
	fi
	SUCK_STATUS=$?
	if [ ${SUCK_STATUS} -eq 0 ]; then
		echo "Downloaded Articles"
		mv ${ETCDIR}/sucknewsrc.${REMOTE_SERVER} ${ETCDIR}/sucknewsrc.${REMOTE_SERVER}.old
		mv ${SUCKDIR}/suck.newrc.${REMOTE_SERVER} ${ETCDIR}/sucknewsrc.${REMOTE_SERVER}
		mv ${SUCKDIR}/suck.*.${REMOTE_SERVER} ${LASTDIR}
		if [ -f ${ETCDIR}/suckothermsgs.${REMOTE_SERVER} ]; then
			mv ${ETCDIR}/suckothermsgs.${REMOTE_SERVER} ${LASTDIR}
		fi
	fi
fi


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

	if [ -f "${INNXMIT}" ]; then
		# 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 articles to be posted have
		# been written out, close off the old one (already renamed)
		# and create a new one.
		mv ${OUTGOING} ${OUTGOINGNEW}

		${CTLINND} flush ${SITE}
		echo "reloaded ctlinnd"

		# outgoing articles to post
		${RPOST} ${REMOTE_SERVER} -N ${REMOTE_PORT} ${AUTHOPTS} -b ${OUTGOINGNEW} -p ${SPOOLDIR} -f \$\$o=${OUTFILE} ${SCRIPT} \$\$i ${OUTFILE}
	
	else

		# outgoing articles to post
		${RPOST} ${REMOTE_SERVER} -N ${REMOTE_PORT} ${AUTHOPTS} -b ${OUTGOING} -p ${SPOOLDIR} -f \$\$o=${OUTFILE} ${SCRIPT} \$\$i ${OUTFILE}

	fi


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

if [ -f ${BATCHFILE} ]; then	
	# locally post articles
	if [ -f "${INNXMIT}" ]; then
		${INNXMIT} ${LOCAL_SERVER} ${BATCHFILE}
	else
		${RNEWS} ${LOCAL_SERVER} < ${BATCHFILE}
	fi

	if [ $? -ne 0 ]; then
		echo "Local posting error"
		exit -1;
	else
		echo "Posted Articles Locally"
		rm -rf ${MSGDIR} ${BATCHFILE}
	fi	
fi	

