/*
** ARexx Support
*/

#include "include/config.h"

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

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/rexxsyslib.h>
#include <rexx/storage.h>

#include "include/msg.h"
#include "include/mui.h"
#include "include/search.h"
#include "include/gui.h"
#include "include/rexx.h"
#include "amster_Cat.h"

MUIF rexx_con(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array);
MUIF rexx_dis(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array);
MUIF rexx_ison(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array);
MUIF rexx_search(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array);
MUIF rexx_sstat(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array);
MUIF rexx_whois(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array);
MUIF rexx_download(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array);

struct Hook rexx_conHook = { {0,0}, &rexx_con, NULL, NULL };
struct Hook rexx_disHook = { {0,0}, &rexx_dis, NULL, NULL };
struct Hook rexx_isonHook = { {0,0}, &rexx_ison, NULL, NULL };
struct Hook rexx_searchHook = { {0,0}, &rexx_search, NULL, NULL };
struct Hook rexx_sstatHook = { {0,0}, &rexx_sstat, NULL, NULL };
struct Hook rexx_whoisHook = { {0,0}, &rexx_whois, NULL, NULL };
struct Hook rexx_downloadHook = { {0,0}, &rexx_download, NULL, NULL };

struct MUI_Command rexx_cmds[] = {
	{ "connect", NULL, 0, &rexx_conHook },
	{ "disconnect", NULL, 0, &rexx_disHook },
	{ "isonline", NULL, 0, &rexx_isonHook },
	{ "search", "TITLE/A,STEM/A", 2, &rexx_searchHook },
	{ "searchstate", NULL, 0, &rexx_sstatHook },
	{ "whois", "USER/A", 1, &rexx_whoisHook },
	{ "download", "NUM/A/N", 1, &rexx_downloadHook },
	{ NULL, NULL, 0, NULL }
};

ULONG gRC;


MUIF rexx_con(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array)
{
	if (gui_onlinestate < CONNECTING) nap_login();
	return(0);
}


MUIF rexx_dis(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array)
{
	nap_logout();
	set(gui->stat, MUIA_Text_Contents, MSG_STATUS2_NOTCONNECTED);
	return(0);
}


MUIF rexx_ison(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array)
{
	if (gui_onlinestate == ONLINE) return 1;
	else return 0;
}


MUIF rexx_search(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array)
{
	gRC = 0;

	if (gui_onlinestate < ONLINE) return 15;

	DoMethod(gui->searchpanel, SEARCH_GO, array[0]);
	DoMethod(gui->searchpanel, SEARCH_GETSTATE);
	if (search_state == 1) do {
		nap_listen();
		DoMethod(gui->searchpanel, SEARCH_GETSTATE);
	}
	while (search_state == 1);

	if (search_state == 2) DoMethod(gui->searchpanel, SEARCH_FILLSTEM, array[1]);
	else gRC = 10;

	return gRC;
}


MUIF rexx_sstat(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array)
{
	DoMethod(gui->searchpanel, SEARCH_GETSTATE);
	return (ULONG)search_state;
}


MUIF rexx_whois(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array)
{
	if (gui_onlinestate == ONLINE) nap_sendbuf(NAPC_WHOIS, array[0]);
	return(0);
}


MUIF rexx_download(REG(a0) struct Hook *h,REG(a2) Object *app, REG(a1) char **array)
{
	gRC = 0;

	DoMethod(gui->searchpanel, SEARCH_DOWNLOAD, *(long *)array[0]);

	return gRC;
}


u_long rexx_sendcommand(char *port, char *com)
{
	struct MsgPort *me,*him;
	struct RexxMsg *rmsg;
	u_long rc=0;

	if (me = CreateMsgPort()) {
		if (rmsg = CreateRexxMsg(me,NULL,NULL)) {
			if (rmsg->rm_Args[0] = CreateArgstring(com, strlen(com))) {
				rmsg->rm_Action = RXCOMM;
				Forbid();
				him = FindPort(port);
				Permit();
				if (him) {
					PutMsg(him, (struct Message *)rmsg);
					WaitPort(me);
					GetMsg(me);
					rc = rmsg->rm_Result1;
/*					if (!rc && rmsg->rm_Result2) DeleteArgstring(rmsg->rm_Result2);*/
				}
				DeleteArgstring(rmsg->rm_Args[0]);
			}
			DeleteRexxMsg(rmsg);
		}
		DeleteMsgPort(me);
	}
	return rc;
}


void rexx_execute(char *com, char *argfmt, ...)
{
	static char buf[1024];
	char *p;
	va_list ap;

	p = buf + sprintf(buf,"Run <>NIL: RexxC:RX %s ",com);
	if(argfmt) {
		va_start(ap,argfmt);
		vsprintf(p,argfmt,ap);
		va_end(ap);
	}

	Execute(buf, 0, 0);
}
