#!/bin/ash
# rustic suck 
# (C) 1998 M. Andreoli

#set -x

# Include suck data

if [ -r /usr/script/suck.conf ] ; then
	. /usr/script/suck.conf
else	# testing
	. /root/mulinux/suck/suck.conf
fi

# user data

if [ -r /setup/cnf/nntp.cnf ] ; then
	. /setup/cnf/nntp.cnf 
else	# testing
	NNTPSERVER=localhost
	NNTP_PORT=nntp
fi

> $LOGFILE

# syntax

usage()
{
cat <<END

rustic-suck, by M. Andreoli (C) 1998

Usage: `basename $0` [-h|-c]
	Option: -c update automatically suck.rc

END
exit 1
}

case "Z$1Z" in
Z-hZ) usage; shift;;
Z-cZ) c_opt=yes; shift;;
esac

# Start netcat client 

> $SUCK_NEW

cat $SUCK_RC| while [ 1 ] ; do
read line
set -- $line
group=$1; status=$2; max=$3
shift 3 2>/dev/null

[ -z "$max" ] && max=9999
[ -z "$group" ] && break

case $group in
\#*) ;;
*)

echo "$group ${status} ${max}" > $TAG.stat
echo "- Retreiving article from server $NNTPSERVER: $group ... wait"
nc  -w 10 -e $SUCK_PRO $NNTPSERVER ${NNTP_PORT} 

set -- `cat $TAG.stat 2>/dev/null`

num=$4
if [ "$num" ] ; then
	echo "- Downloaded $num article."
	echo "$1 $2 $3" >> $SUCK_NEW
else
	error=yes
fi

;;
esac


done

if [ "${c_opt}"  ] && [ -z "$error" ] ; then
	cp $SUCK_NEW $SUCK_RC
else
	read -p "- Update $SUCK_RC (y/[n])?" up
	[ "$up" = y ] && cp $SUCK_NEW $SUCK_RC
fi

rm -f $TAG.*

# End
