/*
   SCopy (C)1996 Martin Samuelsson
   Main module
*/
// Bevel: 570
#include <stdio.h>
#include <stdlib.h>
#include <exec/memory.h>
#include <exec/types.h>
#include <devices/timer.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <intuition/intuition.h>
#define testsize 1024
#define buffersize 1024

UBYTE *block;
ULONG blocksize, nrobjs, nwobjs, blox;
FILE *infile, *outfile;

extern struct Window         *Project0Wnd;
extern struct Screen         *Scr;
struct FileInfoBlock	*MyFIB;
struct EClockVal Time1;
struct EClockVal Time2;
ULONG eclock, totaltrans;
double temp, totaltime, lastupdate, scale, timeleft;
BPTR lock;
BOOL success, GoOn, NoOut;
int x2;

char OrkarInte[256];
char WindowTitle[80];
void MyError(unsigned char *string, int level);

void
MyError(unsigned char *string, int level)
{
 SetWindowTitles(Project0Wnd, (UBYTE *)string, (UBYTE *)-1);
 if(MyFIB != NULL) {
  FreeDosObject(DOS_FIB, MyFIB);
 }
 if(infile != NULL) {
  fclose(infile);
 }
 if(outfile != NULL) {
  fclose(outfile);
 }
 if(NoOut == TRUE) {
  DeleteFile(OrkarInte);
 }
 if(lock != NULL) {
  UnLock(lock);
 }
 if(level==20) {
  Delay(200);
 } else {
  Delay(25);
 }
 CloseProject0Window();
 CloseDownScreen();
 exit(level);
}

void
main(int argc, char **argv)
{
 ULONG hours;
 ULONG minutes;
 ULONG seconds;
 sprintf(OrkarInte,argv[2]);
 SetupScreen();
 OpenProject0Window();
 if(Project0Wnd == NULL) {
  CloseDownScreen();
  Exit(20);
 }
 MyFIB = (struct FileInfoBlock *)AllocDosObjectTags(DOS_FIB, TAG_DONE);
 if(MyFIB == NULL) {
  MyError("SCopy: FileInfoBlock error.",20);
 }
 lock  = Lock( argv[1], ACCESS_READ );
 if(lock == NULL) {
  MyError("SCopy: Could not lock input file.",20);
 }
 success = Examine(lock, MyFIB);
 if(success == NULL) {
  MyError("SCopy: Could not examine input file.",20);
 }
 if(MyFIB->fib_DirEntryType > 0L) {
  MyError("SCopy: Will not copy directories.",20);
 }
 block = malloc(buffersize);
 if(block == NULL) {
  MyError("SCopy: Memory allocation error.",20);
 }
 infile = fopen(argv[1],"rb");
 if(infile == NULL) {
  MyError("SCopy: Could not open input file for reading.",20);
 }
 outfile = fopen(argv[2],"wb");
 if(outfile == NULL) {
  MyError("SCopy: Could not open output file for writing.",20);
 }
 SetAPen(Project0Wnd->RPort,3);
 lastupdate = -1;
 GoOn = TRUE;
 NoOut = FALSE;
 while(GoOn == TRUE) {
  ReadEClock(&Time1);
  nrobjs = fread(block, 1, buffersize, infile);
  if(nrobjs>0) {
   nwobjs = fwrite(block, 1, nrobjs, outfile);
   if(nwobjs != nrobjs) {
    NoOut = TRUE;
    MyError("SCopy: Write error.",20);
   }
  }
  if(nwobjs != buffersize) {
   GoOn = FALSE;
  }
  blox++;
  eclock = ReadEClock(&Time2);
  totaltime=totaltime+(double)(Time2.ev_lo-Time1.ev_lo)/eclock;
  totaltrans=totaltrans+nwobjs;
  if(totaltime - lastupdate > 1) {
   lastupdate = totaltime;
   timeleft = (double)((MyFIB->fib_Size/buffersize)/(totaltrans/totaltime/1024))-totaltime;
   hours = (ULONG)timeleft/3600;
   minutes = ((ULONG)timeleft%3600)/60;
   seconds = (ULONG)timeleft%60;
   sprintf(WindowTitle, "SCopy 1.0 - Block %d of %d: Approx. %02d:%02d:%02d (h:m:s) remaining, %1.3lf K/s.",
         blox,MyFIB->fib_Size/buffersize,hours,minutes,seconds,totaltrans/totaltime/1024);
   scale = (double)568/(MyFIB->fib_Size/buffersize);
   x2 = blox*scale;
   if(x2>568) x2=568;
   if(x2<1) x2=1;
   SetWindowTitles(Project0Wnd, (UBYTE *)WindowTitle, (UBYTE *)-1);
   RectFill(Project0Wnd->RPort,7,13,7+x2,22);
  }
  HandleProject0IDCMP();
 }
 MyError("SCopy 1.0 (C) 1996 Martin Samuelsson",0);
}
