/*
 * Source generated with ARexxBox 1.12 (May 18 1993)
 * which is Copyright (c) 1992,1993 Michael Balzer
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <rexx/storage.h>
#include <rexx/rxslib.h>

#ifdef __GNUC__
/* GCC needs all struct defs */
#include <dos/exall.h>
#include <graphics/graphint.h>
#include <intuition/classes.h>
#include <devices/keymap.h>
#include <exec/semaphores.h>
#endif

#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/rexxsyslib_protos.h>

#ifndef __NO_PRAGMAS

#ifdef AZTEC_C
#include <pragmas/exec_lib.h>
#include <pragmas/dos_lib.h>
#include <pragmas/rexxsyslib_lib.h>
#endif

#ifdef LATTICE
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/rexxsyslib_pragmas.h>
#endif

#endif /* __NO_PRAGMAS */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#ifdef LATTICE
#undef toupper
#define inline __inline
#endif

#ifdef __GNUC__
#undef toupper
static inline char toupper( char c )
{
	return( islower(c) ? c - 'a' + 'A' : c );
}
#endif

#ifdef AZTEC_C
#define inline
#endif

#include "TrackRexx.h"
#include "TrackRexx_rxif.h"

extern struct ExecBase *SysBase;
extern struct DosLibrary *DOSBase;
extern struct RxsLib *RexxSysBase;


/* $ARB: I 848528372 */


/* $ARB: B 17 QUIT */
void rx_quit( struct RexxHost *host, struct rxd_quit **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_quit *rd = *rxd;

	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			SetTrackFlag(CODE_QUIT);
			rd->rc = RETURN_OK;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 17 QUIT */

/* $ARB: B 24 GET */
void rx_get( struct RexxHost *host, struct rxd_get **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_get *rd = *rxd;
    int o = 0, i;
    BOOL BMemErr = FALSE;
    
	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
#define SetNextPtr(VAR, ARRAY) {rd->res.VAR = &rd->res.ARRAY[o++];}
			/* Insert your CODE here */
			UNLESS(
			  (rd->res.server     = strdup(szServerName))&&
			  (rd->res.comment    = strdup(myInfo->comment->buffer))&&
			  (rd->res.username   = strdup(myInfo->userName->buffer))&&
			  (rd->res.realname   = strdup(myInfo->realName->buffer))&&
			  (rd->res.logfile    = strdup(szLogFileName))&&
			  (rd->res.loglevel   = strdup(GetOutputLevelName(nLogLevel)))&&
			  (rd->res.viewby     = strdup(GetViewByName(nViewBy)))&&
			  (rd->res.visibleto  = strdup(szAccessTo))&&
			  (rd->res.logonscript     = strdup(rexxScripts[LOG_LOGON]))&&
			  (rd->res.logoffscript    = strdup(rexxScripts[LOG_LOGOFF]))&&
			  (rd->res.updatescript    = strdup(rexxScripts[LOG_UPDATE]))&&
			  (rd->res.connectscript   = strdup(rexxScripts[LOG_CONNECT]))&&
			  (rd->res.disconnectscript= strdup(rexxScripts[LOG_DISCONNECT]))&&
			  (rd->res.endscript       = strdup(rexxScripts[LOG_END]))&&
			  (rd->res.startscript     = strdup(rexxScripts[LOG_START]))&&
			  (rd->res.defaultscript   = strdup(rexxScripts[LOG_DEFAULT]))) BMemErr = TRUE;
			  
			if (rd->res.port = malloc(20*sizeof(long)))
			{
			  rd->res.port[o] = nPort;               SetNextPtr(port,port);
			  rd->res.port[o] = BConfirmAppLaunch;   SetNextPtr(confirm,port);
			  rd->res.port[o] = (session != NULL);   SetNextPtr(connected,port);
			  rd->res.port[o] = clientListLength;    SetNextPtr(numclients,port);
			  rd->res.port[o] = BEnabled;            SetNextPtr(enabled,port);
			  rd->res.port[o] = BBeepOnLog;          SetNextPtr(beeponlog,port);
			  rd->res.port[o] = nNumActions;         SetNextPtr(numactions,port); 
			  rd->res.port[o] = (TrackWindow==NULL); SetNextPtr(hidden,port);
			  rd->res.port[o] = nLastEntryClicked;   SetNextPtr(lastentryclicked,port);
			  rd->res.port[o] = currentAction;       SetNextPtr(currentaction, port);
			  rd->res.port[o] = maxLogListLen;       SetNextPtr(logviewlength, port);
			  rd->res.port[o] = GetLogViewLevelPercent(nLogViewLevel); SetNextPtr(logviewlevel, port);
			}
            else BMemErr = TRUE;
            
			/* Fill optional fields */
			if (rd->arg.clients)
			{
			 /* allocate & fill null-terminated array for client names */
			 if ((rd->res.clients     = malloc(sizeof(char *)*(clientListLength+1))) &&
			     (rd->res.comments    = malloc(sizeof(char *)*(clientListLength+1))) &&
			     (rd->res.usernames   = malloc(sizeof(char *)*(clientListLength+1))) &&
			     (rd->res.realnames   = malloc(sizeof(char *)*(clientListLength+1))) &&
			     (rd->res.winopen     = malloc(sizeof(long *)*((clientListLength+1)*2))) &&
			     (rd->res.lastchanged = malloc(sizeof(long *)*((clientListLength+1)*2))))
			 {
			  struct Client * c = clientList.lh_Head;
			  long * lastChangedData = ((LONG *)rd->res.lastchanged)+clientListLength+1;
			  long * winOpenData     = ((LONG *)rd->res.winopen)+clientListLength+1;
			  time_t currentTime = time(NULL);
			    
			  for(i=0; ((BMemErr==FALSE)&&(i<clientListLength)); i++)
			  {
			    UNLESS(rd->res.clients[i]  = strdup(c->hostName->buffer)) BMemErr = TRUE;
			    UNLESS(rd->res.comments[i] = strdup(c->comment->buffer))  BMemErr = TRUE;
			    UNLESS(rd->res.usernames[i]= strdup(c->userName->buffer)) BMemErr = TRUE;
			    UNLESS(rd->res.realnames[i]= strdup(c->realName->buffer)) BMemErr = TRUE;
			    rd->res.winopen[i]     = winOpenData;
			    rd->res.lastchanged[i] = lastChangedData;
			    *lastChangedData = (currentTime-(c->timeStamp))/timeUnit; lastChangedData++;
			    *winOpenData     = (c->winOpen->buffer[0] == 'Y'); winOpenData++;
			    c = c->node.ln_Succ;
			  }
			  rd->res.clients[i] = rd->res.comments[i] = rd->res.usernames[i] = rd->res.realnames[i] = NULL; /* terminate the array! */
			  rd->res.lastchanged[i] = rd->res.winopen[i] = NULL;
			 }
			 else BMemErr = TRUE;
			}
			if (rd->arg.actions)
			{
			  /* allocate & fill null-terminated array for action names */
			  /* allocate & fill null-terminated array for client names */
			  if ((rd->res.actions  = malloc(sizeof(char *)*(nNumActions+1))) &&
			      (rd->res.commands = malloc(sizeof(char *)*(nNumActions+1))))
			  {
			    for(i=0; ((BMemErr==FALSE)&&(i<nNumActions)); i++)
			    {
			      UNLESS(rd->res.actions[i]  = strdup(ActionLabels[i]))  BMemErr = TRUE;
			      UNLESS(rd->res.commands[i] = strdup(Actions[i]))       BMemErr = TRUE;
			    }
			    rd->res.actions[i] = rd->res.commands[i] = NULL; /* terminate the array! */
			  }
			  else BMemErr = TRUE;
			}

		    rd->rc  = BMemErr ? RETURN_FAIL : RETURN_OK;
		    rd->rc2 = BMemErr ? ERROR_NO_FREE_STORE : RETURN_OK;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			if (rd->res.clients)          FreeStringArray(rd->res.clients);
			if (rd->res.comments)         FreeStringArray(rd->res.comments);
			if (rd->res.usernames)        FreeStringArray(rd->res.usernames);
			if (rd->res.realnames)        FreeStringArray(rd->res.realnames);
			if (rd->res.actions)          FreeStringArray(rd->res.actions);
			if (rd->res.commands)         FreeStringArray(rd->res.commands);
			if (rd->res.winopen)          free(rd->res.winopen);
			if (rd->res.lastchanged)      free(rd->res.lastchanged);
			if (rd->res.viewby)           free(rd->res.viewby);
			if (rd->res.visibleto)        free(rd->res.visibleto);
			if (rd->res.server)           free(rd->res.server);
			if (rd->res.comment)          free(rd->res.comment);
			if (rd->res.username)         free(rd->res.username);
			if (rd->res.realname)         free(rd->res.realname);
			if (rd->res.logfile)          free(rd->res.logfile);
			if (rd->res.loglevel)         free(rd->res.loglevel);
			if (rd->res.port)             free(rd->res.port);
			if (rd->res.logonscript)      free(rd->res.logonscript);
			if (rd->res.logoffscript)     free(rd->res.logoffscript);
			if (rd->res.updatescript)     free(rd->res.updatescript);
			if (rd->res.connectscript)    free(rd->res.connectscript);
			if (rd->res.disconnectscript) free(rd->res.disconnectscript);
			if (rd->res.endscript)        free(rd->res.endscript);
			if (rd->res.startscript)      free(rd->res.startscript);
			if (rd->res.defaultscript)    free(rd->res.defaultscript);
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 24 GET */

/* $ARB: B 25 SET */
void rx_set( struct RexxHost *host, struct rxd_set **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_set *rd = *rxd;
    int flags = 0;
    
	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			rd->rc = 0;
			if (rd->arg.beeponlog) {BBeepOnLog = ParseBool(rd->arg.beeponlog); flags |= 0x01;}
			if (rd->arg.enabled) SetTrackFlag(ParseBool(rd->arg.enabled) ? CODE_ENABLE : CODE_DISABLE);
			if (rd->arg.hidden)  SetTrackFlag(ParseBool(rd->arg.hidden)  ? CODE_HIDE   : CODE_SHOW);
			if (rd->arg.confirm) {BConfirmAppLaunch = ParseBool(rd->arg.confirm); flags |= 0x01;}
			if (rd->arg.loglevel){nLogLevel         = ParseOutputLevel(rd->arg.loglevel); flags |= 0x01;}
            if (rd->arg.logfile) {char * n; if (n = strdup(rd->arg.logfile)) {free(szLogFileName); szLogFileName = n;}}
			if (rd->arg.left)    {nWinLeft   = *rd->arg.left;   flags |= 0x02;}
			if (rd->arg.top)     {nWinTop    = *rd->arg.top;    flags |= 0x02;}
			if (rd->arg.width)   {nWinWidth  = *rd->arg.width;  flags |= 0x02;}
			if (rd->arg.height)  {nWinHeight = *rd->arg.height; flags |= 0x02;}
			if (rd->arg.comment) SetCommentString(TrackWindow, rd->arg.comment, TRUE);
			if (rd->arg.server)  SetServerString(TrackWindow, rd->arg.server);
			if (rd->arg.port)    SetPortNumber(TrackWindow, *rd->arg.port);
			if (rd->arg.windowtitle) StatMessage(rd->arg.windowtitle);
			if (rd->arg.currentactionindex) SetCurrentActionIndex(TrackWindow,*rd->arg.currentactionindex);
            if (rd->arg.currentaction)    SetCurrentAction(TrackWindow, rd->arg.currentaction);
            if (rd->arg.viewby)           SetViewBy(ParseViewBy(rd->arg.viewby));
            if (rd->arg.logviewlength)    SetLogViewLength(*rd->arg.logviewlength);
            if (rd->arg.logviewlevel)     
            {
              char temp[30];
              sprintf(temp, "%i", *rd->arg.logviewlevel);
              if (SetLogViewLevel(ParseLogViewLevel(temp))) flags |= 0x02;
            }
            if (rd->arg.logonscript)      SetRexxScript(LOG_LOGON,      rd->arg.logonscript);
            if (rd->arg.logoffscript)     SetRexxScript(LOG_LOGOFF,     rd->arg.logoffscript);
            if (rd->arg.updatescript)     SetRexxScript(LOG_UPDATE,     rd->arg.updatescript);
            if (rd->arg.connectscript)    SetRexxScript(LOG_CONNECT,    rd->arg.connectscript);
            if (rd->arg.disconnectscript) SetRexxScript(LOG_DISCONNECT, rd->arg.disconnectscript);
            if (rd->arg.endscript)        SetRexxScript(LOG_END,        rd->arg.endscript);
            if (rd->arg.startscript)      SetRexxScript(LOG_START,      rd->arg.startscript);
            if (rd->arg.defaultscript)    SetRexxScript(LOG_DEFAULT,    rd->arg.defaultscript);
			if ((rd->arg.action)&&(rd->res.actionindex=malloc(sizeof(long))))
			  *rd->res.actionindex = UpdateActions(TrackWindow, rd->arg.action);
			if (flags & 0x01) SetMenuValues();
			if ((flags & 0x02)&&(TrackWindow)) ChangeWindowBox(TrackWindow->win, nWinLeft, nWinTop, nWinWidth, nWinHeight);
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			if (rd->res.actionindex) free(rd->res.actionindex);
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 25 SET */

/* $ARB: B 27 REQUEST */
void rx_request( struct RexxHost *host, struct rxd_request **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_request *rd = *rxd;

	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			if (rd->res.clicked = malloc(sizeof(long)))
			{
			  *rd->res.clicked = MakeReq(
			         rd->arg.title   ? rd->arg.title   : "AmiTrack Rexx Request",
			         rd->arg.message ? rd->arg.message : "An ARexx script called my REQUEST function",
			         rd->arg.buttons ? rd->arg.buttons : "Okay");
			  rd->rc = 0;
			}
			else 
			{
			  rd->rc  = RETURN_ERROR;
			  rd->rc2 = ERROR_NO_FREE_STORE;
			}
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 27 REQUEST */

/* $ARB: B 28 CONNECT */
void rx_connect( struct RexxHost *host, struct rxd_connect **rxd, long action, struct RexxMsg *rexxmsg )
{
	struct rxd_connect *rd = *rxd;
    int connectto = -1, connectwith = -1;
    
	switch( action )
	{
		case RXIF_INIT:
			*rxd = AllocVec( sizeof *rd, MEMF_CLEAR );
			if( rd = *rxd )
			{
				/* set your DEFAULTS here */
			}
			break;
			
		case RXIF_ACTION:
			/* Insert your CODE here */
			if ((rd->arg.actionindex)||(rd->arg.action))
			{
			 if (rd->arg.actionindex) connectwith = *rd->arg.actionindex;
			 if (rd->arg.action)      connectwith = GetTrackActionByName(rd->arg.action);
			}
			else connectwith = currentAction;
			
			if ((rd->arg.hostindex)||(rd->arg.host))
			{
			 if (rd->arg.hostindex)   connectto   = *rd->arg.hostindex;
			 if (rd->arg.host)        connectto   = GetTrackHostByName(rd->arg.host);
			}
			else connectto = nLastEntryClicked;
			
			if ((connectwith >= 0)&&(connectto >= 0)) 
			{
			  rd->rc = DoAction(connectto, connectwith, 
			             (rd->arg.confirm) ? ParseBool(rd->arg.confirm) : BConfirmAppLaunch);
			  if (rd->rc == -2) rd->rc = RETURN_WARN;
			  if (rd->rc == -1) rd->rc = RETURN_ERROR;
			}
			else rd->rc = RETURN_WARN;
			break;
		
		case RXIF_FREE:
			/* FREE your local data here */
			FreeVec( rd );
			break;
	}
	return;
}
/* $ARB: E 28 CONNECT */


#ifndef RX_ALIAS_C
char *ExpandRXCommand( struct RexxHost *host, char *command )
{
	/* Insert your ALIAS-HANDLER here */
	return( NULL );
}
#endif

