/* Zeus CallerStats v1.0 (c)1996 by Rod Schnell
 *
 */


#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define	USE_DOORLIB

#include <proto/pipeline.h>
#include <proto/pipeutil.h>
#include <nodes.h>

struct Library *PipelineBase;
long					 LineNumber;
ULONG					 WriteFlags;

int main(int argc, char *argv[])
{
	int i, a;
	struct SharedData	*Shared;
	struct Line				*le;

	int daily[24], average, ondays, onhours, onmins;

	char obuff[2048];

	static UBYTE   *VersTag = "\0$VER: CallerStats 1.0 "__AMIGADATE__;

	if (argc != 2 || !strcmp(argv[1], "?"))
	{
		Printf("Syntax: CallerStats <Line number>");
		exit(RETURN_WARN);
	}

	if (PipelineBase = OpenLibrary("zeus.library", 0))
	{
		if( Shared = GetShared() )
		{
			LineNumber = atol( argv[1] );
			if( le = GetLine( LineNumber ))
			{
				WriteFlags = NULL;
				onmins = average = 0;

				for(i = 0 ; i < 24 ; i++)
				{
					onmins += Shared->Extra->es_Online[i];
				}

				if(onmins == 0)
				{
					LPuts("\n[1;31mNo calls received yet!\n\n");
				}
				else
				{
					average = onmins/(24*Shared->Extra->es_DaysUp + Shared->Extra->es_HoursUp);
					ondays  = onmins/(24*60);
					onhours = onmins/60 - ondays*24;
					onmins  = onmins%60;

					for(i = 0 ; i < 24 ; i++)
					{
						if( Shared->Extra->es_DaysUp )
						{
							if( Shared->Extra->es_Online[i] > 0 )
								daily[i] = Shared->Extra->es_Online[i]/Shared->Extra->es_DaysUp;
							else daily[i] = 0;
						}
						else daily[i] = Shared->Extra->es_Online[i];
					}

					/* Display Header */

					if( le->le_User->ur_Flags & UF_CLRSCR)
						LPuts("[2J[H");
					LPuts("[1;30mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
					sprintf( obuff, "[1;37;43m System Usage Stats (Mins/hour) %47.47s\n", Shared->SysVars->sv_SystemName);
					LPuts(obuff);
					LPuts("[30;40mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");

					for( i=60 ; i>0 ; i-=5) 
					{
						if(average > i - 3 && average < i + 3)
							sprintf(obuff, "[1;31m %4ld [0;36m³[0m", i);
						else sprintf(obuff, "[1;36m %4ld [0;36m³[0m", i);
						for( a=0 ; a<24 ; a++)
						{
							/* subtract 3 to round off to nearest 5 minute increment */
							if( daily[a] > i - 3 )
							{
								if( a%2 )
								{
									if(average > i - 3 && average < i + 3)
										strcat( obuff, "[1;31mÄ[1;33mÛÛ[0m" );
									else strcat( obuff, "[1;33m ÛÛ[0m" );
								}
								else
								{
									if(average > i - 3 && average < i + 3)
										strcat( obuff, "[1;31mÄ[0;33mÛÛ[0m" );
									else strcat( obuff, "[0;33m ÛÛ[0m" );
								}
							}
							else
							{
								if(average > i - 3 && average < i + 3)
									strcat( obuff, "[1;31mÄÄÄ" );
								else strcat( obuff, "   " );
							}
						}
						strcat( obuff, "\n" );
						LPuts( obuff );
					}
					LPuts( "    [1;36m0 [0;36mÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ[0m\n");
					LPuts( "        [1;36m00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23[0m\n");

					/* Display Footer */
					LPuts("[1;30mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
					LPuts("[43m          [37mUser Online Totals                      BBS Active Totals            \n");
					LPuts("[30;40mÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n");
					sprintf(obuff, "³ [32mDays: [37m%4ld  [32mHours: [37m%4ld  [32mMins: [37m%4ld [30m³ ³[32mDays: [37m%4ld  [32mHours: [37m%3ld  [32mMins: [37m%4ld [32m  [30m³\n", ondays, onhours, onmins, Shared->Extra->es_DaysUp, Shared->Extra->es_HoursUp, Shared->Extra->es_MinsUp);
					LPuts(obuff);
					LPuts("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n");
					LPuts("[43m [37mVersion 1.0                                     Copyright (c)1996 Rod Schnell [0m\n");
				}
			}
			else
			{
				Printf( "Illegal line number!\n\n" );
			}
		}
		else
		{
			Printf( "Cannot get SharedData -- Is Zeus running?\n\n" );
		}
		CloseLibrary(PipelineBase);
	}
	else
	{
		Printf( "Cannot open Zeus Library!\n\n" );
	}
}

