#include <exec/types.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <libraries/dosextens.h>
#include <proto/dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <devices/tpt.h>

#include <dlg/dlg.h>
#include <dlg/user.h>
#include <dlg/resman.h>

#include <link/io.h>
#include <link/lang.h>

#include <proto/dlg.h>

#include <pragmas/dlg.h>

#include <private/Version.h>
#define  ObjRev "1"
const UBYTE version[]="\0$VER: DLGEdit " BUILDVER "." ObjRev " ©" COPYRIGHT " by Digerati Dreams "__AMIGADATE__;

void main(int, char **);
void _CXBRK(void);


struct USER_DATA UserDat;
struct Ram_File  RStruct;
char  *buf     = NULL;

BPTR            sout;
struct Library *DLGBase;
char          **SA;


void main(int argc,char **argv)

{ULONG size;
 ULONG counter;

 char *tempbuf;
 char  Ext     [4];
 char  tempfile[256];

 BPTR  fh;

 sout = Output();

 if (!(DLGBase = OpenLibrary(DLGNAME, DLGVERSION)))  exit(0);
 if (GetDevName(Ext)==-1)                           _CXBRK();
 if (!ReadUser(&RStruct,&UserDat,Ext))               _CXBRK();
 if (!(SA = getlang(Ext)))                          _CXBRK();

 if (argc!=2)
    {AFPrintf(NULL, sout, SA[60]);
    _CXBRK();
    }

 ASPrintf(NULL, tempfile,"%s~", argv[1]);
 Copy(argv[1], tempfile);

 if (CallEditor(NULL,NULL,argv[1],0,&UserDat,&RStruct,Ext)!=-1)
    {FileSize(argv[1], &size);

     if (!(buf = malloc(size)))
        {AFPrintf(NULL, sout, SA[61]);
         DeleteFile(argv[1]);
         Rename(tempfile, argv[1]);
         _CXBRK();
        }

     GetFirstStruct(argv[1], buf, size);
     for(tempbuf = buf, counter = 0; counter < size; counter++)
        {if (*tempbuf==13)  *tempbuf=10;
         tempbuf++;
        }

     DeleteFile(argv[1]);
     if (!(fh = Open(argv[1], MODE_NEWFILE)))
        {AFPrintf(NULL, sout, SA[61]);
         Rename(tempfile, argv[1]);
         _CXBRK();
        }

     Write(fh,buf,size);
     Close(fh);
    }
  else
    AFPrintf(NULL, sout, SA[62]);

 DeleteFile(tempfile);
_CXBRK();
}


void _CXBRK()

{if (buf)     free(buf);
 CloseLibrary(DLGBase);
 exit(0);
}
