/*********************************************************************
 *
 * $Source: Avocado:Work/Data/Source/Project/Misc/RCS/StartFred.c $
 * $Revision: 1.3 $
 * $Date: 1995/01/06 16:45:02 $
 * $Author: Mikael_Saker $
 * ------------------------------------------------------------------
 * $Log: StartFred.c $
 * Revision 1.3  1995/01/06  16:45:02  Mikael_Saker
 * Added real argument-handling. Changed printf() to internal Printf() to
 * disable stdio and gain about 4kB...
 *
 * Revision 1.2  1995/01/03  18:48:04  Mikael_Saker
 * Handles 'sticky' and safer WB-starts.
 *
 * Revision 1.1  1994/12/15  23:17:17  Mikael_Saker
 * Klarar Massa filer och Shell...
 *
 * Revision 1.0  1994/12/13  23:57:48  Mikael_Saker
 * Initial revision
 *
 * ------------------------------------------------------------------
 *		    Copyright (c) 1994,1995, Mikael Saker
 * ------------------------------------------------------------------
 *
 *********************************************************************/

/* Program slightly enhanced and fixed by Daniel Stenberg */

#include <proto/all.h>
#include <stdio.h>
#include <string.h>

// ------------------------------------------------------------------

// %%% MakeVerTags RevInfo
#define PROGNAME	"StartFred"
#define VERSION			1
#define REVISION		3
#define BUMPREV			-1
#define DATE		"06.01.95"
#define VERS		"StartFred 1.3"
#define VSTRING		"StartFred 1.3 (06.01.95)"
#define VERSTAG		"\0$" "VER: StartFred 1.3 (06.01.95)"
// %%% MakeVerTags RevInfo

#define	TEMPLATE	"Files/A/M,Sticky/S"
#define	AFILES			0
#define	ASTICKY			1
#define	ANUMARGS		2

char	ver[] = VERSTAG;
void	main(LONG argc, UBYTE **argv);

// ------------------------------------------------------------------

void main(LONG argc, UBYTE **argv)
{
  UBYTE  command[256],
  **filelist,
  argpath[128],
  path[128],
  temp[128],
  sticky = 0;
  struct WBStartup *wbs;
  struct RDArgs *args;
  LONG  arglist[ANUMARGS];
  ULONG argcount;

  if(argc == 0)
  {
    /*	started from WB... */

    wbs = (struct WBStartup *) argv; //	Startup Msg
    if(wbs->sm_NumArgs >= 1)
    {
      if(wbs->sm_NumArgs == 1)
        SystemTagList("Sys:Rexxc/rx FrexxEd:Rexx/FrexxEdStart.rx", NULL);
      for(argcount = 1;argcount < wbs->sm_NumArgs;argcount++)
      {
        strcpy(command, "Sys:Rexxc/rx FrexxEd:Rexx/FrexxEdStart.rx \"");
        if(NameFromLock(wbs->sm_ArgList[argcount].wa_Lock, path, 128))
        {
          strmfp(temp, path, wbs->sm_ArgList[argcount].wa_Name);
          strcat(command, temp);
          strcat(command, "\"");
          SystemTagList(command, NULL);
        }
      }
    }
  }
  else if(argc>1)
  {
    //	Started from shell

    arglist[ASTICKY] = FALSE;
    arglist[AFILES] = NULL;
    args = ReadArgs(TEMPLATE, arglist, NULL);
    if(args == NULL)
      return;

    if(arglist[ASTICKY])
      sticky = TRUE;

    filelist = (UBYTE **) arglist[AFILES];

    if(GetCurrentDirName(path, 128))	// Get current directory path...
    {
      for(argcount = 0;filelist[argcount] != NULL;argcount++)
      {
        strcpy(command, "Sys:Rexxc/rx FrexxEd:Rexx/FrexxEdStart.rx \"");
        stcgfp(argpath, filelist[argcount]);
        strmfp(temp, path, filelist[argcount]);

        if(strchr(argpath, ':'))
        {
          //  If the file name contains a ':', use the full path name and
          //  not the one from GetCurrentDirName()...
          //  FrexxEdStart.rx *HAVE TO* get the full path specified!
          
          strcat(command, filelist[argcount]);
        }
        else
          strcat(command, temp);
        strcat(command, "\"");

        if(sticky)
          strcat(command, " STICKY");
        
        SystemTagList(command, NULL);
      }
    }
    FreeArgs(args);
  }
  else
  {
    Printf("%s [files] [STICKY]\n"
           "Loads all the specified files into FrexxEd\n", argv[0]);
  }
}
