/*
    This program was written by Stephen W. Berry.
 
    The concept is copyrighted by me, but the program listings
    executable and documentation are placed in the Public Domain.
 
    If you find a bug or enhance this program please let me know
    at the following address:
 
        The Checkered Ball
        A-7 Sinai Circle
        Chelmsford, Ma 01824
        c/o Stephen Berry
 
*/

#define DEBUG TRUE

#include <rexx/storage.h>
#include <rexx/rxslib.h>
#include <libraries/arpbase.h>
#include <stdio.h>
#include "flist.h"

struct RxsLib *RexxSysBase = NULL;

/* external routines - system */

extern void CloseLibrary();
struct Screen *OpenScreen();
struct Window *OpenWindow();
struct IntuiMessage *GetMsg();

/* external routines - Arp */

extern void *ArpAlloc();

/* external routines - mine */

extern void menu(),tticon(),OpenConsole(),drawcur(),blankcur();
extern long parse();
extern void FreeFlistMem(),OpenRexxPort(),DeleteRexxPort(),CleanupRexx();
extern long OpenLib(),StartRexxProg();
extern struct RexxMsg *port,*PollRexxPort(),*WaitMsgPort();
extern void Redirecterror();

/* global variables, structures etc... */

extern struct IOStdReq ConsoleReq;
extern struct FileLock *startdir, *rootdir, *lock;
struct Screen *scrptr = NULL;
struct Window *winptr = NULL, *oldwinptr;
struct RexxMsg myrexxport;
extern long numfiles;

extern long (*finfo[MAXDIR])[4];
extern char (*fname[MAXDIR])[FCHARS];
extern char (*comm[MAXDIR])[FCHARS];
extern char (*fstring[MAXDIR])[LEN_DATSTRING];
extern char (*ftime[MAXDIR])[LEN_DATSTRING];
APTR oldcon;

struct Process *myproc;
long remember = -10;
int didRexx = FALSE;

main(argc,argv)
int argc;
char *argv[];
{
    struct IntuiMessage *msg;
    LONG i,rc = NULL;

    /* parse the command line and get the first file */

    if((rc = parse(argc,argv)) != NULL){
        printf("Input error.\n");
        printf("Flist requires the name(s) of files\n");
        printf("Or FROM and the name of a file with names in it.\n");
        printf("Standard DOS and ARP wildcards are accepted.\n");
        FreeFlistMem();
        Delay(20);
        exit(rc);
    }

    openstuff(); /* open the window and screen */

/*    myproc = (struct Process *)DeviceProc("flist"); */
    myproc = (struct Process *)FindTask(0L);
/*    myproc = (myproc != NULL) ? myproc - sizeof(struct Task): 0; */

    if (myproc != NULL){

/* Believe it or not, this stops ALL requestors from appearing! */

        oldwinptr = myproc->pr_WindowPtr;
        myproc->pr_WindowPtr = (struct Window *)(-1L); 

/* This allows a safer environment for flist's run in the background */

        oldcon = myproc->pr_ConsoleTask;
        myproc->pr_ConsoleTask = (struct Task *)DeviceProc("null:");
        myproc->pr_WindowPtr = winptr; 
        if (myproc->pr_ConsoleTask != NULL)
            freopen("NULL:","w+",stderr);
    }

/*  I link with arp.lib so that I don't have to open:
    intuition, graphics and arp libraries. */

    RexxSysBase = OpenLibrary("rexxsyslib.library",(LONG)RXSVERS);

    if (RexxSysBase != NULL) {
        OpenRexxPort("flport",&myrexxport);
        didRexx = TRUE;
    }

    menu();     /* we should never return ! */
}

Cleanup()
{
    int i;

    myproc->pr_WindowPtr = oldwinptr;
    myproc->pr_ConsoleTask = oldcon;
    if(didRexx)
        DeleteRexxPort(&myrexxport);
    if(RexxSysBase != NULL )
        CloseLibrary(RexxSysBase);
    FreeFlistMem();
    if(winptr != NULL)
        CloseDevice(&ConsoleReq);
        CloseWindowSafely(winptr,NULL);
    if(scrptr != NULL )
        CloseScreen(scrptr);
    UnLock(CurrentDir(startdir));       /* go back to the dir we started at */

    for (i=0;i<MAXDIR;i++)              /* free all array memory */
        if (fname[i] != 0) {
            FreeMem(fname[i],FCHARS);
/*            FreeMem(comm[i],FCHARS); */
            FreeMem(finfo[i],16L);
            FreeMem(fstring[i],LEN_DATSTRING);
            FreeMem(ftime[i],LEN_DATSTRING);
            fname[i] = 0;
            finfo[i] = 0;
            fstring[i] = 0;
            ftime[i] = 0;
        }
}
