/*
**   YAM2NN - Usenet access for YAM p7 and newer
**   Copyright (C) 1999 Karol Bryd <kbryd@femina.com.pl>
**
**   This program is free software; you can redistribute it and/or
**   modify it under the terms of the GNU General Public License
**   as published by the Free Software Foundation; either version 2
**   of the License, or (at your option) any later version.
**
**   This program is distributed in the hope that it will be useful,
**   but WITHOUT ANY WARRANTY; without even the implied warranty of
**   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**   GNU General Public License for more details.
**
**   You should have received a copy of the GNU General Public License
**   along with this program; if not, write to the Free Software
**   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/*
** $Log$
*/

#include "Yam2NN.h"
#include "get_folder_path.h"

char current_user_path[108];
static char tmp[1024];

// gets path for current user's directory

// this needs to be rewritten
int query_for_user(void)
{
    BPTR fh;
    char *ln;

    sprintf(tmp, "/* */\n"
                "OPTIONS RESULTS\n"
                "ADDRESS YAM\n"
                "USERINFO STEM user.\n"
                "call open(fd, 'T:userinfo.tmp', 'w')\n"
                "call writeln(fd, user.maildir)\n"
                "call close(fd)\n");

    fh = Open("T:qfu.rexx", MODE_NEWFILE);
    if(!fh)
        return(-1);
    Write(fh, tmp, strlen(tmp));
    Close(fh);

    Execute("rx T:qfu.rexx", NULL, NULL);

    fh = Open("T:userinfo.tmp", MODE_OLDFILE);
    if(!fh)
        return(-1);

    Read(fh, current_user_path, sizeof(current_user_path));
    Close(fh);

    ln = strchr(current_user_path, 10);
    if(ln)
        *ln = 0;

    return(0);
}

/*
char *stristr(char *buf, char *str)
{
    register int len = strlen(buf);
    register int len2 = strlen(str);
    register int a;

    for(a = 0; a <= len - len2; a++)
        if(strnicmp(&buf[a], str, len2) == 0)
            return(&buf[a]);
    return(0);
}
*/

// scans YAM's folders and gets type of folder from .fconfig files

void scan_folders(struct List *folders)
{
    struct Folder *folder;
    BPTR fh;

    folder = (struct Folder *)folders->lh_Head;

    while(folder->f_next)
    {
        strcpy(tmp, current_user_path);
        AddPart(tmp, folder->f_path, sizeof(tmp));
        AddPart(tmp, ".fconfig", sizeof(tmp));

        fh = Open(tmp, MODE_OLDFILE);
        if(fh)
        {
            char *fconfig, *equal;

            fconfig = AllocVec(1000, MEMF_ANY);
            Read(fh, fconfig, 1000);

            Close(fh);

            equal = stristr(fconfig, "Type");

            equal = strchr(equal, '=');
            if(equal)
            {
                equal++;
                equal = stpblk(equal);

                folder->f_type = atol(equal);
            }
        }
        folder = folder->f_next;
    }
}

// creates list of folders - only in YAM p7+

int build_folders_list(struct List *folders)
{
    BPTR folders_fh;
    int num = 0;

    strcpy(tmp, current_user_path);
    AddPart(tmp, ".folders", sizeof(tmp));
    folders_fh = Open(tmp, MODE_OLDFILE);
    if(!folders_fh)
        return(-2);  // perhaps no YAM P7

    FGets(folders_fh, tmp, sizeof(tmp));

    while(FGets(folders_fh, tmp, sizeof(tmp)))
    {
        if(strnicmp(tmp, "@FOLDER", 7) == 0)
        {
            struct Folder *folder;

            FGets(folders_fh, tmp, sizeof(tmp));

            folder = AllocVec(sizeof(struct Folder), MEMF_ANY | MEMF_CLEAR);
            folder->f_num = num++;
            folder->f_path = AllocVec(strlen(tmp) + 1, MEMF_ANY | MEMF_CLEAR);
            strcpy(folder->f_path, strtok(tmp, "\n"));
            AddTail(folders, folder);
        }
    }
    scan_folders(folders);
    Close(folders_fh);
    return(0);
}

void free_folders_list(struct List *folders)
{
    struct Folder *folder = (struct Folder *)folders->lh_Head;
    struct Folder *next;

    while(folder->f_next)
    {
        next = folder->f_next;
        FreeVec(folder->f_path);
        FreeVec(folder);
        folder = next;
    }
}

// gets folder path

char *get_folder_path(struct List *folders, int type)
{
    struct Folder *folder = (struct Folder *)folders->lh_Head;

    while(folder->f_next)
    {
        if(folder->f_type == type)
            return(folder->f_path);
        folder = folder->f_next;
    }
    return(NULL);
}

// gets folder number

int get_folder_pos(struct List *folders, int type)
{
    struct Folder *folder = (struct Folder *)folders->lh_Head;

    while(folder->f_next)
    {
        if(folder->f_type == type)
            return(folder->f_num);
        folder = folder->f_next;
    }
    return(-1);
}

struct List *init_folder_list(void)
{
    static struct List *folders;

    folders = AllocVec(sizeof(struct List), MEMF_ANY | MEMF_CLEAR);
    if(!folders)
        return(NULL);

    NewList(folders);

    return(folders);
}

void save_folder_list(struct List *folders)
{
    struct Folder *folder;
    BPTR           fh;

    if(!folders)
        return;

    fh = Open("YAM:YAM2NN.folders", MODE_NEWFILE);
    if(!fh)
        return;

    folder = (struct Folder *)folders->lh_Head;

    while(folder->f_next)
    {
        sprintf(tmp, "%s %d %d\n", folder->f_path, folder->f_num, folder->f_type);
        Write(fh, tmp, strlen(tmp));

        folder = folder->f_next;
    }
    Close(fh);
}

struct List *open_folder_list(void)
{
    struct Folder *folder;
    struct List   *list;
    char           name[108];
    BPTR           fh;

    list = AllocVec(sizeof(struct List), MEMF_ANY | MEMF_CLEAR);
    if(!list)
        return(NULL);

    NewList(list);

    fh = Open("YAM:YAM2NN.folders", MODE_OLDFILE);
    if(!fh)
    {
        FreeVec(list);
        return(NULL);
    }

    while(FGets(fh, tmp, sizeof(tmp)))
    {
        folder = AllocVec(sizeof(struct Folder), MEMF_ANY | MEMF_CLEAR);
        if(!folder)
            break;

        sscanf(tmp, "%s %d %d", name, &folder->f_num, &folder->f_type);
        folder->f_path = AllocVec(strlen(name) + 1, MEMF_ANY | MEMF_CLEAR);
        strcpy(folder->f_path, name);

        AddTail(list, folder);
    }

    Close(fh);
    return(list);
}

/*
int main(void)
{
    struct List   *folders;
    struct Folder *folder;

    query_for_user();
    puts(current_user_path);

    folders = open_folder_list();
    if(!folders)
    {
        folders = init_folder_list();

        puts(get_folder_path(folders, INCOMING));
        printf("%d\n", get_folder_pos(folders, DELETED));

        save_folder_list(folders);
        free_folders_list(folders);

        folders = open_folder_list();
    }

    folder = (struct Folder *)folders->lh_Head;

    while(folder->f_next)
    {
        printf("%s %d %d\n", folder->f_path, folder->f_num, folder->f_type);
        folder = folder->f_next;
    }
    free_folders_list(folders);
}
*/
