#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <exec/libraries.h>
#include <libraries/asl.h>
#include <intuition/intuition.h>

struct Library *IconBase;
struct Library *WorkbenchBase;
struct Library *AslBase=NULL;
struct IntuitionBase *IntuitionBase;
struct Library *CxBase;

struct EasyStruct myES=
{
	sizeof(struct EasyStruct),
	0,
	"VIEW's request:",
	"Do you want to quit VIEW ?",
	"YEP|GOMF",
};
/*
struct EasyStruct myES2=
{
	sizeof(struct EasyStruct),
	0,
	"VIEW's error message:",
	"Error %d, %s",
	"GOMF",
};
*/
struct TagItem frtags[] =
{
	ASL_Hail,"Select file to view",
	ASL_Dir,(ULONG)NULL,
	ASL_File,(ULONG)NULL,
	TAG_DONE
};

char buffer[256];
char lastdir[256];

/*	pops up a file-requester	*/

char *request()	/*	pointer to _complete_ file name	*/
{
	struct FileRequester *fr;

	if ((fr=(struct FileRequester *)AllocAslRequest(ASL_FileRequest, frtags))==NULL) return 0;

	if (AslRequest(fr,NULL))
	{
		strcpy(buffer,fr->rf_Dir);
		strcpy(lastdir,buffer);
		frtags[1].ti_Data=(char *)lastdir;
		AddPart(buffer,fr->rf_File,256);
		FreeAslRequest(fr);
		return buffer;
	}
	else return 0;
}

main(int argc, char **argv)
{
	struct DiskObject   *dobj=NULL;
	struct MsgPort    *myport=NULL;
	struct AppIcon   *appicon=NULL;
	struct AppMessage *appmsg=NULL;
	int n,max,answer;
	char *icon,*mview,*lha,*tmp;
	char buff[256],com[512];
	UBYTE **ttypes;


	if ((IntuitionBase=OpenLibrary("intuition.library",37))==NULL)
	{
		exit(20);
	}

	if ((AslBase=OpenLibrary("asl.library", 37L))==NULL)
	{
		CloseLibrary(IntuitionBase);
		exit(20);
	}

	if (!(IconBase=OpenLibrary("icon.library",37)))
	{
		CloseLibrary(IntuitionBase);
		CloseLibrary(AslBase);
		exit(20);
	}

	if (!(WorkbenchBase=OpenLibrary("workbench.library",37)))
	{
		CloseLibrary(IntuitionBase);
		CloseLibrary(AslBase);
		CloseLibrary(IconBase);
		exit(20);
	}

	if ((ttypes=(UBYTE **)ArgArrayInit(argc,argv)))
	{
		mview=ArgString(ttypes,"COMMAND","sys:utilities/multiview");
		icon=ArgString(ttypes,"ICON","sys:utilities/multiview");
		lha=ArgString(ttypes,"LHA","hd1:packer/lha v");
	}

	if (!(dobj=GetDiskObject(icon)))
	{
		CloseLibrary(IntuitionBase);
		CloseLibrary(AslBase);
		CloseLibrary(IconBase);
		CloseLibrary(WorkbenchBase);
		exit(20);
	}

/* The type must be set to NULL for a WBAPPICON */

	dobj->do_Type=NULL;

/* The CreateMsgPort() function is in Exec version 37 and later only */

	myport=CreateMsgPort();
	if(!myport)
	{
		CloseLibrary(IntuitionBase);
		CloseLibrary(AslBase);
		CloseLibrary(IconBase);
		CloseLibrary(WorkbenchBase);	
		FreeDiskObject(dobj);
		exit(20);
	}

/* Put the AppIcon up on the Workbench window */

	appicon=AddAppIconA(0L,0L,"VIEW",myport,NULL,dobj,NULL);
	if(!appicon)
	{
		CloseLibrary(IntuitionBase);
		CloseLibrary(AslBase);
		CloseLibrary(IconBase);
		CloseLibrary(WorkbenchBase);
		FreeDiskObject(dobj);
		DeleteMsgPort(myport);
		exit(20);
	}

	while (1)
	{
		WaitPort(myport);
		while (appmsg=(struct AppMessage *)GetMsg(myport))
		{
			if (!(max=appmsg->am_NumArgs))
			{
				if (!(tmp=request()))
				{
					if (answer=EasyRequest(NULL,&myES,NULL,0))
					{
						ReplyMsg((struct Message *)appmsg);
						goto exit;
					}
				}
				else
				{
					if (!strcmp(&tmp[strlen(tmp)-4],".lha"))
					{
						strcpy(com,lha);
						strcat(com," \"");
						strcat(com,tmp);
						strcat(com,"\"");
						strcat(com," >t:view.tmp");
						Execute(com,0,0);
						strcpy(tmp,"t:view.tmp");
					}
					strcpy(com,mview);
					strcat(com," \"");
					strcat(com,tmp);
					strcat(com,"\"");
					Execute(com,0,0);
				}
			}
			else if (max>0)
			{
				for (n=0; n<max; n++)
				{
					NameFromLock(appmsg->am_ArgList[n].wa_Lock,buff,256);
					AddPart(buff,appmsg->am_ArgList[n].wa_Name,256);
					if (!strcmp(&buff[strlen(buff)-4],".lha"))
					{
						strcpy(com,lha);
						strcat(com," \"");
						strcat(com,buff);
						strcat(com,"\"");
						strcat(com," >t:view.tmp");
						Execute(com,0,0);
						strcpy(buff,"t:view.tmp");
					}
					strcpy(com,mview);
					strcat(com," \"");
					strcat(com,buff);
					strcat(com,"\"");
					Execute(com,0,0);
				}
			}
			ReplyMsg((struct Message *)appmsg);
		}
	}
exit:
	RemoveAppIcon(appicon);
	while(appmsg=(struct AppMessage *)GetMsg(myport)) ReplyMsg((struct Message *)appmsg);
	
	DeleteMsgPort(myport);
	FreeDiskObject(dobj);
	DeleteFile("t:view.tmp");
	CloseLibrary(IntuitionBase);
	CloseLibrary(AslBase);
	CloseLibrary(WorkbenchBase);
	CloseLibrary(IconBase);
}
