const char *_VERSION="\0$VER: Activity 1.01 (18.03.95)";

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <exec/semaphores.h>
#include <graphics/rastport.h>
#include <cnet/empty.h>
#include <cnet/cnet.h>

FILE	*fpin,*fpout;

char	filein[18] = "sysdata:log/calls",
		datum_org[15][7],
		graph[65],
		satz_in[300],
		satz_out[100],
		error[25] = "Error - please ask Qwert",
		signoff[8] = "SIGNOFF";

int		i,
		j,
		tag,
		next,
		carrier,
		logon,
		tage,
		node,
		nodes,
		blanks,
		std_start,
		min_start,
		std_stop,
		min_stop;
		color[9] = {35,33,34,36,32,35,33,34,36};

int		colnum=33,
		logs=1,
		mins_start,
		mins_stop,
		mins_log,
		mins_gesamt[15][10],
		calls[15][10],
		prozent[10];


struct	tm *tp;
long	t;


main(int argc, char **argv)
{
	PreMain(argc,argv);        /*** Let it here, dute! ***/

	if (argc != 3 || (argv[2][0] < '1' || argv[2][0] > '9'))
	{
		PutText("\nActivityGraph v1.0 by Qwert\n");
		PutText("Usage: Activity <number of ports (2-9)>\n\n");
	}
	else
	{


		nodes = atoi(argv[2]);
		tage = (z->user1.TermLength - 7) / (nodes+1);

		if (tage < 1)
		{
			PutText("\nSie sollten sich mal ein Terminalprogramm mit mehr Zeilen zulegen :-)\n");
		}
		else
		{

			if (tage > 14)
			{
				tage = 14;
			}

			ReadFile("pfiles:activity/activity.ansi",0);

			if (fpin=fopen(filein,"r"))
			{
				time(&t);

				for (tag=0;tag<tage;tag++)
				{
					tp = localtime(&t);
					strftime(datum_org[tag],7,"%d-%b",tp);
					t -= 86400;
					if (datum_org[tag][0]==48)
					{
						datum_org[tag][0]=32;
					}
				}

				while (fgets(satz_in,sizeof(satz_in),fpin))
				{
					if (next)
					{
						next = 0;
						tag = 0;

						while (tag < tage && strncmp(satz_in+3,datum_org[tag],6) != 0)
						{
							tag++;
						}

						if (tag < tage)
						{
							if (satz_in[32] != 76)
							{
								node = satz_in[24]-49;
								if (node >= 0 && node < nodes)
								{
									if (satz_in[10] == 32)
									{
										satz_in[10] = 48;
									}
									std_start = (satz_in[10]-48)*10 + satz_in[11]-48;
									min_start = (satz_in[13]-48)*10 + satz_in[14]-48;
									mins_start = std_start * 60 + min_start;
									if (satz_in[32] != 82)
									{
										calls[tag][node]++;
									}
								}
								else
								{
									node = -1;
								}
							}
							else
							{
								node = -1;
							}
						}
						else
						{
							node = -1;
						}
					}

					if (satz_in[0] == 46)
					{
						next = 1;
						if (logs == 79)
						{
							logs = 1;
							if (colnum == 38)
							{
								colnum=31;
							}
							else
							{
								colnum++;
							}
							sprintf(satz_out,"[78D[%dm",colnum);
							PutText(satz_out);
						}
						else
						{
							logs++;
							PutText(".");
						}
					}

					if (!next)
					{
						if (strncmp(satz_in+19,signoff,7) == 0 && node >= 0)
						{
							if (satz_in[10] == 32)
							{
								satz_in[10] = 48;
							}
							std_stop = (satz_in[10]-48)*10 + satz_in[11]-48;
							if (std_stop < std_start)
							{
								std_stop += 24;
							}
							min_stop = (satz_in[13]-48)*10 + satz_in[14]-48;
							mins_stop = std_stop * 60 + min_stop;
							mins_log = mins_stop - mins_start;
							mins_gesamt[tag][node] += mins_log;
						}
					}
				}
				fclose(fpin);

				PutText("\n[1A                                                                              [1A\n");
				for (tag=tage-1;tag>=0;tag--)
				{
					for (node=0;node<nodes;node++)
					{
						prozent[node] = (mins_gesamt[tag][node]+7) * 100 / 1440;
						mins_gesamt[tag][nodes] += mins_gesamt[tag][node];
						blanks = 56 * prozent[node] / 100;
						calls[tag][nodes] += calls[tag][node];
						if (blanks > 56)
						{
							strcpy(graph,error);
						}
						else
						{
							for (i=0;i<blanks;i++)
							{
								graph[i] = 32;
							}
							graph[i] = 0;
						}
						if (node == 0)
						{
							sprintf(satz_out,"[37m%s [32m1 %3d %5d %3d [42m%s[0m\n",datum_org[tag],calls[tag][0],mins_gesamt[tag][0],prozent[0],graph);
						}
						else
						{
							sprintf(satz_out,"[%dm[7C%d %3d %5d %3d [%dm%s[0m\n",color[node-1],node+1,calls[tag][node],mins_gesamt[tag][node],prozent[node],color[node-1]+10,graph);
						}
						PutText(satz_out);
					}
					prozent[nodes] = (mins_gesamt[tag][nodes]+7) * 100 / 1440 / nodes;
					blanks = 56 * prozent[nodes] / 100;
					if (blanks > 56)
					{
						strcpy(graph,error);
					}
					else
					{
						for (i=0;i<blanks;i++)
						{
							graph[i] = 32;
						}
						graph[i] = 0;
					}
					sprintf(satz_out,"[31m[7CT %3d %5d %3d [41m%s[0m\n",calls[tag][nodes],mins_gesamt[tag][nodes],prozent[nodes],graph);
					PutText(satz_out);
				}
			}
		}
	}
	PutText("CfPress RETURN to continueG0");
	PostMain();          /*** Must be here ***/
}
