#include <string.h>
#include <exec/types.h>
#include <proto/exec.h>
#include "nl.h"

/*------------------------------------------------------------------------*/
// A "real" application should look it up in some config!

#define NODELISTDIR	"Nodelist:"
/// Accomodate max's taste for directories

/*-------------------------------------------------------------------------*/
extern void printf(char *,...);
extern void puts(char *);
extern int atol(char *);
extern void exit(int);

void main(int, char **);

/*------------------------------------------------------------------------*/
struct Library *NodelistBase;

/*-------------------------------------------------------------------------*/
void main(int argc, char **argv)
	{
	Extra xtra;
	Addr addr;
	char *cnf;

 	if((argc != 3) && (argc != 2))
  		{
 		printf("Usage: %s FQFA [config]\n", argv[0]);
  		exit(5);
  		}

	if(!(NodelistBase = OpenLibrary(TRAPLIST_NAME, TRAPLIST_VER)))
		{
		printf("Can't open library\n");
		exit(5);
		}

	/* First, open the Extra database */

	if((xtra = ExtraOpen(NODELISTDIR)))
		{
/// fixed D-3066; default for setconfig was wierd.

		NLParseAddr(&addr, argv[1], NULL);

		/* Example how to obtain data from the data base */

		ExtraLock(xtra);
		if(cnf = ExtraFind(xtra, &addr, ID_TCNF, NULL))
			{
			printf("Current config is \"%.80s\"\n", cnf);
			if (argc>2)
				{
				ExtraDel(xtra, cnf);
				if (!*argv[2])
					puts("Config removed");
				}
			}
		else
			{
			puts("No config set for this node");
			}
		ExtraUnlock(xtra);

		if(argc>2 && *argv[2])
			{
			if(ExtraPut(xtra, &addr, ID_TCNF, strlen(argv[2])+1, argv[2]))
				printf("Config set to \"%.80s\"\n", argv[2]);
/// fixed A-3005; long config line crashes setconfig.
			else
				puts("Config can't be changed");
			}

		ExtraClose(xtra);
		}
	else	
		puts("Extra won't open");

	CloseLibrary(NodelistBase);
	}

/*-------------------------------------------------------------------------*/
/* ID: 74.1@2011  Last Changed: 03 Apr 1991 17:10:39 by max */
