#!/bin/bash

# USAGE: newspress [-l <#links>] <newsgroup> [<newsgroup> ...]
#
# Option "-l" forces to search and compress all arcticles with a given number
#             of links (thread). If this option is not used, all articles 
#             are compressed regardless of the number of links. 
#             This is realy SLOWLY since corresponding filenames in several 
#             newsgroups have to be searched first!
#
# Option "-s" gives the minimal size of a news article to be compressed.
#             Default is 1k
#
# Option "-t" test only the existance of files and do not compress
#
# ToDo:       trap handling to avoid interupting the script, while creating
#             the sequence of links and the compressed file 
#             (function compresslinks).
#
# Version.    0.1 (16. March 1997)
#
# Please      send bugfixes / enhancements to the author
# Author:     reiner@schildi.xnc.com Germany

export LD_ZLIB_DISABLE=1

NEWSSPOOL=/var/spool/news	# ADJUST TO YOUR NEEDS !!!
LOGDEV="/dev/null"
size="-size +1k";
testonly=0;


function usage()
{	echo "USAGE:" $0 '[-l <#links>] [-s <size>] [-v] <newsgroup> [<newsgroup> ...]'
}

function findlinks ()
{
	while read fname; do
		# numlinks=`ls -l $fname | awk ' {print $2}'`
		ngroup=`dirname $fname`
		groups=`awk '/Newsgroups: / { print $2}' $fname | tr ',.' ' /'`
		ifile=`ls -i $fname | awk ' {print $1}'`

		echo "SEARCHFOR INODE $ifile IN $groups" > $LOGDEV;

		for group in $groups; do
			if [ -d $NEWSSPOOL/$group ]; then
				if [ "$NEWSSPOOL/$group" = "$ngroup" ]; then
					echo -n "$fname ";
					echo -n "$fname " > $LOGDEV;
				else
					fname="`find $NEWSSPOOL/$group -maxdepth 1 -inum $ifile`";
					if [ $? = 0 ]; then
						echo -n "$fname ";
						echo -n "$fname " > $LOGDEV;
					else
						echo > $LOGDEV;
						echo "$ifile NOT FOUND in $group" > $LOGDEV;
					fi
				fi
			fi
		done
		echo ;
		echo > $LOGDEV;
	done
}

function compresslinks ()
{
	while read crosspost; do
		tocompress=`echo $crosspost | cut -d ' ' -f 1`
		compressed=$tocompress.gz

		echo "gzip -9 -c $tocompress > $compressed" > $LOGDEV;
		gzip -9 -c $tocompress > $compressed
		chmod +rw $compressed
		chown news.news $compressed

		if [ $testonly != 0 ]; then
			echo "TESTING $crosspost" > $LOGDEV
			return 0;
		else

		for tolink in $crosspost; do
			if [ ! -r $tolink.gz ]; then
				echo "ln $compressed $tolink.gz; " > $LOGDEV;
				ln $compressed $tolink.gz;
			fi
			if [ -r $tolink -a -r $tolink.gz ]; then
				echo "rm -f $tolink" > $LOGDEV;
				rm -f $tolink;
			else
				echo "Problem with $tolink" > $LOGDEV;
			fi
		done

		fi
	done
}

# *******************************  M A I N  ********************************

findopt="";
links="";

while getopts "vtl:s:" opt; do 
	case $opt in
	   l)	links="-links $OPTARG" ;;
	   s)	size="-size $OPTARG" ;;
	   t)	testonly=1;
			LOGDEV="`tty`" ;;
	   v)	LOGDEV="`tty`" ;;
	   ?)   usage; exit ;;
	esac
done

if [ "$OPTIND" -gt 1 ] ; then shift $[$OPTIND - 1]; fi
if [ $# = 0 ]; then usage; exit; fi

findopt="$links $size";

for newsdir in $*; do
	newsdir=`echo $newsdir | tr '.' '/'`	# accept syntax newsgrp.newsgrp
	find $NEWSSPOOL/$newsdir -type f $findopt \! -name "*.gz" | findlinks | compresslinks
done

echo > $LOGDEV;
