/* 
    This module contains routines that do things to AmigaDOS. Things
    such as change directory, or parent directory or delete file etc...
*/

#include "libraries/arpbase.h"
#include "flist.h"

struct Window *make_gadget();
extern void refresh();

extern struct AnchorPath *ap;
extern struct FileLock *startdir, *lock, *olddir;
extern struct Window *winptr;
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];
extern long top, row, line, numfiles, lastsort;
extern char titlestr[80];

extern int PathName();
extern struct NewWindow strwin;
extern char strbuf[256];

long rename_file(file)
char *file;
{
    char buf[256];
	struct Window *strptr;

    strbuf[0] = '\0';
    strptr = make_gadget("Enter string to rename to");

    if (strptr == NULL) {
        auto_req("Couldn't open Requestor!");
        return BAD_COMMAND;
    }

    drawcur();

    wait_for_event(strptr);

    blankcur();

#ifdef DEBUG
    sprintf(buf,"string gadget is -%s-",strbuf);
    auto_req(buf);
#endif

    if (strbuf[0] == '\0')
        return NULL;

    if (!Rename(file,strbuf)) {
        auto_req("Couldn't rename file!");
        return 1;
    }

    strcpy(&fname[line][0],strbuf);

    return NULL;
}

long make_dir()
{
    char buf[256];
    long err;
    struct FileLock *newlock;
	struct Window *strptr;

    strbuf[0] = '\0';
    strptr = make_gadget("Enter name of Directory ");

    if (strptr == NULL) {
        auto_req("Couldn't open Requestor!");
        return BAD_COMMAND;
    }

    wait_for_event(strptr);

    if (strbuf[0] == '\0')
        return NULL;

    newlock = CreateDir(strbuf);

    if (newlock == 0) {
        err = IoErr();
        sprintf(buf,"Couldn't Create directory! DOS error %ld",err);
        auto_req(buf);
        return 1;
    } else
        UnLock(newlock);

    getdir();

    return NULL;
}

long changedir(str)
char *str;
{
    char buf[1000], buf1[1000];
    long rc;
    struct FileLock *dummy;

    dummy = lock;
    lock = Lock(str,ACCESS_READ);
    if (lock == NULL){
        lock = dummy;
        sprintf(buf,"Can't Lock directory -%s-!",str);
        auto_req(buf);
        return DIR_LOCKED;
    }

    strcpy(buf,str);
    TackOn(buf, "*");

    rc = FindFirst(buf,ap); /* ARP does all the work getting the directory */

    if (rc == NULL){
        Fillarray();    /* fill the arrays with the names and info */
        top = 0;
        row = 0;    
        line = 0;
    }
    else {
        if (rc != ERROR_NO_MORE_ENTRIES){
            sprintf(buf,"Can't find directory -%s- !",str);
            auto_req(buf);
            return BAD_DIR;
        } else
            numfiles = 0;
    }

    olddir = CurrentDir(lock);
    
    if (dummy != NULL)
        UnLock(dummy);

    return rc;
}

/* Go up one directory, saving all the locks */

parent()
{
    long rc;
    char buf[1000], buf1[1000];
    struct FileLock *dummy;

    if (lock == NULL)   /* if we are at the parent already -> return */
        return;

                        /* Also watch for the root of the current volume */
    PathName(lock, buf);
    if (buf[strlen(buf)-1] == ':')
        return;

    dummy = ParentDir(lock);
    olddir = CurrentDir(dummy);

    if (lock != NULL)
        UnLock(lock);

    lock = dummy;

    rc = FindFirst("*",ap); 

    if (rc == NULL) {
        Fillarray();
        top = 0;
        row = 0;    
        line = 0;
    }
    else 
        auto_req("Can't change to Parent!");
}

/* Delete (Kill) a file */

long kill(str)
char *str;
{
    long rc,i;
    char buf[1000];

    if (strlen(str) == 0 || numfiles == 0)
        return BAD_COMMAND;

    rc = DeleteFile(str);

    if(rc == NULL) {
        sprintf(buf,"Couldn't delete file %s",str);
        auto_req(buf);
        return BAD_COMMAND;
    } else
        numfiles--;
        
    for (i = line; i < numfiles;i++) {
        swap(&finfo[i], &finfo[i+1]);
        swap(&fname[i], &fname[i+1]);
        swap(&comm[i], &comm[i+1]);
        swap(&fstring[i], &fstring[i+1]);
        swap(&ftime[i], &ftime[i+1]);
    }
    
    return rc;
}

/* 
    Re-get the current directory... This is good for when the user renames
    a file and wants to see the change in the list.
*/

getdir()
{
    long rc,i;
    char buf[1000];

    rc = FindFirst("*",ap); 

    if (rc == NULL) {
        Fillarray();    /* fill the arrays with the names and info */
        top = 0;
        row = 0;    
        line = 0;
    }
    else {
        if (rc != ERROR_NO_MORE_ENTRIES){
            sprintf(buf,"Can't find current directory!");
            auto_req(buf);
            return BAD_DIR;
        } else
            numfiles = 0;
    }
}

/* swap two pointers */

swap(ptr1,ptr2)
long *ptr1,*ptr2;
{
    long temp;
    
    temp = *ptr1;
    *ptr1 = *ptr2;
    *ptr2 = temp;
}
