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

long inputlines = 0, outputlines = 0;
long totallines = 0;
char buffer[256];
FILE *fp;

#if !defined(_lint)
static char rcsid[] OPTIONAL = "$Id: wpfix.c,v 1.7 1997/07/31 00:44:20 root Exp $";
#endif

struct entry	{
	char  *call;
};
struct entry *e;


int
finddup (filenm, index)
char *filenm;
int index;
{
int retval = 0;
long k;
char *cp;

	if (buffer[0] < 'A')
		return 1;
	putchar ('\r');
	clreol();
	printf ("%s processing #%05ld/%05ld: '%s'....", filenm, inputlines, outputlines, buffer);
	for (k = index + 1; k < totallines; k++)	{
		if (!strncmp (buffer, e[k].call, 6))	{
			retval = 1;
			break;
		}
	}
	return (retval);
}


void
process (filenm, maxlen)
char *filenm;
int maxlen;
{
register FILE *out, *fpin;
register char *sptr = buffer;
register char *cp, *cp2;
int llen;
long k;

	inputlines = outputlines = 0;
	if ((fpin = fp = fopen (filenm, "r")) == NULL)
		return;
	if ((out = fopen ("spool/temp", "w")) == NULL)	{
		fclose (fp);
		return;
	}
	while (!feof(fpin))	{
		fgets (sptr, 256, fpin);
		if (feof(fpin))
			break;
		totallines++;
	}
	rewind(fpin);
	printf ("File '%s' originally contains %ld entries\n", filenm, totallines);
	e = malloc (totallines * sizeof (char *));
	if (e == 0)	{
		printf ("Error allocating memory\n");
		exit (0);
	}
	for (k = 0; k < totallines; k++)	{
		fgets (sptr, 256, fpin);
		e[k].call = malloc(6);
		strncpy (e[k].call, sptr, 6);
	}
	rewind(fpin);
		
	cp2 = strrchr (filenm, '/');
	cp2++;
	k = 0;
	while (!feof(fpin))	{
		fgets (sptr, 256, fpin);
		if (feof(fpin))
			break;
		inputlines++;
		llen = strlen(sptr);
		if ((llen > maxlen+2) || (llen < maxlen))	{
			k++;
			continue;
		}
		cp = strchr (sptr, ' ');
		*cp++ = 0;
		if (!finddup (cp2, k))	{
			while (*cp == ' ')
				cp++;
			cp[strlen(cp) - 1] = 0;
			fprintf(out,"%-*s %-14s\n", maxlen - 15, sptr, cp);
			outputlines++;
		}
		k++;
	}
	fclose (fp);
	fclose (out);
	for (k = 0; k < totallines; k++)
		free (e[k].call);
	free (e);
	unlink (filenm);
	rename ("spool/temp", filenm);
	printf ("\n\nFile '%s' complete!\nOriginal number of lines: %ld\nNew number of lines: %ld\nNumber of duplicate lines: %ld\n\n", filenm, inputlines, outputlines, inputlines - outputlines);
}


void
main ()
{
	printf ("Quick hack to fix white pages duplicates\nStand by, this will take a while...\n\n");
/*	process ("spool/wpages", 28); */
	process ("spool/wpagebbs", 47);
}


