[news.admin] mvjunk - a program to tidy away stuff in your junk newsgroup

news@stl.stc.co.uk (News admin c/o dww) (05/01/89)

Back in 1985 someone at TRW wrote and posted to the net the program mvjunk,
which goes through the articles in 'junk' and puts them into the news
groups given in their Newsgroups: headers, creating any groups that don't
already exist.   But the version we got had bugs, for example it did not
create the groups and directories properly.  Despite that it was useful,
and eventually - last month - I got round to fixing the bugs, adding code
to create parent directories too, making it tell you what it was doing as
it did it, and adding a few comments to the code.  No doubt improvements in
style and performance are possible, but I decided to leave the parts that
worked unchanged, and follow the original author's style in my mods. 
I have tested it on Sun OS3.5 and VAX UNIX 4.2 - I know no reason why it
should not work on other flavours of UNIX, but nor do I give any guarantees.

Here it is - use as you wish, at your own risk of course.

Enjoy!

- - - - - - - - - - - Cut Here 8== - - - - - - - - - - - - - - - - - - - - - -
#!/bin/sh
# mvjunk.sh
#
# Move news files out of junk and into their proper directories,
# updating the active list and localgroups as we go.
# Unknown newsgroups found in junked articles are created by adding an
# entry to the active file and creating the directory.    A newgroups
# article is also posted in case the group is required across the local net,
# and to put the group creation into the news logs.
#
# TRW Advanced Technology Facility  STA 2-Jun-1985
#
# Modified 10/22/85-James M. Scardelis, SA, Warner Computer Systems
#       to create newsgroups locally only.
#
# Modified April 1989 David Wright  dww@stl.stc.co.uk
#	to fix bugs in group creation and make directories include parents
#
# This program is in the public domain - do with it whatever you like.
#
# Be CAREFUL with that active list!  Although we try to write out
# buffers as quickly as possible, conflicts can occur if someone else
# tries to use it at the same time this script is running!
#
# Actions taken and strange occurences are recorded in mvjunk-log.

# Set DISTRIB to cover your local net or organisation if you want any
# new groups found to be created across your net.   The default is local only
DISTRIB=local

JUNK=/usr/spool/news/junk
SPOOL=/usr/spool/news
LIB=/usr/lib/news		# See line 121

umask 22

chmod u+wr $LIB/active

cd $JUNK

if /bin/test -z "`ls`" ; then exit
fi

chmod u+wr *

for j in $JUNK/*
do
	for group in `egrep '^Newsgroups:' $j | head -1 | \
		sed 's;Newsgroups:[ 	];;' | \
		sed 's;,; ;g'`
	do
		echo "Found article $j for group $group"
		echo "Found article $j for group $group" >> $LIB/mvjunk-log
		l=`egrep '^Control:' $j`
		if /bin/test -n "$l" ; then
			GROUPDIR="control"
			group="control"
		else
			GROUPDIR=`echo $group | sed 's;\.;\/;g'`
		fi
#
		cd $SPOOL
		if /bin/test -d $GROUPDIR ; then
		# Directory exists - work out number of the next article
			cd $GROUPDIR
			l=`ls`
			if /bin/test -n "$l" ; then
				a=`ls | sort -r -n`
				for f in $a
				do
					if /bin/test -f "$f" ; then
						article=`echo $f "+1" | bc`
						break
					fi
				done
			else
				article=1
			fi
		else
		# No directory for this group - create one, and set art.no = 1
		# Create parent directories too if they don't exist already
		dir=$SPOOL
		for subdir in `echo $group | sed 's;\.; ;g'`
		do
			dir=${dir}/${subdir}
			if /bin/test ! -d "$dir" ; then
				echo "Creating directory $dir"
				echo "Creating directory $dir">>$LIB/mvjunk-log
				mkdir $dir
			fi
		done
		article=1
		fi

		# Now we know where to put it, put it there,
		# and update active file too
		cd $SPOOL/$GROUPDIR
		if /bin/test ! -d "$article" ; then

			cp $j $SPOOL/$GROUPDIR/$article

			chmod ugo+wr $SPOOL/$GROUPDIR/$article
			chown news $SPOOL/$GROUPDIR/$article
			chgrp news $SPOOL/$GROUPDIR/$article
			the_date=`date`
			entry=`egrep "^$group " $LIB/active`
			if /bin/test -z "$entry" ; then
			    echo \
			    "Created $the_date to hold articles found in junk"|\
				$LIB/inews -d $DISTRIB -C $group
			    echo "Created group $group at $the_date" \
				>> $LIB/mvjunk-log
			    entry="$group 00001 00001 y"
			    echo $entry >> $LIB/active
			    echo \
			    "$group	found in junk; created at $the_date" \
						>> $LIB/localgroups
			fi
			entry="^$entry "
#
# There should be a way of passing shell variables into awk, but since
# we don't know how to do that we just hardwire it.  Kludgy... no?
#
			echo $article $entry $the_date | \
				awk 'BEGIN { FS = " " } \
			{ if ( NF != 11 ) printf "Munged active entry: %s\n",\
				$0 >"/usr/lib/news/mvjunk-log" ; \
			if ( length($1) > 5 ) printf "Field too large: %s\n",\
				$0 >"/usr/lib/news/mvjunk-log" ; \
			if ( length($3) > 5 ) printf "Field too large: %s\n",\
				$0 >"/usr/lib/news/mvjunk-log" ; \
			if ( length($4) > 5 ) printf "Field too large: %s\n",\
				$0 >"/usr/lib/news/mvjunk-log" ; \
			printf "/%s/s;%s;",$2,$3 ; \
			for ( i = 5 - length($1) ; i > 0 ; i-- ) printf "0" ; \
			print $1 ";" ; \
			if ( length($4) < 5 ) { \
			    printf "s;%s;",$4 ; \
			    for ( i = 5 - length($1) ; i > 0 ; i-- ) printf "0" ; \
			    print $4 ";" \
			    } \
			if ( $5 != "y" && $5 != "n" ) print "s;.$;y;" } \
			    	END { printf "w\nq\n" }' | ed - $LIB/active
		else
		   echo \
		   "Hey, there's a directory called $SPOOL/$GROUPDIR/$article!"
		   echo \
		   "Hey, there's a directory called $SPOOL/$GROUPDIR/$article!"\
			>> $LIB/mvjunk-log
		fi
	done
	if /bin/test -f $SPOOL/$GROUPDIR/$article ; then
		/bin/rm -f $j		# But my MOTHER lives in Hackensack!
	else
		echo "Couldn't find the article I just installed!"
		echo "Couldn't find the article I just installed!" >> \
			$LIB/mvjunk-log
	fi
done
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -