/*                                                                             */
/*   ___ _            _    _            ___       __ _                         */
/*  | _ ) |_  _ ___  | |  (_)___ _ _   / __| ___ / _| |___ __ ____ _ _ _ ___   */
/*  | _ \ | || / -_) | |__| / _ \ ' \  \__ \/ _ \  _|  _\ 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 makes index file for a Language Tools dictionary.             */
/*                                                                             */

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

extern struct Catalog *catalog;

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

extern BOOL aborted;

void MakeIndexFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    char *dir;
    char ipath[256];
    char dpath[256];
    char infotext[128];
    struct FileInfoBlock *fib;
    char buf[1024];

    FILE *fhi,*fhd;
    LONG in,out,len,id,page;

    get(gr_dir, MUIA_Group_ActivePage, &page);

    if (page == 1)
    {
        set(gr_handle, MUIA_Disabled, TRUE);
        set(bt_abort,MUIA_Disabled, FALSE);

        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_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'))
            {
                get(lv_dir,MUIA_Dirlist_Directory,&dir);
                strcpy(dpath,dir);
                AddPart(dpath,fib->fib_FileName,100);
                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,"w"))!=NULL)
                    {
                        fseek(fhd,0,SEEK_END);
                        len = ftell(fhd);
                        fseek(fhd,0,SEEK_SET);
                        sprintf(infotext,Gcs(48,MSG_48),FilePart(dpath));
                        set(te_status, MUIA_Text_Contents,infotext);
                        out=0;
                        while(fgets(buf,1000,fhd))
                        {
                            in=strlen(buf);
                            out=out+in;
                            if ((buf[0]=='&') && (buf[1]=='\n'))
                                fwrite(&out,4,1,fhi);
                            set(gau_status, MUIA_Gauge_Current, ((out*100)/len));
                            DoMethod(app, MUIM_Application_InputBuffered);
                            if (aborted)
                                break;
                        }
                    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;
        }
        set(gr_handle, MUIA_Disabled, FALSE);
        set(bt_abort,MUIA_Disabled, TRUE);
        DoMethod(lv_dir, MUIM_Dirlist_ReRead);
    }
    else
        MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(38,MSG_38),NULL);

}

struct Hook MakeIndexHook ={{NULL,NULL},(void *)MakeIndexFunc,NULL,NULL};
