# EMACS_MODES: !fill
# This shell should be run just before network1
WWIVDIR=c:/wwiv
UUCPDIR=c:/usr/lib/uucp
WORKDIR=c:/usr/lib/uucp/incoming
TEMPDIR=c:/rd1
export WWIVDIR UUCPDIR WORKDIR TEMPDIR


#set -x

echo "\n# PROCWWIV: WWIVnet->UUCP - Copyright 1992 (C) Andrew Gaunt"

echo "Cleaning work directory"
rm -f $WORKDIR/*
echo "### Checking p.net files"
for i in $WWIVDIR/data/p?.net
do
	if [ -f "$i" ]
	then
		echo Found $i
		$UUCPDIR/extract +to 3 6300 $i $TEMPDIR/p.net $WORKDIR
#		if [ $? = 0 ]
#		then
#			rm -f $i
#			mv -f $TEMPDIR/p.net $i
#		else
#			echo "###### Extract failed!\007"
#		fi
		cd $WORKDIR
		for i in *.msg
		do
			if [ -r $i ]
			then
				echo "rmail `cat ${i%.msg}.add` <$i"
				rmail `cat ${i%.msg}.add` <$i
			fi
		done	

#		echo "Cleaning work directory"
#		rm -f $WORKDIR/*	
	fi
done

cd $WWIVDIR
network1

echo "Cleaning work directory"
rm -f $WORKDIR/*
echo "### Checking for mail to WWCP node"
for i in $WWIVDIR/data/s504.net
do
	if [ -f "$i" ]
	then
		echo Found $i

 		# Filter out the confirmations for read mail
		echo "### Deleting junk"
		$UUCPDIR/extract -type 15 0 $i $TEMPDIR/p.net $WORKDIR && mv -f $TEMPDIR/p.net $i
		# Anything mail sent to 1@504 will be extracted.
		echo "### Extracting messages"
		$UUCPDIR/extract +to 1 504 $i $TEMPDIR/p.net $WORKDIR
		if [ $? = 0 ]
		then
			rm -f $i
			cat $TEMPDIR/p.net >>$WWIVDIR/data/p1.net &&\
				rm -f $TEMPDIR/p.net
			
		else
			echo "###### Extract failed!\007"
		fi
		cd $WORKDIR
		for i in *.msg
		do
			if [ -r $i ]
			then
#				echo "To: \c"
#				cat ${i%.msg}.add
#				echo "Message:"
#				cat $i
#				echo "--------"
				echo "rmail `cat ${i%.msg}.add` <$i"
				rmail `cat ${i%.msg}.add` <$i
			fi
		done	

		echo "Cleaning work directory"
		rm -f $WORKDIR/*	
	fi
done
cd $WWIVDIR
network1

#cd $WWIVDIR
#network1
#network2
#netpurge

#echo "\n################# End of procwwiv #############################"

