/* ASL File Requester Shell for CLI Commands,
  © 1991 Arnie Cachelin,  HyperActive InterMedia  */
/* 24 Sep 1991 At 21:56:39 */

#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <libraries/asl.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/icon.h>
/* #include <proto/asl.h> */
#include <stdio.h>

struct  IntuitionBase         *IntuitionBase=NULL;
struct  AslBase              *AslBase=NULL;
struct  IconBase              *IconBase=NULL;
struct  FileRequester	      *req=NULL;
extern struct   FileHandle    *Open(),*Output();
extern struct   WBStartup     *WBenchMsg;
struct   FileHandle    *DOSOut=NULL,*DOSIn=NULL;

void CleanUp()
{
	if(DOSOut) Close(DOSOut);
  if(req) FreeAslRequest(req);
  if(IconBase) CloseLibrary(IconBase);
  if(AslBase) CloseLibrary(AslBase);
  if(IntuitionBase) CloseLibrary(IntuitionBase);
  exit(0L);
}

int main(argc,argv)
    int argc;
    char    *argv[];
{
	char Title[82]="Hello Sailor!",Command[255],dir[80]="",*file="";
  int i,n;
  struct  DiskObject   *dob;

  if (argc==1)
  {  /* Called w/out args from CLI  */
    printf("[3mUSAGE: [0m[2m%s <FileCommand>[0m\n",argv[0]);
    puts("   FileCommand is the command to be executed.");
    puts("   The selected path and file will replace alternating");
    puts("   occurances of '%s' as in the DOS List LFORMAT command.");
    puts("    ( e.g. FileReq \"C:ShowPic %s%s NOCYCLE\" ) ");
    DOSOut=NULL;
    CleanUp();
	}
  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0);
  if (IntuitionBase==0) CleanUp();

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

  AslBase=(struct AslBase *) OpenLibrary("asl.library",0);
  if (AslBase==0) CleanUp();

	req=(struct  FileRequester *)AllocAslRequestTags(ASL_FileRequest,ASL_Hail,Title,TAG_DONE);
  if (req==NULL) CleanUp();

  if(argc>1)
  {  /* Called with args from CLI */
	  sprintf(Title,argv[1],"Dir:","File");
		DOSOut = Output();
    if(argc>2)
      { strcpy(dir,argv[2]);
        if(argc>3) file=argv[3];
      }

  	if (!AslRequest(req,ASL_Hail,Title,ASL_Dir,dir,ASL_File,file,TAG_END)) CleanUp();
    strcpy(dir,req->rf_Dir);
    n=strlen(dir);
    if( n && ( (char)dir[n-1]!=':') ) strcat(dir,"/");
  	sprintf(Command,argv[1],dir,req->rf_File,dir,req->rf_File,dir,req->rf_File);
    puts(Command);
		Execute(Command,DOSIn,DOSOut);
    DOSOut=NULL;
    CleanUp();
	}
  else
  if(WBenchMsg->sm_NumArgs>0)
    if(dob=(struct  DiskObject *)GetDiskObject(WBenchMsg->sm_ArgList->wa_Name))
    {
      if(!(DOSOut = Open(FindToolType(dob->do_ToolTypes,"WINDOW"),MODE_NEWFILE)))
      DOSOut = Open("CON:0/12/638/132/Command Output Window  " ,MODE_NEWFILE);
  	  sprintf(Title,dob->do_ToolTypes[0],"Dir:","File");
      if (AslRequest(req,ASL_Hail,Title,TAG_END))
        for(i=0;strlen(dob->do_ToolTypes[i])>0;i++)
        {
       	  strcpy(dir,req->rf_Dir);
          n=strlen(dir);     /* What a pain! why doesn't string include '/' or ':' */
          if( n && ((char)dir[n-1]!=':') ) strcat(dir,"/");
          sprintf(Command,dob->do_ToolTypes[i],dir,req->rf_File,dir,req->rf_File,dir,req->rf_File);
          Execute(Command,DOSIn,DOSOut);
       	}
      FreeDiskObject(dob);
    }
  CleanUp();
}