/**************************************************************************
 * NetEchoList.c - List all configured Fidonet areas in CNet
 **************************************************************************/

#include "NetEchoList.h"

BOOL CheckConfigured( char *ZeroPath );
void GetOut ( void );


short						FidoType=-1;
char						EchoFile[] = "cnet:bbsechos";
char						GenBuff[256];
struct	EchoList		Echo1;
struct	MainPort		*myp=NULL;
BOOL						ControlRunning=FALSE;

LONG		Listed=0;

UBYTE *vers="$VER:v1.0 NetEchoList (03-11-95)";

void main(int argc, char *argv[])
{
	FILE *fp;
	short xxx=0;

	atexit(GetOut);

	if( myp = (struct MainPort *)FindPort("cnetport") )
		{
		ControlRunning=TRUE;

		printf("\n[0;4mAvailable networks:[0m\n\n");
		for(xxx=0;((xxx<myp->gc.nfido) && strlen(myp->gc.fido[xxx].Name));xxx++)
			{
			printf("	%d. %s\n", xxx+1, myp->gc.fido[xxx].Name);
			}
		if(xxx > 0)
			{
			printf("\nChoose a network [RETURN/ENTER = ALL]: ");
			fflush(stdout);
			FidoType=-1;
			if(gets(GenBuff))
				{
				FidoType = atoi(GenBuff)-1;
				printf("%c", 12);	/* clear screen */
				}
			}
		}
	else
		{
		printf("\n[0;1mNote: Base0/Net-type listing disabled while CNet/Control not loaded[0m\n");
		}

	if(fp=fopen(EchoFile, "r"))
		{
		if(FidoType == -1)
			{
			printf("\n[0;4mConfigured echos           Fido Type                                  In Base0?[0m\n");
			}
		else
			{
			printf("\n[0;4mConfigured echos                                                      In Base0?[0m\n");
			}
		
		while(fread((char *)&Echo1.EchoType, sizeof(struct EchoType), 1, fp))
			{
			Echo1.Configured=FALSE;

			if(ControlRunning)
				{
				Echo1.Configured = CheckConfigured(Echo1.EchoType.Title);

				if(FidoType == -1)
					{
					Listed++;
					printf("%-24.24s [%10.10s ] %-33.33s %s\n", Echo1.EchoType.Title, myp->gc.fido[ Echo1.EchoType.domain ].Name, Echo1.EchoType.Info, (Echo1.Configured) ? "Yes":"No");
					}
				else
					{
					/* Does current echo belong the requested net? */
					if(Echo1.EchoType.domain == FidoType)
						{
						Listed++;
						printf("%-24.24s %-48.48s %s\n", Echo1.EchoType.Title, Echo1.EchoType.Info, (Echo1.Configured) ? "Yes":"No");
						}
					}
				}
			else
				{
				Listed++;
				printf("%-24.24s %-48.48s %s\n", Echo1.EchoType.Title, Echo1.EchoType.Info, "Unknown");
				}
			}
		fclose(fp);
		}

	if(!Listed)
		{
		printf("[3m[No match/empty][0m\n");
		}
	else
		{
		printf("[3mListed %ld echos[0m\n", Listed);
		}
	exit(0);
}


BOOL CheckConfigured( char *SubDirName )
{
	struct NewSubboardType *sub1;
	LONG zzz=0;

	for(zzz=0;zzz<myp->ns;zzz++)
		{

//printf("comparing subboard %s with %s\n", myp->Subboard[zzz].SubDirName, SubDirName);

		if(!stricmp(myp->Subboard[zzz].SubDirName, SubDirName))
			{
			return TRUE;
			}
		}
	return FALSE;
}


void GetOut ( void )
{
	printf("[34mNetEchoList %5.5s copyright © 1995-1996 MetalSoft.[0m\n", vers+5);
}
