/*                                                                             */
/*   ___ _            _    _            ___       __ _                         */
/*  | _ ) |_  _ ___  | |  (_)___ _ _   / __| ___ / _| |___ __ ____ _ _ _ ___   */
/*  | _ \ | || / -_) | |__| / _ \ ' \  \__ \/ _ \  _|  _\ 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 converts LSDictionary data file to Language Tools format.     */
/*                                                                             */


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

extern struct Catalog *catalog;

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

extern BOOL aborted;

void LSDConvFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{
    ULONG page;
    char *dir,*type;
    char ipath[256];
    char dpath[256];
    char npath[256];
    char nfile[64];
    char infotext[128];
    char input[1024];
    struct FileInfoBlock *fib;
    FILE *fhi,*fhd,*fhn;
    ULONG x,len,cha,num,prev=0;

    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_dicdir, MUIA_String_Contents,dir);
                set(wi_lsdconv, MUIA_Window_Open, TRUE);
                set(wi_lsdconv, MUIA_Window_ActiveObject, st_dictype);
            }
            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)
    {
        get(st_dictype,MUIA_String_Contents, &type);
        get(lv_dir,MUIA_Dirlist_Directory,&dir);

        if ((strlen(type))>0)
        {
            DoMethod(lv_dir,MUIM_List_GetEntry,MUIV_List_GetEntry_Active,&fib);

            if ((fib->fib_FileName[0]=='L') && (fib->fib_FileName[1]=='S') && (fib->fib_FileName[2]=='D') && (fib->fib_FileName[3]=='_')
                && (fib->fib_FileName[11]=='.') && (fib->fib_FileName[12]=='d'))
            {
                set(wi_lsdconv,MUIA_Window_Open, FALSE);
                set(gr_handle, MUIA_Disabled, TRUE);
                set(bt_abort,MUIA_Disabled, FALSE);

                strcpy(dpath,dir);
                AddPart(dpath,fib->fib_FileName,250);
                strcpy(ipath,dir);
                fib->fib_FileName[12]='n';
                fib->fib_FileName[13]='d';
                fib->fib_FileName[14]='x';
                AddPart(ipath,fib->fib_FileName,250);
                if ((fhd = fopen(dpath,"r"))!=NULL)
                {
                    if ((fhi = fopen(ipath,"r"))!=NULL)
                    {
                        fseek(fhi,0,SEEK_END);
                        len = ftell(fhi)-4;

                        sprintf(infotext,Gcs(46,MSG_46),FilePart(dpath));
                        set(te_status,MUIA_Text_Contents,infotext);
                        get(st_dicdir,MUIA_String_Contents,&dir);

                        aborted = FALSE;

                        for (x=4;x<len;x+=4)
                        {
                            fseek(fhi,x,SEEK_SET);
                            fread(&num,4,1,fhi);
                            fseek(fhd,num,SEEK_SET);
                            fgets(input,1000,fhd);
                            cha=0;

                            while (input[cha++])
                            {
                                if (((input[cha]>='A') && (input[cha]<='Z')) || ((input[cha]>=192) && (input[cha]<=222)))
                                    input[cha]=input[cha]+32;
                            }
                            strcpy(npath,dir);
                            sprintf(nfile,"%s_%c.ltdic",type,input[0]);
                            AddPart(npath,nfile,259);
                            if ((fhn = fopen(npath,"r+"))!=NULL)
                            {
                                if (prev!=input[0])
                                {
                                    fputs("&\n",fhn);
                                    prev = input[0];
                                }

                                fseek(fhn,0,SEEK_END);
                                cha=0;
                                while ((input[cha]) && (input[cha]!=' '))
                                { fputc(input[cha],fhn); cha++; }
                                if (input[cha]==' ')
                                {
                                    input[strlen(input)-1]='\0';
                                    fprintf(fhn,"\n---\n\033p[i]%s\033p[0]\n",input);
                                }
                                cha=0;
                                while ((cha = fgetc(fhd))!='&')
                                {
                                    if (cha == 'ˇ')
                                        fputs("\033i",fhn);
                                    else
                                        fputc(cha,fhn);
                                }
                                fputs("&\n",fhn);
                            fclose(fhn);
                            }
                            set(gau_status,MUIA_Gauge_Current,(((x+4)*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));

                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(47,MSG_47),fib->fib_FileName);

        }
        else
            set(wi_lsdconv, MUIA_Window_ActiveObject, st_dictype);
    }

}

struct Hook LSDConvHook = {{NULL,NULL},(void *)LSDConvFunc,NULL,NULL};
