#include <fido.h>
#include <\mailer\fidomem.h>
#include <ascii.h>

/*
	One time Fido initialization.

It is ASSUMED that all globals are inited to all zeros at program
load time!

*/

extern char _spr_sepchar;			/* in printf(), in SPR, in LIB.LIB */

fidoini(argc,argv)
int argc;
char **argv;
{
char *p,buff[SS],swbuff[SS];
int i,f,n;
unsigned t;
char **cp;

	conflag= 99;				/* console on for now */

	allmem();				/* get all available */
	if (! fastfile(6)) {			/* init the buffered file system */
		clprintf(0,"! NOT ENOUGH MEMORY\377");
		exit(1);
	}
	memsize= sizmem();			/* how much there is, */
	mem= getmem(memsize);			/* get it all */
	t= 0;					/* tally of space needed */

/* Load the language files first, set the language tables so we can 
issue error messages! Note that the loaded tables contains a table of offsets,
from the start of the table to the text strings; we have to convert the
built-in tables to that format. (The compiler generates them as the usual
array of string pointers.) */

	texttbl[0]= (char *) &fmtable;		/* link to fixed messages */
	texttbl[2]= (char *) &lmtable;		/* link to log messages */
	f= open("FIDO.SLN",0);			/* default sysop file */
	if (f == -1) {
		cprintf(0,"! NO FILE: \"FIDO.SLN\"\377");
		exit(1);
	}

/* Load the language file and link it in. Check the CRC of the file. */

	read(f,mem,sizeof(struct _lang_hdr));		/* read the header, */
	if (((struct _lang_hdr *)mem)-> version != LANGVER) {/* check version */
		clprintf(0,"! WRONG VERSION FILE: \"FIDO.SLN\"\377");
		exit(1);
	}
	clrcrc();
	for (n= 0; n < sizeof(struct _lang_hdr); n++) 	/* CRC the header portion, */
		updcrc(mem[n]);

	n= read(f,mem,15000);				/* hell if I know */
	close(f);

	for (i= 0; i < n; i++) updcrc(mem[i]);		/* CRC the data, */
	if (chkcrc()) {
		cprintf(0,"! DAMAGED FILE: FIDO.SLN\377"); /* "bad language file" */
		exit(1);
	}
	texttbl[1]= mem;				/* link it in, */
	for (cp= (char **) mem; *cp != -1; ++cp) {	/* convert offsets */
		*cp += (unsigned) mem;			/* to pointers */
	}
	n -= 4;						/* skip -1 marker, CRC bytes */
	mem += n; t += n;				/* tally memory used */

/* OK, so now we have the sysop language linked in, we can load FIDO.SYS
and figure out what other things we need. */

	if (getsys() == -1) {			/* load system stuff */
		cprintf(SM+11);			/* "missing FIDO.SYS" */
		cprintf(SM+12);
		exit(1);
	}
	if ((fido.fido_version != FIDOVER) || (fido.file_version != FILEVER)) {
		cprintf(SM+13);
		cprintf(SM+14);			/* "wrong version" */
		cprintf(SM+15,fido.fido_version,fido.file_version + 96);
		cprintf(SM+16,FIDOVER,FILEVER+96);
		cprintf(SM+17);
		exit(1);
	}

	iodev= -1;				/* no serial port selected */
	mdmtype= -1;				/* no modem type set */

/* Set certain globals from the FIDO.SYS file, now, and only once. */

	iodev= fido.iodev;			/* default I/O port to use */
	_spr_sepchar= fido.sep_char;		/* printf() 1000's seperator */

	zmttype= fido.zmttype;			/* Zmodem transmit behaviour */
	zmrtype= fido.zmrtype;			/* Zmodem receive behaviour */
	zmblkst= fido.zmtstart;			/* Zmodem starting block size */
	zmblkmax= fido.zmblkmax;		/* Zmodem maximum blocksize */

	id.zone= fido.zone;			/* node ID */
	id.net= fido.net;
	id.number= fido.number;
	id.point= fido.point;

	n= 0;					/* memory used so far */

/* ZMODEM buffer. */

	zmbuff= mem; mem += zmblkmax; t += zmblkmax; /* ZMODEM buffer */

/* Now check that the memory we took was actually available! What's left over
is the "mem buffer". */

	if (t > memsize) {
		cprintf(SM+19);			/* "not enough memory" */
		exit(1);
	}
	memsize -= t;				/* memory buffers' actual size */

/* Now memory is set, system is loaded, it's almost like a normal 
program start. */

	newdelim(" \t;,\n\r");			/* arg delimiter list, */

	*filelist= NUL;				/* file(s) to send list */
	if (--argc < 3) {
oops:		conflag= 1;			/* on for error report */
		printf(0,"MAIL (15 Oct 90) Copyright Fido Software 1990\377");
		printf(0,"Licensed to Creative Marketing Services\377\377");
/*		cprintf(0," - Usage:  MAIL z:n/f phone# files ...\r\n");
*/		exit(1);
	}
	++argv;					/* skip program name */
	cpy_node(&node,&id);			/* default to us */
	set_nn(*argv,&node);			/* set dest. node address */
	if (same_node(&node,&id)) goto oops;	/* must have changed */

	++argv; --argc;
	cpyarg(phone,*argv);			/* the phone number */

	while (--argc) {
		++argv;
		cpyarg(buff,*argv);
		if (*buff) {			/* if filename present */
			if (*filelist) strcat(filelist," ");
			strcat(filelist,buff);	/* add this one to it */
		}
		strip_switch(swbuff,*argv);	/* the switches, */
		p= swbuff;
		while (*p) {
			switch (*p) {
				case '1': iodev= 0; goto comp;
				case '2': iodev= 1; 
comp:;					cprintf(SM+20,iodev + 1); 
					break;

				case 'C': conflag= 1; break;

				default: printf(SM+26,*p); break;
			}
			++p;
		}
	}
	if (! *filelist) goto oops;		/* must be at least one file */

	if (conflag != 1) conflag= 0;		/* off unless explicitly ON */
	cprintf(SM+18,fido.name);		/* impress the sysop */
}
