#include "string.h"
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
#define FMXSIZE (FMSIZE+FMSIZE+11)

char src_path[FMSIZE],dst_path[FMSIZE];
unsigned char Diff_Drives;
extern int msflag;

main(argc,argv)
char *argv[];
int argc;
{
	char *source;
	int Error,z;
	struct FILEINFO *info,ispace;
	void chkabort();

	if (argc!=3)
	{
		printf("\t<Move> version 2.4 by Doug Tittle\n\n");
		printf("\tCorrect usage is \"Move {oldfilename} {newfilename}\"\n\n");
		printf("\tThe oldfilename may use wildcard characters:\n");
		printf("\t    \"*\" - meaning \"Anything or Nothing\" matches\n");
		printf("\t    \"#?\" - also meaning \"Anything or Nothing\" matches\n");
		printf("\t    \"?\" - meaning \"Any Single Character\" matches\n");
		printf("\tOnly the PATH part of the newfilename is used!\n");
        exit(10);
    }

	Error=0;
	info=&ispace;

	(void) stcgfp(src_path,argv[1]);
	(void) stcgfp(dst_path,argv[2]);

	if ((strnicmp(argv[1],argv[2],4)==0) ||
	((strchr(argv[1],':')==NULL) && (strchr(argv[2],':')==NULL)))
	{
		Diff_Drives=0;
	}
	else
		Diff_Drives=1;

	z=strlen(src_path)-1;
	if ((src_path[z]!=':') && (z>0))
	{
		src_path[z+1]='/';
		src_path[z+2]='\0';
	}

	z=strlen(dst_path)-1;
	if ((dst_path[z]!=':') && (z>0))
	{
		dst_path[z+1]='/';
		dst_path[z+2]='\0';
	}

	if (strchr(argv[1],'#')==NULL)
		msflag=4;
	else
		msflag=0;
	
	if ((stricmp(src_path,dst_path)) == 0)
	{
		printf("Source and Destination Paths are the SAME!\n");
		printf("Useless Move Action Skipped.\n\n");
		exit(10);
	}	

	if (dfind(info,argv[1],1)==0)
	{
		if ((info->fib_DirEntryType)<0L)
		{
			source=(char *) (&(info->fib_FileName));
			chkabort();
			(void) Moveit(source);
		}
		else
		{
			printf("%s is a Directory!\n",argv[1]);
			exit(10);
		}
		while (dnext(info)==0)
		{
			if ((info->fib_DirEntryType)<0L)
			{
				source=(char *) (&(info->fib_FileName));
				chkabort();
				(void) Moveit(source);
			}
		}
	}			
	else
	{
		printf("Found no matching files for %s.\n",argv[1]);
	}

	return(Error);
}

Moveit(source)
char *source;
{
	int syserror;
    static char copy[]="Copy ", to[]=" TO ",delete[]="Delete ";
	static char rename[]="Rename ";
	char cmd[FMXSIZE],src[FMSIZE],dst[FMSIZE];

	(void) strcpy(src,src_path);
	(void) strcat(src,source);

	(void) strcpy(dst,dst_path);
	(void) strcat(dst,source);

	if (Diff_Drives==1)
	(void) strcpy(cmd,copy);
	else
	(void) strcpy(cmd,rename);

	(void) strcat(cmd,src);
	(void) strcat(cmd,to);
	(void) strcat(cmd,dst);

	syserror=0;

	if ((syserror=system(cmd))==0)
	{
		if (Diff_Drives==1)
		{
			printf("Copied File:  %s ==> %s\n",src,dst);
			(void) strcpy(cmd,delete);
	    	(void) strcat(cmd,src);
			if ((syserror=system(cmd))==0)
			{
				printf("Removed File: %s\n\n",src);
			}
		}
		else
			printf("Moved File:  %s ==> %s\n",src,dst);
	}
	return(syserror);
}


