/*--------------------------------------------------------------------- */
/* getren.c								*/
/*	Read each input file (a news article) and output a command to	*/
/*	rename it to the VnnIxxx from the Subject: line.		*/
/*									*/
/* History:								*/
/*	6 Jan 1991	v1.0						*/
/*									*/
/* Author:								*/
/*	Eyal Lebedinsky							*/
/*	Canberra, AUSTRALIA						*/
/*--------------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define	VBUF

typedef unsigned char	uchar;

static uchar	line[4096];

main (argc, argv)
int	argc;
uchar	*argv[];
{
	FILE	*fin = NULL;
	uchar	*q;
	int	nopened = 0,
		nrenamed = 0;
#ifdef	VBUF
	uchar	*p;
#endif

#ifdef	VBUF
#define	BSIZE	1024	/* we do not expect to read much */
	if ((p = malloc (BSIZE)) == NULL) {
		fprintf (stderr, "Out of memory\n");
		exit (1);
	}
#endif

	for (; argc-- > 1;) {
		fin = fopen (*++argv, "rt");
		if (fin == NULL) {
			fprintf (stderr, "could not open %s\n", *argv);
			continue;
		}
#ifdef	VBUF
		if (setvbuf (fin, p, _IOFBF, BSIZE)) {
			fprintf (stderr, "setvbuf failed!\n");
			exit (1);
		}
#endif
		++nopened;
		while (fgets (line, sizeof (line), fin) != NULL) {
		    if (!memcmp (line, "Subject: ", 9)) {
			q = line + 9;
			if ((q[0] == 'v' || q[0] == 'V') &&
			    isdigit (q[1]) &&
			    isdigit (q[2]) &&
			    (q[3] == 'i' || q[3] == 'I') &&
			    (q[4] == 'n' || q[4] == 'N' || isdigit (q[4])) &&
			    (q[5] == 'f' || q[5] == 'F' || isdigit (q[5])) &&
			    isdigit (q[6]) &&
			    (q[7] == ':' || isdigit (q[7]) && q[8] == ':')) {
				printf ("ren %s ", *argv);
				for (; isalnum (*q); ++q)
					 putchar (*q);
				putchar ('\n');
				++nrenamed;
			}
			break;
		    }
		}
		fclose (fin);
	}
	fprintf (stderr, "%u files found, %u renamed\n",
		nopened, nrenamed);
	exit (0);
}
