/* 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 highest, middle, average, percent[24];

	char obuff[2048];

	static UBYTE   *VersTag = "\0$VER: CallerStats 1.1 "__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;

				/* Display Header */

				highest = middle = average = 0;

				for(i = 0 ; i < 24 ; i++)
				{
					if( Shared->Saved.sd_LogIns[i] > highest)
						highest = Shared->Saved.sd_LogIns[i];
				}

				if(highest == 0)
				{
					LPuts("[1;31mNo calls received yet!\n\n");
				}
				else
				{
					middle = highest/2;
					for(i = 0 ; i < 24 ; i++)
					{
						if( Shared->Saved.sd_LogIns[i] > 0 )
							percent[i] = (Shared->Saved.sd_LogIns[i] * 60)/highest;
						else percent[i] = 0;
						average += percent[i];
					}
					average = average/24;

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

					for( i=60 ; i>0 ; i-=5) 
					{
						if(i==60) sprintf(obuff, "[1;36m %4ld [0;34m³[0m", highest);
						else if(i==30) sprintf(obuff, "[1;36m %4ld [0;34m³[0m", middle);
						else sprintf(obuff, "      [0;34m³[0m", NULL);
						for( a=0 ; a<24 ; a++)
						{
							if( percent[a] >= i )
							{
								if( a%2 )
								{
									if( average >= i && average < i+5)
										strcat( obuff, "[1;33mÄ[1;34mÛÛ[0m" );
									else strcat( obuff, "[1;34m ÛÛ[0m" );
								}
								else
								{
									if( average >= i && average < i+5)
										strcat( obuff, "[1;33mÄ[0;36mÛÛ[0m" );
									else strcat( obuff, "[0;36m ÛÛ[0m" );
								}
							}
							else
							{
								if( average >= i && average < i+5)
									strcat( obuff, "[1;33mÄÄÄ" );
								else strcat( obuff, "   " );
							}
						}
						strcat( obuff, "\n" );
						LPuts( obuff );
					}
					LPuts( "    [1;36m0 [0;34mÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ[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("[44m           [37mCall Statistics                        BBS Active Totals            \n");
					LPuts("[30;40mÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n");
					sprintf(obuff, "³ [32mTotal: [37m%4ld [32mLocal: [37m%4ld [32mToday: [37m%4ld [30m³ ³[32mDays: [37m%4ld  [32mHours: [37m%3ld  [32mMins: [37m%4ld [32m  [30m³\n", Shared->Saved.sd_Calls, Shared->Saved.sd_LocalCalls, Shared->Saved.sd_CallsToday, Shared->Extra->es_DaysUp, Shared->Extra->es_HoursUp, Shared->Extra->es_MinsUp);
					LPuts(obuff);
					LPuts("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n");
					LPuts("[44m [37mVersion 1.1                                     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" );
	}
}

