/* EZQ.c    v1.0ß © 1991 Arnie Cachelin
   Run DOS operations on files dropped onto Workbench 2.0 App Icon.
   DOS commands specified in the EZQ icon's ToolTypes will be filled
   with the path and name of incoming icons replacing alternate '%s's
   then Execute()'d.  (Sorry, no console I/O provided)  */
/* 24 Sep 1991 At 22:21:11 */

#include <intuition/intuition.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>
#include <proto/wb.h>
#include <workbench/icon.h>
#include <proto/icon.h>
#define YES_ANSWER    "  YES  "
#define NO_ANSWER     "  NO  "
#include "AskUser.h"  /* The YES_ANSWER defines override 'Continue','cancel' */
#include "EZQIcon.h"  /*  Output from my 'Icon' utility works nicely here!  */
#define DIRLEN 34
#define VERSION  "EZQ v1.0 © 1991 Arnie Cachelin"
#define INTUITIONNAME	"intuition.library"
#define WORKBENCHNAME	"workbench.library"
#define ICONNAME	"icon.library"

struct IntuitionBase *IntuitionBase = NULL;
struct WorkbenchBase *WorkbenchBase = NULL;
struct Library *IconBase = NULL;
struct Library *OpenLibrary();
struct MsgPort *CreatePort();
struct Message *GetMsg();
struct AppIcon *AddAppIcon();
struct MsgPort *msgport = NULL;
struct AppIcon *ai = NULL;
struct IntuiMessage *imsg;
struct AppMessage *amsg;
struct DiskObject *dob=NULL;
int imagememsize = 0;
extern struct   WBStartup     *WBenchMsg;

#ifdef BLAB
void ShowMsg(char *title,struct AppMessage *amsg)
{
	struct WBArg *argptr;
  int i,n;
  char dirname[DIRLEN+1];

	argptr = amsg->am_ArgList;
  printf("%s: appmsg=%lx, Type=%ld, ID=%ld, UserData=%ld, NumArgs=%ld\n",title, amsg, amsg->am_Type, amsg->am_ID, amsg->am_UserData, amsg->am_NumArgs);
  for (i=0; i<amsg->am_NumArgs; i++)
  {
   if(argptr->wa_Lock)  /* AppIcons have wa_Lock=0... get "SYS:" as dir */
     if (NameFromLock(argptr->wa_Lock,dirname,DIRLEN))
      if( (n=strlen(dirname)) && ((char)dirname[n-1]!=':') ) strcat(dirname,"/");
	 	printf("\targ(%ld): Name='%s%s', Lock=%lx\n",i, dirname,argptr->wa_Name, argptr->wa_Lock);
    strcpy(dirname,"");
  	argptr++;
	}
}
#endif

void Terminate(char *text)
{
  if (ai) RemoveAppIcon(ai);
	if (msgport) DeletePort(msgport);
  if(IconBase) CloseLibrary(IconBase);
  if(WorkbenchBase) CloseLibrary(IntuitionBase);
  if(IntuitionBase) CloseLibrary(IntuitionBase);
#ifdef BLAB
  printf("Terminating: %s\n",text);
#endif
  exit(0L);
}

void Initialize(char *text)
{
  ULONG userdata = 0,id = 1;

#ifdef BLAB
  printf("Initializing: %s\n",text);
#endif
  if (!(IntuitionBase = OpenLibrary(INTUITIONNAME, 0)))
    Terminate("Could not open intuition\n");
	if (!(WorkbenchBase = OpenLibrary(WORKBENCHNAME, 36)))
    Terminate("Could not open workbench\n");
  if (!(IconBase = OpenLibrary(ICONNAME, 36)))
    Terminate("Could not open icon library\n");
	if (!(msgport = CreatePort("appwindow", 0)))
    Terminate("Could not createport\n");
	if (!(ai = AddAppIcon(id, userdata, text, msgport, NULL, &DOB, NULL)))
		Terminate("Could not addappicon()\n"); /* It would be even easier to just use the prog. icon 'dob'  */
}                          /*  But that could get confusing, with identical icons floatin around  */

#define MESLEN 15
main(argc, argv)
int argc;
char **argv;
{
	struct Message *GetMsg();
 	char dir[DIRLEN+1]="",Command[255],*file;
  struct WBArg *argptr;
  int i,n,t;
	BOOL done = FALSE;
  ULONG Sigs;
  char MesText[MESLEN+2*DIRLEN]="Really Quit ",ProgName[2*DIRLEN];

  if(argc) strcpy(ProgName,argv[0]);
  else strcpy(ProgName,WBenchMsg->sm_ArgList->wa_Name);
  strncat(MesText,ProgName,2*DIRLEN);
  Initialize(ProgName);
  Sigs= (1 << msgport->mp_SigBit);
  if((dob = (struct  DiskObject *)GetDiskObject(ProgName) ))
  {
  	do
    {
      Wait(Sigs);
      while (amsg =(struct AppMessage *) GetMsg(msgport))
      {
#ifdef BLAB
        ShowMsg("APP ",amsg);
#endif
        if (amsg->am_NumArgs==0)  /* Just a double-click  */
        {
          ReplyMsg((struct Message *)amsg);
          done = AskUser(NULL,MesText);
        }
        else
        {
          argptr = amsg->am_ArgList;
          for (i=0; i<amsg->am_NumArgs; i++,argptr++)
            if(argptr->wa_Lock)
            {
              NameFromLock(argptr->wa_Lock,dir,DIRLEN);
              if( (n=strlen(dir)) && ((char)dir[n-1]!=':') ) strcat(dir,"/");
              file=argptr->wa_Name;
              for(t=0;strlen(dob->do_ToolTypes[t]);t++)
              {
                sprintf(Command,dob->do_ToolTypes[t],dir,file,dir,file,dir,file);
                Execute(Command,NULL,NULL);
              }
            }
          ReplyMsg((struct Message *)amsg);
        }
      }
  	} while (!done);
    FreeDiskObject(dob);
  }
  else Terminate("Program Has No Icon!!");
  Terminate("All OK.");
}
