#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <alloc.h>
#include <conio.h>
#include <ctype.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>
#include <sys\stat.h>

#include "nufind.h"
#include "queue.h"

void            ErrorMsg(char *Format,...);
void            ExceptionMsg(char *Format,...);
int             DoCopy(char *Src, char *Dest);
int             GetYorN(char *Message,...);
int             MakeDir(char *Name);
int				CheckPath (QUE_DEF *Q, char *Name);

 int
Copy (INFO_BLOCK *Name) {
	extern int      TreeSwt, CopySwt;
	extern int      BaseLength;
	extern char     DestDir[];
	extern QUE_DEF	NoCreateQ;

	char           *p;
	char            Dest[65];

	if ((Name->Attrib & (FA_DIREC | FA_LABEL)) != 0) return (1);
	strcpy(Dest, DestDir);
	if (Dest[strlen(Dest) - 1] != '\\') strcat(Dest, "\\");
	if (TreeSwt) strcat(Dest, &Name->Name[BaseLength]);
	else {
		p = strrchr(Name->Name, '\\');
		strcat(Dest, p + 1);
		}
	if (!strcmp(Name->Name, Dest)) return (0);
	if (NoCreateQ.Count != 0)
		if ( CheckPath(&NoCreateQ, Dest) != 0 ) return(0);
	printf("%s: %s TO %s\n", (CopySwt) ? "COPY" : "MOVE", Name->Name, Dest);
	if (!access(Dest, 0))
		if (!GetYorN("Destination file exists: Overwrite ")) return (1);
	return( DoCopy(Name->Name, Dest) );
	}

 int
Move (INFO_BLOCK *Name) {
	extern char     DestDir[];

	char           *p;
	char            Dest[65];
	int             Result;

	if ((Name->Attrib & (FA_DIREC | FA_LABEL)) != 0) return (0);
	if (Name->Name[0] != DestDir[0]) {
		if (!Copy(Name)) unlink(Name->Name);
		return (0);
		}
	strcpy(Dest, DestDir);
	if (Dest[strlen(Dest) - 1] != '\\') strcat(Dest, "\\");
	if (TreeSwt) strcat(Dest, &Name->Name[BaseLength]);
	else {
		p = strrchr(Name->Name, '\\');
		strcat(Dest, p + 1);
		}
	printf("MOVE: %s TO %s\n", Name->Name, Dest);
	if (!access(Dest, 0)) {
		if (!GetYorN("Destination file exists: Overwrite ")) return (1);
		unlink(Dest);
		}
	if ( (Result = MakeDir(Dest)) != 0 ) {
		if (Result == 2) return(-1);
		ExceptionMsg("Cannot create destination directory: %s", Dest);
		return (-1);
		}
	if ((Result = rename(Name->Name, Dest)) != 0)
		ExceptionMsg("FAILED");
	return (Result);
	}


 int
DoCopy (char *Src, char *Dest) {
	extern          errno;
	extern int		OldDate, CopySwt;

	unsigned        BuffSize;
	char           *BuffPtr;
	int             Sf, Df;
	int             InCount, OutCount;
	int				Result;
	struct ftime	DateTime;

	if (coreleft() > 32 * 1024 - 1) BuffSize = 32 * 1024 - 1;
	else if ( (BuffSize = (unsigned) (coreleft() - 100) ) < 512) {
		ErrorMsg("Insufficient memory for COPY buffer!\n");
		return (-1);
		}

	if ((BuffPtr = malloc(BuffSize)) == NULL) {
		ErrorMsg("Insufficient memory for COPY buffer!\n");
		return (-1);
		}

	if ((Sf = open(Src, O_RDONLY | O_BINARY)) == 0) {
		ExceptionMsg("Open error on source file: %s", Src);
		free(BuffPtr);
		}
	if ( (Result = MakeDir(Dest)) != 0 ) {
		if (Result != 2)
			ExceptionMsg("Cannot create destination directory: %s", Dest);
		close(Sf);
		free(BuffPtr);
		return (-1);
		}
	if ((Df = open(Dest, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE)) < 0) {
		ExceptionMsg("Open error on destination file: %s", Dest);
		close(Sf);
		free(BuffPtr);
		return (-1);
		}

	while ((InCount = read(Sf, BuffPtr, BuffSize)) > 0) {
		OutCount = write(Df, BuffPtr, InCount);
		if (OutCount < 0 || OutCount != InCount) {
			ExceptionMsg("Write error on %s ", Dest);
			close(Sf);
			close(Df);
			unlink(Dest);
			free(BuffPtr);
			return (-1);
			}
		}

	if (InCount < 0) {
		ExceptionMsg("Read error on %s", Src);
		close(Sf);
		close(Df);
		unlink(Dest);
		free(BuffPtr);
		return (-1);
		}

	if (OldDate || !CopySwt) {
		getftime(Sf, &DateTime);
		setftime(Df, &DateTime);
		}
	close(Sf);
	close(Df);
	free(BuffPtr);
	return (0);
	}
