#include <exec/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

FILE	*in = NULL;
FILE	*out = NULL;
char	search_string[256];
char	nl_name[64];
char	log_file[64];
char	log_flag = 0x00;

int	zone = 1;
int	net = 1;
int	node = 1;

int	zone_low;
int	zone_high;
int	net_low;
int	net_high;
int	node_low;
int	node_high;


int	i,j;
char	instring[256];
char	*match;
char	temp[128];
int	count = 0;
long 	ncount = 0;

char	*cp;
char	*cpz;
char	*cpn;
char	*cp_bbs_name;
char	*cp_bbs_city;
char	*cp_bbs_sysop;
char	*cp_bbs_phone;
char	*cp_bbs_baud;
char	*cp_bbs_flags;

void	C_ABK();
void	done();

void main(argc,argv)
int argc;
char *argv[];
{
	signal (SIGINT,&C_ABK);
	zone_low = 0;
	zone_high = 32767;
	net_low = 0;
	net_high = 32767;
	node_low = 0;
	node_high = 32767;
	if (argc < 3)
	{
		printf ("\nSearch Node List (pd) 1992 by Doug McLean");
		printf ("\nUSAGE: %s NodeList String <optional parameters>",argv[0]);
		printf ("\n       Ofilename   : Log file name");
		printf ("\n       Lzone:net/0 : Lowest node number to search");
		printf ("\n       Hzone:net/0 : Highest node number to search\n");
		exit (0);
	}
	sprintf (search_string,"%s",argv[2]);
	sprintf (nl_name,"%s",argv[1]);
	for (j = 3; j < argc; j++)
	{
		switch (argv[j][0])
		{
			case 'o':
			case 'O':
				sprintf (log_file,"%s",&argv[j][1]);
				log_flag = 0x01;
				break;
			case 'l':
			case 'L':
				for (i = 0; i < 127;i++)temp[i]=0;
				sprintf (temp,"%s",&argv[j][1]);
				
				cpz = strchr (temp,':');
				cpn = strchr (temp,'/');
				if ((cpz == 0)||(cpn == 0))
				{
					printf ("\nIllegal node format: %s\n",temp);
					exit (0);
				}
				*cpz = 0; cpz++;
				*cpn = 0; cpn++;
				zone_low = atoi(temp);
				net_low = atoi(cpz);
				break;
			case 'h':
			case 'H':
				for (i = 0; i < 127;i++)temp[i]=0;
				sprintf (temp,"%s",&argv[j][1]);
				cpz = strchr (temp,':');
				cpn = strchr (temp,'/');
				if ((cpz == 0)||(cpn == 0))
				{
					printf ("\nIllegal node format: %s\n",temp);
					exit (0);
				}
				*cpz = 0; cpz++;
				*cpn = 0; cpn++;
				zone_high = atoi(temp);
				net_high = atoi(cpz);
				break;
			default:
				printf ("\nUnrecognized option: %s\n",argv[j]);
				exit (0);
		}
	}
	strupr (search_string);
	if ((in = fopen(nl_name,"ra")) == NULL)
	{
		printf ("\nCan't open %s for input!\n",nl_name);
		exit (0);
	}
	
	if (log_flag)
	{
		if ((out = fopen(log_file,"w")) == NULL)
		{
			fclose(in);
			printf ("\nCannot open %s for output!\n",log_file);
			exit (0);
		}
		fprintf (out,"\nSearching %s for %s",nl_name,search_string);
		fprintf (out,"\nSearch from %d:%d/%d to %d:%d/%d\n\n",zone_low,net_low,node_low,zone_high,net_high,node_high);
	}
	
	printf ("\nSearching %s for %s",nl_name,search_string);
	printf ("\nSearch from %d:%d/%d to %d:%d/%d",zone_low,net_low,node_low,zone_high,net_high,node_high);
	if (log_flag) printf ("\nLog file: %s",log_file);
	else printf ("\nLog file: None (screen output only)");
	printf ("\n<ctrl-c> to exit\n");
	while (fgets(instring,256,in) != NULL)
	{
		strupr (instring);
		if (instring[0] == ';') continue;
		ncount++;
		if (stcpma (instring,"ZONE"))
		{
			zone = atoi(&instring[5]);
			net = 1;
			node = 1;
			printf ("\nZone %d\n",zone);
			
		}
		else if (stcpma (instring,"REGION"))
		{
			net = atoi(&instring[7]);
			node = 1;
			printf ("\nZone %d Region %d\n",zone,net);
		}
		else if (stcpma (instring,"HOST"))
		{
			net = atoi(&instring[5]);
			node = 1;
			printf ("Net %-10d",net);
			printf ("\033[%dD",14);
		}
		else if (stcpma (instring,"HUB"))
		{
			node = atoi(&instring[4]);
		}
		else if (instring[0] == ',')
		{
			node = atoi(&instring[1]);
		}
		if (zone < zone_low) continue;
		if (zone > zone_high) done();
		if (net < net_low) continue;
		if ((net > net_high) && (zone >= zone_high))done();
		if (stcpm(instring,search_string,&match))
		{
			count++;
			cp = strchr(instring,',');
			cp++;
			cp = strchr(cp,',');
			cp++;
			/*** cp now -> 1st char BBS name ***/
			
			cp_bbs_name = cp;
			cp_bbs_city = strchr(cp_bbs_name,',') + 1;
			cp_bbs_sysop = strchr(cp_bbs_city,',') + 1;
			cp_bbs_phone = strchr(cp_bbs_sysop,',') + 1;
			cp_bbs_baud = strchr(cp_bbs_phone,',') + 1;
			cp_bbs_flags = strchr(cp_bbs_baud,',') + 1;
			cp = cp_bbs_flags;
			while (*cp != 0x00)
			{
				if (*cp == ',')*cp = ' '; cp++;
			}
			j = strlen(instring);
			for (i = 0; i < j; i++)
			{
				if (instring[i] == ',') instring[i]=0x00;
				if (instring[i] == '_') instring[i]=' ';
			}
			printf ("\n\r>>>>> Found %s at %d:%d/%d <<<<<",search_string,zone,net,node);
			printf ("\n\r      Name     : %s",cp_bbs_name);
			printf ("\n\r      Location : %s",cp_bbs_city);
			printf ("\n\r      Sysop    : %s",cp_bbs_sysop);
			printf ("\n\r      Phone    : %s",cp_bbs_phone);
			printf ("\n\r      Max Baud : %s",cp_bbs_baud);
			printf ("\n\r      Flags    : %s\n",cp_bbs_flags);
			if (log_flag)
			{
				fprintf (out,"\n>>>>> Found %s at %d:%d/%d <<<<<",search_string,zone,net,node);
				fprintf (out,"\n      Name     : %s",cp_bbs_name);
				fprintf (out,"\n      Location : %s",cp_bbs_city);
				fprintf (out,"\n      Sysop    : %s",cp_bbs_sysop);
				fprintf (out,"\n      Phone    : %s",cp_bbs_phone);
				fprintf (out,"\n      Max Baud : %s",cp_bbs_baud);
				fprintf (out,"\n      Flags    : %s\n",cp_bbs_flags);
			}
		}
	}
	fclose(in);
	if (out) fclose(out);
	printf ("\nEnd of nodelist reached...");
	printf ("\nSearched from %d:%d/%d to %d:%d/%d",zone_low,net_low,node_low,zone_high,net_high,node_high);
	printf ("\nFound %s %d times.",search_string,count);
	printf ("\nSearched %d entries\n",ncount);
	exit (0);
}


void C_ABK()
{
        signal (SIGINT,SIG_IGN);
	if (in) fclose (in);
	if (out) fclose (out);
	printf ("\n<CTRL-C> break!");
	printf ("\nFound %s %d times\n",search_string,count);
	printf ("\nLast node searched: %d:%d/%d",zone,net,node);
	printf ("\nRead %d entries\n",ncount);
	exit (0);
}

void done()
{
        signal (SIGINT,SIG_IGN);
	if (in) fclose (in);
	if (out) fclose (out);
	printf ("\nSearch completed");
	printf ("\nSearched from %d:%d/%d to %d:%d/%d",zone_low,net_low,node_low,zone_high,net_high,node_high);
	printf ("\nFound %s %d times",search_string,count);
	printf ("\nRead %d entries\n",ncount);
	exit (0);
}

