/*	:ts=4
 *	fixnews -- reads uncompressed news batch on stdin and generates a new
 *		one in T:All with corrected "#! rnews" headers
 *
 *	$Log:	fixnews.c,v $
 * Revision 1.3  90/12/25  15:52:42  crash
 * as of 12-15-90 patches
 * 
 * Revision 1.2  90/05/28  13:58:11  crash
 * added RCS headers
 * 
 */

#ifndef lint
static char RCSid[] =
	"$Id: fixnews.c,v 1.3 90/12/25 15:52:42 crash Exp Locker: crash $";
#endif /* lint */

#define min(a,b)	((a) < (b) ? (a) : (b))
#define max(a,b)	((a) > (b) ? (a) : (b))

#include <stdio.h>
#include <string.h>
#include <stat.h>

char buff[BUFSIZ];
char fname[32];
struct stat statbuf;

main(int argc, char **argv)
{
	FILE *fout = 0, *fin;
	register long size = 0, len;
	register char *iobuf = 0;
	register int entry = 0;

	while (fgets(buff, sizeof(buff), stdin)) {
		if (strncmp(buff, "#! rnews ", 9)) {
			size -= fwrite(buff, 1, strlen(buff), fout);
		} else {
			if (fout) fclose(fout);
			if (size) printf(" -- was out of sync");
			sprintf(fname, "T:%03d", ++entry);
			fout = fopen(fname, "w");
			size = atol(buff+9);
			printf("\nFile %d: #! rnews %ld", entry, size);
		}
	}
	if (fout) fclose(fout);
	if (size) printf(" -- was out of sync\n");

	printf("\nStarting recollection process...\n\n");
	fout = fopen("T:All", "w");
	size = 0;
	while (entry > 0) {
		sprintf(fname, "T:%03d", entry--);
		stat(fname, &statbuf);
		fprintf(fout, "#! rnews %ld\n", statbuf.st_size);
		fin = fopen(fname, "r");
		if (statbuf.st_size > size) {
			if (iobuf) free(iobuf);
			iobuf = (char *) malloc(size = statbuf.st_size);
		}
		len = fread(iobuf, 1, statbuf.st_size, fin);
		fwrite(iobuf, len, 1, fout);
		fclose( fin );
		remove( fname );
	}
	fclose(fout);
	return( 0 );
}
