/*                                                                             */
/*   ___ _            _    _            ___       __ _                         */
/*  | _ ) |_  _ ___  | |  (_)___ _ _   / __| ___ / _| |___ __ ____ _ _ _ ___   */
/*  | _ \ | || / -_) | |__| / _ \ ' \  \__ \/ _ \  _|  _\ V  V / _` | '_/ -_)  */
/*  |___/_|\_,_\___| |____|_\___/_||_| |___/\___/_|  \__|\_/\_/\__,_|_| \___|  */
/*                                                                             */
/*             Copyright 2002, Miklós Németh (Blue Lion Software)              */
/*                        email: desco@freemail.hu                             */
/*                   WWW: http://amigos.amiga.hu/bluelion                      */
/*                                                                             */
/* --------------------------------------------------------------------------- */
/*                                                                             */
/* WARNING!                                                                    */
/* If you have any ideas to change the code, please contact the author!        */
/* (see above!)                                                                */
/*                                                                             */
/* FUNCTION                                                                    */
/* This function creates spell dictinary for Language Tools.                   */
/*                                                                             */

#include "dichandle.h"
#include "locale.h"

extern struct Catalog *catalog;

extern APTR app,wi_main,wi_spell,
    gr_dir,lv_dir,te_status,gau_status,st_language,st_spellpath,
    gr_handle,bt_abort;

extern BOOL aborted;

void MakeSpellFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{

    char *dir,*path;
    char ipath[256];
    char dpath[256];
    char spath[256];
    char infotext[128];
    struct FileInfoBlock *fib;
    char buf[1024];
    FILE *fhi,*fhd,*fhs;
    LONG index,len,id,x,out,page;

    if (*param == 0)
    {
        DoMethod(lv_dir, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &fib);
        get(gr_dir,MUIA_Group_ActivePage, &page);

        if (page == 1)
        {
            if (fib)
            {
                get(lv_dir, MUIA_Dirlist_Directory, &dir);
                set(st_spellpath, MUIA_String_Contents,dir);
                set(wi_spell, MUIA_Window_Open, TRUE);
                set(wi_spell, MUIA_Window_ActiveObject, st_language);
            }
            else
                MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(39,MSG_39),NULL);
        }
        else
            MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(38,MSG_38),NULL);
    }
    else if (*param == 1)
    {
        set(wi_spell,MUIA_Window_Open, FALSE);
        set(gr_handle, MUIA_Disabled, TRUE);
        set(bt_abort,MUIA_Disabled, FALSE);

        get(lv_dir,MUIA_Dirlist_Directory,&dir);
        id = MUIV_List_NextSelected_Start;

        for (;;)
        {
            DoMethod(lv_dir, MUIM_List_NextSelected, &id);
            if (id==MUIV_List_NextSelected_End) break;
            DoMethod(lv_dir, MUIM_List_GetEntry, id, &fib);

            if ((fib->fib_DirEntryType < 0) && ((fib->fib_FileName[8]=='.') && (fib->fib_FileName[9]=='l')
                && (fib->fib_FileName[10]=='t') && (fib->fib_FileName[11]=='d')
                &&(fib->fib_FileName[12]=='i') && (fib->fib_FileName[13]=='c')))
            {
                strcpy(dpath,dir);
                get(st_spellpath,MUIA_String_Contents,&path);
                strcpy(spath,path);
                get(st_language,MUIA_String_Contents,&path);
                AddPart(spath,path,250);
                strcat(spath,".spell");
                AddPart(dpath,fib->fib_FileName,2500);
                fib->fib_FileName[11]='i';
                fib->fib_FileName[12]='d';
                fib->fib_FileName[13]='x';
                strcpy(ipath,dir);
                AddPart(ipath,fib->fib_FileName,100);

                aborted = FALSE;

                if ((fhd = fopen(dpath, "r"))!=NULL)
                {
                    if ((fhi = fopen(ipath,"r"))!=NULL)
                    {
                        fseek(fhi,0,SEEK_END);
                        len = ftell(fhi)/4;
                        fseek(fhi,0,SEEK_SET);
                        if ((fhs = fopen(spath,"r+"))!=NULL)
                        {
                            sprintf(infotext,Gcs(49,MSG_49),FilePart(spath),FilePart(dpath));
                            set(te_status, MUIA_Text_Contents,infotext);
                            fseek(fhs,0,SEEK_END);
                            x = 0;
                            while(fread(&index,4,1,fhi))
                            {
                                fseek(fhd,index,SEEK_SET);
                                if (fgets(buf,1000,fhd))
                                    fputs(buf,fhs);
                                set(gau_status, MUIA_Gauge_Current, ((x*100)/len));
                                DoMethod(app,MUIM_Application_InputBuffered);
                                if (aborted)
                                    break;
                                x++;
                            }
                        fclose(fhs);
                        }
                        else
                            MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(36,MSG_36),FilePart(spath));
                    fclose(fhi);
                    }
                    else
                        MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(36,MSG_36),FilePart(ipath));
                fclose(fhd);
                }
                else
                    MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(36,MSG_36),FilePart(dpath));
            }
            else
                MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(37,MSG_37),fib->fib_FileName);
            if (aborted)
                break;
        }

        /* Making index for .spell file */

        if (!aborted)
        {
            get(st_spellpath,MUIA_String_Contents,&path);
            strcpy(ipath,path);
            get(st_language,MUIA_String_Contents,&path);
            AddPart(ipath,path,250);
            strcat(ipath,".spidx");

            out=0;

            if ((fhs = fopen(spath,"r"))!=NULL)
            {
                fseek(fhs,0,SEEK_END);
                len = ftell(fhs);
                fseek(fhs,0,SEEK_SET);
                if ((fhi = fopen(ipath,"w"))!=NULL)
                {
                    sprintf(infotext,Gcs(48,MSG_48),FilePart(spath));
                    set(te_status,MUIA_Text_Contents,infotext);
                    fwrite(&out,4,1,fhi);
                    while(fgets(buf,1000,fhs))
                    {
                        out+=strlen(buf);
                        fwrite(&out,4,1,fhi);
                        set(gau_status, MUIA_Gauge_Current,((out*100)/len));
                    }
                fclose(fhi);
                }
                else
                    MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(36,MSG_36),FilePart(spath));
            fclose(fhs);
            }
            else
                MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(36,MSG_36),FilePart(dpath));
        }
        set(gr_handle, MUIA_Disabled, FALSE);
        set(bt_abort,MUIA_Disabled, TRUE);
        DoMethod(lv_dir, MUIM_Dirlist_ReRead);
    }
}

struct Hook MakeSpellHook ={{NULL,NULL},(void *)MakeSpellFunc,NULL,NULL};
