#define MAXCOMP 7
#define MINTRACKLEN 6200
/* Will assume that every Track has *at least* MINTRACKLEN words!!*/
#define MINAMIGALEN 6300
/* Will assume that all AMIGAs are able to hold MINAMIGALEN words per track */
#define MAXAMIGALEN 6380
/* Will assume that no AMIGA is able to hold more than MAXAMIGALEN words */
#define TRASH 0
/* Will assume that the last read TRASH bytes are actually trash.
   Can be 20 for MSDos, Should be 0 (or better negative) for AMIGA disks.
   Be Aware: The new Disk must have AT LEAST a capacity of (old capacity-TRASH).*/

int Drive=0;
main(argc,argv)
int argc;
char **argv;
{
unsigned short *TrackBuffer;
char c;
int track,head;
int count;
register int i,j;
int fromdrive=0;
int todrive=1;
int first=0,second=0;
int max=0,min=100000;
int emptysym=StandardMfmEnc(0); /* Fill the empty spaces with 0s! */
unsigned short *ptr;
int found,attempt,tries;

InitMotor();
TrackBuffer=(unsigned short *)AllocChipMem(2*(TrackLen+2000))+2000;
for (i=-2000;i<0;i++) TrackBuffer[i]=emptysym;

OnceAgain:

printf("\n\nThis Prog will copy a disk from Drive 0 to Drive 1.\n");
printf("If you are sure that this is what you want to do, please type 'y'\n");
printf("after inserting source- and destination-disks in the respective drives.\n");
printf("To quit, type anything else.\n");

do
c=getchar();
while (c=='\n');

if (toupper(c)!='Y') {printf("Poooh... Finishing.\n");_abort();}

max=0;
min=10000;
for (track=0;track<84;track++)
for (head=0;head<2;head++)
 {
	attempt=0;
	NextAttempt:
	tries=0;
	NextTry:
	ReadTrack(TrackBuffer,fromdrive,track,head);
	first=0;
	second=0;
	found=0;
	TrackBuffer[-1]=emptysym;
	TrackBuffer[-2]=emptysym;
	TrackBuffer[-3]=emptysym;
	for (i=0;i<MAXAMIGALEN;i++)
	 if (TrackBuffer[i]==0x4489)
	 {
	  while (TrackBuffer[i]==SYNC) i++;
	  TrackBuffer[i-1]=SYNC;
	  TrackBuffer[i-2]=SYNC;
	  TrackBuffer[i-3]=SYNC;

	  /* Now test if a DataSector has been read in first: */
	  if (first)
	   if (second==0)
	    {second=-1;
		 if ((tries<3)&&(i-first>150))
		  goto NextTry;}

	  if (first==0) first=i;
	  if (i>MINTRACKLEN)
	  {
	  for (j=0;j<MAXCOMP;j++)
	   if (TrackBuffer[i+j]!=TrackBuffer[first+j]) goto none;
	  if (i>max) max=i;
	  if (i<min) min=i;
	  found=-1;
	  goto done;
	  }
	 none:;
	 }
	done:
    if (found==0)
		{
		tries++;
		if (tries!=4) goto NextTry;
		if (attempt==4)
		 aprintf(stderr,"Giving up this Track.\n");
		else
		 {attempt++;
		  aprintf(stderr,"Read Error on Track %ld Head %ld!\n",track,head);
		  goto NextAttempt;}
		}
	else
		{
		 i=i-3;
		 if (i>MINAMIGALEN) i-=TRASH;
		 /* Make sure that the first Bytes are actually written out,
		    hoping that the last TRASH Bytes are trash.This is *surely*
			wrong for AMIGA-disks.*/
		 ptr=TrackBuffer+i-TrackLen;
		 WriteTrack(ptr,todrive,track,head);
		}
 }
printf("Statistics:    Shortest Track: %ld  Longest Track: %ld\n",min,max);
printf("Copy finished.\n");
goto OnceAgain;
}
