#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <sys\stat.h>
#include <dir.h>
#include <io.h>

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

struct DateSel {
	unsigned Date;
	int      Swt;
	};

int  Match (char *Str, char *Pat);
char *PrintNumber (long Number);
int Copy (INFO_BLOCK *Name);
int Move (INFO_BLOCK *Name);
void Execute (char *Name);
int CheckPath (QUE_DEF *Q, char *Name);
void FixDirRef (char *Name, char *Ref, char *Temp);

 int
Select (INFO_BLOCK *Name) {
	extern QUE_DEF Paths, IncNameQueue, ExcNameQueue;
	extern QUE_DEF IncPathQueue, ExcPathQueue;
	extern int AttrSwt, NotAttrSwt, SizeSwt, TreeSwt;
	extern char AttrMask, NotAttrMask;
	extern char *ExistRef, *NotExistRef, *CompRef, *NotCompRef;
	extern INFO_BLOCK *LT_Ref, *LE_Ref, *GT_Ref, *GE_Ref;
	extern INFO_BLOCK *LT_DateRef, *LE_DateRef, *GT_DateRef, *GE_DateRef;
	extern struct DateSel Since, Before;
	extern int BaseLength;
	extern long Size;

	QUE_ENTRY *t;
	char *p;
	int Result = 0;
	char Temp[66];
	struct ffblk DirBlk;

	if ( (p = strrchr(Name->Name, '\\')) != NULL) ++p;
	else p = Name->Name;
	if (IncNameQueue.Count) {
		for (t=IncNameQueue.Head; t != NULL; t = t->Next)
			if (Match(p, t->Body)) break;
		if (t == NULL) Result = 0;
		else Result = 1;
		}
	else Result = 1;
	if (ExcNameQueue.Count) {
		for (t=ExcNameQueue.Head; t != NULL; t = t->Next)
			if (Match(p, t->Body)) {
				Result = 0;
				break;
				}
		}
	if (IncPathQueue.Count) {
		if (CheckPath(&IncPathQueue, Name->Name)) Result = 1;
		}
	if (ExcPathQueue.Count) {
		if (CheckPath(&ExcPathQueue, Name->Name)) Result = 0;
		}
	if (ExistRef != NULL) {
		if ( (Name->Attrib & (FA_DIREC | FA_LABEL)) == 0 )  {
			FixDirRef(Name->Name, ExistRef, Temp);
			if (findfirst(Temp, &DirBlk, 0xFF)) Result = 0;
			else if ( (DirBlk.ff_attrib & FA_DIREC) != 0) Result = 0;
			}
		}
	if (NotExistRef != NULL) {
		if ( (Name->Attrib & (FA_DIREC | FA_LABEL)) == 0 )  {
			FixDirRef(Name->Name, NotExistRef, Temp);
			if ( !findfirst(Temp, &DirBlk, 0xFF)
			   && ( (DirBlk.ff_attrib & FA_DIREC) == 0) ) Result = 0;
		   }
		}
	if (CompRef != NULL) {
		if ( (Name->Attrib & (FA_DIREC | FA_LABEL)) == 0 ) {
			FixDirRef(Name->Name, CompRef, Temp);
			if ( findfirst(Temp, &DirBlk, 0xFF)
				|| ((DirBlk.ff_attrib & FA_DIREC)) == 0 ) Result = 0;
			else
				if ( (Name->Size != DirBlk.ff_fsize)
					 || (Name->FDate.Date != DirBlk.ff_fdate)
					 || (Name->FTime.Time != DirBlk.ff_ftime)
				   ) Result = 0;
		   }
	   }
	if (NotCompRef != NULL) {
		if ( (Name->Attrib & (FA_DIREC | FA_LABEL)) == 0 ) {
			FixDirRef(Name->Name, NotCompRef, Temp);
			if ( findfirst(Temp, &DirBlk, 0xFF)
				&& ((DirBlk.ff_attrib & FA_DIREC)) == 0 ) Result = 0;
			else
				if ( (Name->Size != DirBlk.ff_fsize)
					 && (Name->FDate.Date != DirBlk.ff_fdate)
					 && (Name->FTime.Time != DirBlk.ff_ftime)
				   ) Result = 0;
		   }
	   }
	if (Result == 0) return(0);
	if (AttrSwt)
		if ( (Name->Attrib & AttrMask) == 0) return(0);
	if (NotAttrSwt)
		if ( (Name->Attrib & NotAttrMask) != 0) return(0);
	if (Since.Swt)
		if (Name->FDate.Date < Since.Date) return(0);
	if (Before.Swt)
		if (Name->FDate.Date >= Before.Date) return(0);
	if (SizeSwt) {
		if (SizeSwt < 2 && Name->Size < Size) return(0);
		if (SizeSwt > 1 && Name->Size > Size) return(0);
		}
	if (LT_Ref != NULL) {
		if (Name->FDate.Date >= LT_Ref->FDate.Date) return(0);
		if (Name->FDate.Date == LT_Ref->FDate.Date
			&& Name->FTime.Time >= LT_Ref->FTime.Time) return(0);
		}
	if (LE_Ref != NULL) {
		if (Name->FDate.Date > LE_Ref->FDate.Date) return(0);
		if (Name->FDate.Date == LE_Ref->FDate.Date
			&& Name->FTime.Time > LE_Ref->FTime.Time) return(0);
		}
	if (GT_Ref != NULL) {
		if (Name->FDate.Date <= GT_Ref->FDate.Date) return(0);
		if (Name->FDate.Date == GT_Ref->FDate.Date
			&& Name->FTime.Time <= GT_Ref->FTime.Time) return(0);
		}
	if (GE_Ref != NULL) {
		if (Name->FDate.Date < GE_Ref->FDate.Date) return(0);
		if (Name->FDate.Date == GE_Ref->FDate.Date
			&& Name->FTime.Time < GE_Ref->FTime.Time) return(0);
		}
	if (LT_DateRef != NULL)
		if (Name->FDate.Date >= LT_DateRef->FDate.Date) return(0);
	if (LE_DateRef != NULL)
		if (Name->FDate.Date > LE_DateRef->FDate.Date) return(0);
	if (GT_DateRef != NULL)
		if (Name->FDate.Date <= GT_DateRef->FDate.Date) return(0);
	if (GE_DateRef != NULL)
		if (Name->FDate.Date < GE_DateRef->FDate.Date) return(0);
	return(Result);
	}

 void
Action (INFO_BLOCK *Name) {
	extern char AttrOnMask, AttrOffMask;
	extern int PrtSwt, LsSwt, LdSwt, RmSwt, RmSwtA, SumSwt;
	extern int AttrOnSwt, AttrOffSwt, CopySwt, MoveSwt, ExecSwt, RelSwt;
	extern long TotalSize;

	static char *MonthTab[] = {
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
		"Oct", "Nov", "Dec"
		};

	if (SumSwt) TotalSize += Name->Size;
	if (PrtSwt) printf("%s\n", (RelSwt) ? &Name->Name[BaseLength] : Name->Name);
	if (LsSwt || LdSwt) {
		printf("%c%c%c%c%c%c",
			Name->Attrib & FA_ARCH   ? 'a' : '-',	/* for the archive bit being on */
			Name->Attrib & FA_DIREC  ? 'd' : '-',	/* for the directory bit being on */
			Name->Attrib & FA_LABEL  ? 'v' : '-',	/* for the file being a volume label */
			Name->Attrib & FA_SYSTEM ? 's' : '-',	/* the for file being a system file */
			Name->Attrib & FA_HIDDEN ? 'h' : '-',	/* for the file being hidden */
			Name->Attrib & FA_RDONLY ? '-' : 'w'	/* for the file being read-only */
			);
		if (Name->Attrib & FA_DIREC) printf("                  ");
		else printf(" %16s ", PrintNumber(Name->Size));
		if (LsSwt) printf("%2u-%3s-%02u ", Name->FDate.d.day,
			MonthTab[Name->FDate.d.month - 1], Name->FDate.d.year+80);
		else printf("%02u-%02u-%02u ", Name->FDate.d.year+80,
			Name->FDate.d.month, Name->FDate.d.day);
		printf("%2u:%02u %s\n", Name->FTime.t.Hour, Name->FTime.t.Minute,
			Name->Name);
		}
	if (AttrOnSwt) {
		printf("ATTR_ON: %s\n", Name->Name);
		_chmod(Name->Name, 1, AttrOnMask | Name->Attrib);
		}
	if (AttrOffSwt) {
		printf("ATTR_OFF: %s\n", Name->Name);
		_chmod(Name->Name, 1, ~AttrOffMask & Name->Attrib);
		}
	if ( (RmSwt || RmSwtA)
			&& !( (Name->Attrib & FA_DIREC) || (Name->Attrib & FA_LABEL) ) ) {
		printf("RM: %s\n", Name->Name);
		if (unlink(Name->Name)) {
			if (!RmSwtA) perror("    FAILED");
			else {
				chmod(Name->Name, S_IREAD | S_IWRITE);
				if (unlink(Name->Name)) perror("    FAILED");
				}
			}
		}
	if ( (CopySwt)
			&& !( (Name->Attrib & FA_DIREC) || (Name->Attrib & FA_LABEL) ) ) {
		Copy(Name);
		}
	if ( (MoveSwt)
			&& !( (Name->Attrib & FA_DIREC) || (Name->Attrib & FA_LABEL) ) ) {
		Move(Name);
		}
	if (ExecSwt) {
		Execute(Name->Name);
		}
	}

 int
CheckPath (QUE_DEF *Q, char *Name) {
	QUE_ENTRY *t;
	char *p1, *p2;
	int Result = 0;

	for (t=Q->Head; t != NULL; t = t->Next) {
		p1 = strrchr(Name, '\\');
		p2 = strrchr(t->Body, '\\');
		if (p1 - Name == p2 - t->Body)
			if (Match(Name, t->Body)) {
				Result = 1;
				break;
				}
		}
	return(Result);
	}

 void
FixDirRef (char *Name, char *Ref, char *Temp) {
	extern int BaseLength, TreeSwt;

	char *p;

	if ( (p = strrchr(Name, '\\')) == NULL) p = Name;
	else ++p;
	strcpy(Temp, Ref);
	if (TreeSwt) strcat(Temp, &Name[BaseLength]);
	else strcat(Temp, p);
	}