/*  WBX.C Workbench XPress Utility © 1990 Arnie Cachelin for AMIGA Plus */
/*  Friday 11-May-90 11:11:32  */

#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <stdio.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <functions.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>

#define DELAY   120L

struct  IntuitionBase         *IntuitionBase;     /* global structures... */
struct  IconBase              *IconBase;
struct  DiskObject            *dob;
extern struct   WBStartup     *WBenchMsg=NULL;
extern struct   FileHandle    *Open(),*Output();
struct   FileHandle    *DOSOut=NULL,*DOSIn=NULL;
int FromWB=0;

_wb_parse() {} /* This would find a WINDOW= Tooltype, and open a CON:... */
         /* We do this anyway. I really wanted the tiniest code possible */

main(argc,argv)
    int argc;
    char    *argv[];
{
  int i;

  IntuitionBase = (struct IntuitionBase *)
    OpenLibrary("intuition.library",LIBRARY_VERSION);
  if (IntuitionBase==0) CleanUp();

  IconBase=(struct IconBase *)
    OpenLibrary("icon.library",LIBRARY_VERSION);
  if (IconBase==0) CleanUp();

  if(argc)   /* If CLI start, get icon anyway... */
    {            /* Get program icon, This is a VERY finicky f'n */
    dob = GetDiskObject(argv[0]);
    if (dob==0) CleanUp();
    DOSOut = Output();        /* Get CLI window's file handle */
    }
  else        /* Started from Workbench */
    {        /* Get program icon using name in WB Startup Msg */
    dob=GetDiskObject(WBenchMsg->sm_ArgList->wa_Name);
    if (dob==0) CleanUp();

/* Now we try to open a CON: window specified in the icon ToolTypes, or */
/* if there is no WINDOW= tooltype, we open our default CON: */

    if (! (DOSOut = Open(FindToolType(dob->do_ToolTypes,"WINDOW"),MODE_NEWFILE)))
    DOSOut = Open("CON:0/12/638/132/   »»»--Workbench XPress--->   " ,MODE_NEWFILE);
    FromWB=1;
    }
  DOSIn = DOSOut;
  if (FindToolType(dob->do_ToolTypes,"INTERACTIVE"))
    DOSOut = NULL;  /* This will make our CON: two-way, but need ENDCLI */
  else
    DOSIn = NULL;  /* Now CON: window will close with Close() */

/* Main Loop... it will stop at the first blank ToolType entry */
/* The INTERACTIVE tooltype (w/ a WINDOW) will cause the program to */
/* use NULL for the output file handle of Execute() which will leave a CLI */
/* window which must be closed by ENDCLI... This allows DOS scripts with */
/* ASK to get user input.  Make ENDCLI the last command in the script and */
/* make EXECUTE MyScript (e.g.) the last (only?) non-blank ToolType  */

  for(i=0;strlen(dob->do_ToolTypes[i]);i++)
    Execute(dob->do_ToolTypes[i],DOSIn,DOSOut);
  CleanUp();
}
CleanUp()
{
extern int FromWB;
  if(dob) FreeDiskObject(dob);
  if((DOSIn||DOSOut) && FromWB)
   {            /* I wanted to make the program short, or I would have used */
    Delay(DELAY);  /* the DELAY tooltype which is kind of standard...*/
    DOSOut ? Close(DOSOut) : Close(DOSIn);
   }
  if(IconBase) CloseLibrary(IconBase);
  if(IntuitionBase) CloseLibrary(IntuitionBase);
  exit(0L);
}




