/*                                                                             */
/*   ___ _            _    _            ___       __ _                         */
/*  | _ ) |_  _ ___  | |  (_)___ _ _   / __| ___ / _| |___ __ ____ _ _ _ ___   */
/*  | _ \ | || / -_) | |__| / _ \ ' \  \__ \/ _ \  _|  _\ 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 optimizes the new dictionary files.                           */
/*                                                                             */

#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 Much2OneFunc(struct Hook *hook __asm("a0"), APTR obj __asm("a2"), ULONG *param __asm("a1"))
{

    ULONG page,x,len,neww;
    LONG id;
    char *dir;
    struct FileInfoBlock *fib;
    char spath[256];
    char dpath[256];
    char infotext[128];
    char prev[1024];
    char input[1024];
    FILE *fhs,*fhd;

    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(spath,dir);
                AddPart(spath,fib->fib_FileName,250);
                strcpy(dpath,dir);
                fib->fib_FileName[11]='t';
                fib->fib_FileName[12]='m';
                fib->fib_FileName[13]='p';
                AddPart(dpath,fib->fib_FileName,250);

                x=0;
                sprintf(infotext,Gcs(50,MSG_50),FilePart(spath));
                set(te_status, MUIA_Text_Contents, infotext);
                neww = 0;
                aborted = FALSE;

                if ((fhs = fopen(spath,"r"))!=NULL)
                {
                    if ((fhd = fopen(dpath,"w"))!=NULL)
                    {
                        fseek(fhs,0,SEEK_END);
                        len = ftell(fhs);
                        fseek(fhs,0,SEEK_SET);

                        while(fgets(input,1000,fhs))
                        {
                            x=x+strlen(input);
                            if (!strcmp(input,"&\n"))  neww=1;

                            if (neww==1)
                            {
                                neww=2;
                            }
                            else if (neww==2)
                            {
                                if (strcmp(prev,input))
                                    fprintf(fhd,"&\n%s",input);
                                strcpy(prev,input);
                                neww=3;
                            }
                            else if (neww==3)
                            {
                                fputs(input,fhd);
                            }
                        set(gau_status, MUIA_Gauge_Current, ((x*100)/len));
                        DoMethod(app,MUIM_Application_InputBuffered);
                        if (aborted)
                            break;
                        }
                        fputs("&\n",fhd);
                    fclose(fhd);
                    }
                    else
                        MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(36,MSG_36),FilePart(dpath));
                fclose(fhs);
                }
                else
                    MUI_Request(app,wi_main,0,Gcs(34,MSG_34),Gcs(35,MSG_35),Gcs(36,MSG_36),FilePart(dpath));

                DeleteFile(spath);
                Rename(dpath,spath);
            }
            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 Much2OneHook ={{NULL,NULL},(void *)Much2OneFunc,NULL,NULL};
