/*********************************************************
 * Gotcha library example code.
 * (c)1995 Thomas Bickel. All rights reserved.
 *
 * Demonstrates the usage of the DZN functions
 * NB: This was done as a fast example, just to show how
 * it works!
 *********************************************************/
#include <stdio.h>
#include <stdlib.h>

#include <exec/lists.h>
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>

#include <clib/gotchalib_protos.h>
#include <pragmas/gotchalib_pragmas.h>
#include <libraries/gotchalib.h>

struct Library *GotchaLibBase;
struct gl_context *context;

UWORD  *nets;
UWORD  *zones;
UBYTE  **dom;

ULONG dcnt, d,z,n;
LONG zx,nx, zx2,nx2;

UBYTE adrstr[64];
struct gl_address adr;
struct gl_pattern pat;
struct gl_nodeinfo ninfo;
/*************************************/
ULONG read_val(UBYTE *s, ULONG max)
{
	BOOL done=FALSE;
	ULONG a;

	while (!done) {

		printf(s);
		fscanf(stdin,"%ld",&a);

		if (a && (a <= max)) done = TRUE;
		else puts("Illegal number");
	}
return(a);
}
/***********************************************************************/
int main(long argc, char *argv[])
{
	if (GotchaLibBase = OpenLibrary("gotcha.library",1)) {
		if (context = GL_OpenNL("nodelist:")) {
			if (GL_GetDZNLists(context,&dom,&zones,&nets)) {

				dcnt = 0;
				while (dom[dcnt]) {
					printf("%2ld %s\n",dcnt+1,dom[dcnt]);
					dcnt++;
				}
				d = read_val("Show zones for domain #",dcnt) - 1;

                printf("Zones in domain %s: ",dom[d]);

				GL_MoveIndexDomain(d,&zx,&nx,&zones,&nets);
				zx2 = zx; z = 0;
				while (zones[zx] != 65535) {
					printf("%ld ",zones[zx]);
					z = ( zones[zx] > z ? zones[zx] : z );
					zx++;
				}
				zx = read_val("\nSelect a zone #",z);

				z=0;
				while (zones[zx2] != 65535) {
					if (zx == zones[zx2]) break;
					zx2++; z++;
				}
				if (zones[zx2] == 65535) { puts("No such zone"); exit(10); }

				printf("Nets in zone %ld: ",zones[zx2]);
				GL_MoveIndexDomain(d,&zx,&nx,&zones,&nets);
				GL_MoveIndexZone(z,&nx,&nets);

				n = 0; nx2 = nx;				/* show nets */
				while (nets[nx] != 65535) {
					printf("%ld ",nets[nx]);
					n = (nets[nx] > n ? nets[nx] : n);
				nx++;
				}
                nx = read_val("\nSelect a net #",n);

				n = nx2;						/* get net# index */
				while (nets[n] != 65535) {
					if (nx == nets[n]) break;
					n++;
				}
				if (nets[n] == 65535) { puts("No such net"); exit(10); }

				printf("Nodes in %s, zone %ld, net %ld:\n",dom[d],zones[zx2],nets[n]);
				sprintf(adrstr,"%d:%d/?.?@%s",zones[zx2],nets[n],dom[d]);
				GL_XtractInfos(&adr,adrstr);

				if (GL_FindNodeFirst(&adr,context,&ninfo,&pat)) {
					do {
						if (! ((!ninfo.point) && (!*ninfo.location)) ) {
							printf("%d:%d/%d.%d@%s\t%s / %s / %s\n",
							ninfo.zone,ninfo.net,ninfo.node,ninfo.point,ninfo.domain,
							ninfo.name,ninfo.location,ninfo.sysop);
    					}
					} while (GL_FindNodeNext(&adr,context,&ninfo,&pat));
				}

				GL_FreeDZNLists(context,&dom,&zones,&nets);
			} else
				puts("Out of memory!");

			GL_CloseNL(context);
		} else
			puts("Could not open the nodlist!");

		CloseLibrary(GotchaLibBase);
	} else
		puts("Could not open 'gotcha.library'!");
}
