/* Zeus WhoIs v1.0 (c)1996 by Rod Schnell
 *
 * Return codes: 0 - Success
 *               1 - Couldn't find user
 *               2 - Invalid Line number
 *               5 - Bad args
 *              10 - Couldn't open Zeus Library
 */


#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 <nodes.h>

struct Library 		*PipelineBase;
struct User *u;
long					 LineNumber;
ULONG					 WriteFlags;

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

UBYTE *Months[] =
{
	"Jan",	"Feb",	"Mar",	"Apr",	"May",	"Jun",
	"Jul",	"Aug",	"Sep",	"Oct",	"Nov",	"Dec",
};

UBYTE *Genders[] =
{
	"???",
	"Male",
	"Female",
};

int CalcAge ( struct tm *t, struct User *user );

/* Zeus routine to calculate user age from a time struct (as returned by GMTime()) */
int
CalcAge ( struct tm *t, struct User *user )
{
	signed	int	Age;
	signed	int	Mon;

	Age = t->tm_year - (user->ur_DOB_Year - 1900) ;

	Mon = (t->tm_mon + 1) - user->ur_DOB_Mon;
	if ( Mon < 0 )
	{
		Age --;
	} else
	if ( ! Mon )
	{
		Mon = t->tm_mday - user->ur_DOB_Day;
		if ( Mon < 0 )
		{
			Age --;
		}
	}
	return ( Age ) ;
}

int main(int argc, char *argv[])
{
	struct Line	*le;
	struct tm   *FirstCall, *LastCall, t;
	int online, RC;
	UBYTE Age;

	char obuff[256], Location[70];

	if (argc < 3 )
	{
		Printf("Syntax: WhoIs <Line number> <\"RealName|Alias\">\n\n");
		exit(RETURN_WARN);
	}
	
	RC = 0;

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

			if (u = LoadUser(argv[2]))
			{
				RC = 0;
				GMTime ( Time ( NULL ), &t ) ;
				Age = CalcAge ( &t, u ) ;

				FirstCall = gmtime ( &u->ur_FirstCall ) ;
				LastCall  = gmtime ( &u->ur_LastCall ) ;

				sprintf( Location, "%s, %s, %s", u->ur_Town, u->ur_County, u->ur_Country ) ;

				LPuts("[1;30mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
				LPuts("[1;37;44mAlias                                    Real Name                             \n");
				LPuts("[30;40mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");

				if( (online = IsUserOnline(argv[2])) != -1)
					sprintf( obuff, "[1;37m%-40s %-40s\n[1;37m%-40.40s [1;37;44mLastCall[1;37;40m: ONLINE [1;34m([1;37m%d[1;34m)  [1;37;44mAge[1;37;40m: %d [1;34m([1;37m%s[1;34m)\n", u->ur_Alias, u->ur_Name, Location, online, Age, Genders[u->ur_Gender]);
				else
					sprintf( obuff, "[1;37m%-40s %-40s\n[1;37m%-40.40s [1;37;44mLastCall:[1;37;40m %02d-%3s-19%02d [1;37;44mAge:[1;37;40m %d [1;34m([1;37m%s[1;34m)\n", u->ur_Alias, u->ur_Name, Location, LastCall->tm_mday, Months[LastCall->tm_mon], LastCall->tm_year, Age, Genders[u->ur_Gender]);
				LPuts( obuff );

				LPuts("[30;40mÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n");
			}
			else RC = 1;
		}
		else
		{
			Printf( "Invalid line number!\n\n" );
			RC = 2;
		}
	CloseLibrary(PipelineBase);
	}
	else
	{
		Printf( "Cannot open Zeus Library!\n\n" );
		RC = RETURN_FAIL;
	}
	exit( RC ); 
}

