/* New routines added by TNOS/KO4KS version of NOS */

#include <ctype.h>
#include <time.h>
#include "config.h"
#include "dirutil.h"
#include "session.h"

#define ASCII_TYPE 0
int STATLINE = 0;
char *NextCmdMsg;
short NextCmd;
unsigned char SCREENwidth = 80, SCREENlength = 25;
char vgaDesired = 0;
extern char statversion[];
extern int BbsUsers, FtpUsers, Tutored;
extern int main_exit;			/* from main program (flag) */

void log(int s,char *fmt, ...);
int tprintf (char *fmt, ...);
int socklen (int s,int rtx);
int setbool (int *var,char *label,int argc,char *argv[]);
int CountConfUsers (void);
void rip (char *);

extern unsigned Nsessions;		/* Maximum number of sessions */
extern struct session *Sessions;	/* Session descriptors themselves */
extern struct session *Current;
extern struct session *Command;
extern struct session *Lastcurr;	/* Last non-command session */
extern struct session *Trace;
struct session *ScreenOwner;		/* Session currently displayed */

long sendfile(FILE *fp,int s,int mode,int hash);
int chksession (struct session *sp);
int ssenabled (void);
void setscreens (int back, int fore, int clr);
int getcolor (char *str);
int docolor (int argc, char *argv[], void *p);
void statlog (char *buf);
void displayStatLine (int offset, int phase);
void statLineToggle (void);
int dostatline (int argc, char *argv[], void *p);
int DisplayFile (char *fname,int socket);


int SYSback = 40, SYSfore = 37;
static char nope[] = "Color '%s' unknown!\nAvailable colors: black, red, green, cyan, blue, magenta, and white\n";
#if 0
static char setem[] = "\033[%-2d;%-2dm";
#endif
static int backs[8] = {0, 4, 2, 0, 1, 5, 3, 7};
static int fores[8] = {0, 4, 2, 0, 1, 5, 3, 7};

int DisplayFile (char *fname,int socket)
{
FILE *fp;
int size = 0;

	if((fp = fopen(fname,READ_TEXT)) != (FILE *)0) {
		sendfile(fp,socket,ASCII_TYPE,0);
		size = ftell (fp);
		fclose(fp);
		}
	return (size);
}


void
setscreens (int back, int fore, int clr)
{
/* do nothing */
}


int
getcolor (char *str)
{
char *ptr;
	for (ptr = str; *ptr; ptr++)
		*ptr = tolower (*ptr);
	switch (*str)	{
		case 'r':	return 31;
		case 'g':	return 32;
		case 'm':	return 35;
		case 'c':	return 36;
		case 'w':	return 37;
		case 'b':	if (str[1] != 'l')
					return 0;
				switch (str[2])	{
					case 'a':	return 30;
					case 'u':	return 34;
				}
		default:	return 0;
		}
}

/* Set terminal colors */
int
docolor (int argc, char *argv[], void *p)
{
int temp;
int back, fore;
struct session *sp;

	temp = getcolor (argv[1]);
	if (!temp)
		tprintf (nope, argv[1]);
	else	{
		back = temp + 10;
		temp = getcolor (argv[2]);
		if (!temp)
			tprintf (nope, argv[2]);
		else	{
			fore = temp;
			if (argc > 3)	{
				temp = atoi(argv[3]);
				if (temp)	{
					sp = &Sessions[temp];
					if (chksession (sp))
						sp->screen->attr = (((backs[back - 40] << 4) + (fores[fore - 30])) | 0x80);
				}
			} else	{
				SYSback = back;
				SYSfore = fore;
				setscreens (0, 0, 1);
			}
		}
	}
	return 0;
}

void
statlog (char *buf)
{
	NextCmd = 0;
	free (NextCmdMsg);
	if (Command && Command->screen->statline)	{ /* Command status line is on */
		NextCmdMsg = strdup (buf);
		rip (NextCmdMsg);
		NextCmd = 120;
	}
}


void
_strtime (str)
char *str;
{
char *cp;
int k;
time_t tm;

	time (&tm);
	cp = ctime (&tm);
	cp += 11;
	for (k = 0; k < 8; k++)
		*str++ = *cp++;
	*str = 0;
}

void
screendaemon (i,v1,v2)
int i;		/* Args not used */
char *v1;
void *v2;
{
	for(;;){
		mspause(500L);	/* Run displayStatLine every 1/2 second */
		displayStatLine (0, 1);
		mspause(500L);	/* and run the rest every second */
		displayStatLine (0, 0);
#ifdef SCREENSAVER
		screensaver ();
#endif
	}
}

void displayStatLine (int offset, int phase)
{
struct session *sp, *tochk;
int stop, r;
unsigned iscmd = 0;
char timebf[9];
short beepit = 0;
static short lastbeep = 0;
int origx, origy, k;
char mybuf[100];

	if (main_exit)
		return;
	if (!ScreenOwner->screen->statline && offset != 1)
		return;
#ifdef SCREENSAVER
	if (ssenabled ())
		return;
#endif
	origx = wherex();
	origy = wherey();
	if (ScreenOwner == Command)
		iscmd = 1;
	window (1,1,SCREENwidth,/*(iscmd) ?*/ 3/* : 2 */);
	tochk = Current;
	if ((Current == Trace) || (Current == Command))
		tochk = Lastcurr;
	highvideo();
	if (offset)
		clrscr ();
	if (offset != 1)		{
		sprintf (mybuf, statversion, Nsessions);
		cputs (mybuf);
		stop = 20;
		if (Nsessions < stop)
			stop = Nsessions;
		for(sp=Sessions; sp < &Sessions[stop];sp++)	{
		        if(sp->type == FREE || sp->type == COMMAND || sp->type == TRACESESSION)	{
				cputs ("   ");
				continue;
				}
			r = socklen(sp->output,1);
			if (r && !beepit)
				beepit = 1;
			sprintf (mybuf, "%2d ", sp->index);
			cputs (mybuf);
/*			normvideo ();	*/
			}
		clreol ();	
/*		gotoxy (75, 1);		*/
		for (k = 45 - strlen(statversion); k; k--)
			cputs (" ");
/*		cputs("                       ");	*/
		_strtime (timebf);
		timebf[5] = 0;		/* clip off the seconds */
		if (!phase)
			timebf[2] = ' ';
/*		textattr (attr);	*/
		cputs (timebf);
		cputs (" ");
		if (iscmd)	{
/*			gotoxy (2, 1);	*/
#ifdef CONVERS
			sprintf (mybuf, " BBS=%2d CONF=%2d FTP=%2d TUT=%2d  ", BbsUsers, CountConfUsers(), FtpUsers, Tutored);
#else
			sprintf (mybuf, " BBS=%2d FTP=%2d TUT=%2d           ", BbsUsers, FtpUsers, Tutored);
#endif
			cputs (mybuf);
/*			clreol ();	*/
			sprintf (mybuf, "- %-46.46s ", (NextCmd) ? NextCmdMsg : "");
			cputs (mybuf);
		}
		if (NextCmd)
			if (!--NextCmd)	{
				free (NextCmdMsg);
				NextCmdMsg = 0;
			}
		if (!lastbeep && beepit)
			cputs ("\007");
		lastbeep = beepit;
#if 0
 		window (1,2,SCREENwidth,SCREENlength);
 		window (1,2 + iscmd,SCREENwidth,SCREENlength - (ScreenOwner->split * 2) - iscmd - 1);
#else
 		window (1,2 + iscmd,SCREENwidth,SCREENlength - (ScreenOwner->split * 2));
#endif
		gotoxy (origx, origy + offset + (offset * iscmd));
		}
	else	{
#if 0
		window(1,1,SCREENwidth,SCREENlength);
#else
		window(1,1,SCREENwidth,SCREENlength - (ScreenOwner->split * 2));
#endif
		gotoxy (origx, origy + 1 + iscmd);
	}
	normvideo();
}

/* Set status line on/off */
int
dostatline (int argc, char *argv[], void *p)
{
int retval, itwas;

    itwas = STATLINE; 
    retval = setbool(&STATLINE,"Status line",argc,argv);
    if (STATLINE && !itwas)	{
 	Command->screen->statline = STATLINE;
	displayStatLine (-1, 1);
	}
    if (!STATLINE && itwas)	{
 	Command->screen->statline = STATLINE;
	displayStatLine (1, 1);
	}
    return (retval);
}

/* Status line toggle */
void
statLineToggle ()
{
    ScreenOwner->screen->statline ^= 1;
    if (ScreenOwner == Command)
	STATLINE = ScreenOwner->screen->statline;
    displayStatLine ((ScreenOwner->screen->statline) ? -1 : 1, 1);
}


