/****************************************************************
 *                                                              *
 *    XICON                                                     *
 *                                                              *
 *   Runs Command Script Files from an Icon                     *
 *                                                              *
 *   copyright 1986 by Pete Goodeve -- All Rights Reserved      *
 *                                                              *
 *    Compile with pass2 -v option (Lattice)                    *
 *    Link with lstartup.obj, amiga.lib, lc.lib                 *
 *                                                              *
 * vers 86:8:11                                                 *
 ****************************************************************/

#include "exec/types.h"
#include "workbench/startup.h"
#include "workbench/workbench.h"
#include "workbench/icon.h"

LONG IconBase;

extern struct WBStartup *WBenchMsg;

struct WBArg *argptr;
int nargs;
int autoclose=FALSE, usescript=TRUE;

LONG outfile;

_main() /* bypasses standard C startup code */
{

   if (!WBenchMsg)
      return 0;

   IconBase = OpenLibrary(ICONNAME,1); /* doesn't matter much if not there */

   if (!(outfile = Open("CON:0/10/640/185/XICON 1.05", MODE_OLDFILE)))
      return 0;

   argptr = WBenchMsg->sm_ArgList;
   nargs = WBenchMsg->sm_NumArgs;
   /* Skip the first arg (the program itself) and process the rest */
   for(argptr++, nargs--; nargs; argptr++, nargs--)
      doito(argptr);

   if (!autoclose) {
      display("\n\n<<type ctrl-C to close window>>");
      Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D);
   }
   Close(outfile);
   if (IconBase) CloseLibrary(IconBase);
}
/*** end main ***/

/************************************************************
 * In the code below, I don't follow the usual method of    *
 * passing each command string in turn to Execute.  Instead *
 * I pass the entire command file as the new input stream,  *
 * which Execute accepts after processing the null string   *
 * it gets as a "command".  This avoids the system having   *
 * to reload the DOS "RUN" module for every line of the     *
 * command file, though on the other hand it does lock out  *
 * the possibility of redirecting keyboard input through    *
 * the "current window" to the command.  I thought the      *
 * trade-off was worth it.                                  *
 ************************************************************/

doito(argp) struct WBArg *argp;
{
   LONG oldir;
   LONG infile;

   usescript = TRUE; /* assume icon refers to a script file
                        unless told otherwise (by ToolTypes) */
   oldir = CurrentDir(argp->wa_Lock);
   readtt(argp);
   if (usescript) {
      infile = Open(argp->wa_Name, MODE_OLDFILE);
      Execute("", infile, outfile);
      Close(infile);
   }
   CurrentDir(oldir);
}

/* Read ToolTypes of current icon */

readtt(argp) struct WBArg *argp;
{
   struct DiskObject *iconobj=NULL, *GetDiskObject();
   char **toolarray, *FindToolType();
   char *modes;

   if (!IconBase) {
      display("Icon Library not found -- ignoring ToolTypes\n");
      return 0;
   }
   /* we don't do any checks here for icon type -- probably should...*/
   if (!(iconobj = GetDiskObject(argp->wa_Name))) {
      display("COULDN'T GET INFO FOR ICON!!\n");
      return 0;
   }
   toolarray = iconobj->do_ToolTypes;
   modes = FindToolType(toolarray,"MODE");
   autoclose = MatchToolValue(modes,"closewindow");
   usescript = !MatchToolValue(modes,"noscript");
   dotoolcmds(toolarray); /* some tooltypes may be commands */
   FreeDiskObject(iconobj);
}


/* Execute any commands specified in the ToolTypes */

dotoolcmds(toolarray) char **toolarray;
{
   char *holdnext, *cmdstr, *FindToolType();
   while (*toolarray) {
      holdnext = toolarray[1];
      toolarray[1] = NULL;
      if (cmdstr=FindToolType(toolarray,"CMD"))
         Execute(cmdstr,0,outfile);
      *++toolarray = holdnext; /* restore next item & move to it */
   }
}


display(msg) char *msg;
{
   Write(outfile, msg, strlen(msg));
}
/**************************************/

