/************************************************************
* MultiUser - MultiUser Task/File Support System				*
* ---------------------------------------------------------	*
* Logout																		*
* ---------------------------------------------------------	*
* © Copyright 1993-1994 Geert Uytterhoeven						*
* All Rights Reserved.													*
************************************************************/


#include <exec/types.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <utility/tagitem.h>
#include <libraries/multiuser.h>
#include <proto/multiuser.h>

#include "Logout_rev.h"

#include "Locale.h"

char __VersTag__[] = VERSTAG;


int __saveds Start(char *arg)
{
	struct ExecBase *SysBase;
	struct DosLibrary *DOSBase;
	struct muBase *muBase = NULL;
	struct RDArgs *args;
	LONG argarray[] = {
		NULL, NULL, NULL, NULL, NULL, NULL
	};
	struct TagItem tags[6];
	struct Task *task = NULL;
	int rc = RETURN_ERROR;
	struct LocaleInfo li;

	SysBase = *(struct ExecBase **)4;

	if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
		 (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
		rc = ERROR_INVALID_RESIDENT_LIBRARY;
		goto Exit;
	}

	OpenLoc(&li);

	args = ReadArgs("GUI/S,TASK/K,GLOBAL/S,QUIET/S,PROCESS/K/N,ALL/S",
						 argarray, NULL);
	if (!args)
		PrintFault(IoErr(), NULL);
	else if (argarray[1] && argarray[4])
		PutStr(GetLocS(&li,MSG_BOTH_TASKPROC));
	else if (argarray[1] && !(task = FindTask((char *)argarray[1])))
		VPrintf(GetLocS(&li,MSG_TASKNOTFOUND), (ULONG *)&argarray[1]);
	else if (argarray[4] &&
				!(task = (struct Task *)FindCliProc((ULONG)*(ULONG *)argarray[4])))
		VPrintf(GetLocS(&li,MSG_PROCNOTFOUND), (ULONG *)argarray[4]);
	else {
		tags[0].ti_Tag = muT_Graphical;
		tags[0].ti_Data = argarray[0];
		tags[1].ti_Tag = muT_Task;
		tags[1].ti_Data = (ULONG)task;
		tags[2].ti_Tag = muT_Global;
		tags[2].ti_Data = argarray[2];
		tags[3].ti_Tag = muT_Quiet;
		tags[3].ti_Data = argarray[3];
		tags[4].ti_Tag = muT_All;
		tags[4].ti_Data = argarray[5];
		tags[5].ti_Tag = TAG_DONE;
		muLogoutA(tags);
		rc = RETURN_OK;
	}
	FreeArgs(args);

	CloseLoc(&li);

Exit:
	CloseLibrary((struct Library *)muBase);
	CloseLibrary((struct Library *)DOSBase);

	return(rc);
}
