#  mv <file> <destination>
#    Description: moves a file from its source to the given destination.
#      The file can also be a mask, such as /incoming/*, provided the
#      destination is a directory.
#    Returns: If the command was successful, the number of files moved will
#      be returned. Otherwise, a negative number will be returned:
#        -1 invalid source file
#        -2 invalid destination
#        -3 destination file exists
#        -4 no matches found
#    Module: filesys
proc ::tcldrop::filesys::mv {file dest} {
	global incoming-path
	set relfile $file
	set reldest $dest
	#need to normalize the $dir first to make sure they aren't trying to move something that they shouldn't be able to.
	
	#check if a mask was provided as file and a directory as the dest
	if {[string first * [file tail $file]] == -1} {
		#no mask
	} else {
		#make sure dest is a dir
		if {[catch [file normalize [file join ${incoming-path} [file normalize $dest]]] dest]} {
			#could not normalize - invalid destination
			return -2
		} else {
			if {[file pathtype $file] == "relative"} {
				# no need to normalize file by itself - just file/mask
				if {[catch {file normalize [file join ${incoming-path} $file]} file]} {
					# could not normalize source for some reason
					return -1
				} else {
					# make sure files are within bounds
					if {[string compare -nocase [file normalize ${incoming-path}] $file] == 1} {
						return -1
					} elseif {[string compare -nocase [file normalize ${incoming-path}] $dest] == 1} {
						return -2
					} else {
						# now make sure dest dir exists
						if {[file isdirectory $dest]} {
							# get source file list using glob (containing each path for each file)
							set filelist [glob -nocomplain -type f -- $file]
							if {[llength $filelist] == 0} {
								#couldn't find any files matching the file mask
								return -4
							} else {
								set trampling 0
								foreach filepath $filelist {
									if {[catch {file rename $filepath $dest} err] && [string equal [string range $a end-5 end] "exists"]} {
										# at least one of the files already existed (skip)
										set trampling 1
									}
								}
								if {$trampling == 1} {
									return -3
								} else {
									return 0
								}
							}
						} else {
							return -2
						}
					}
				}
			} else {
				# normalize
				if {[catch [file normalize [file join ${incoming-path} [string trimleft [file normalize $file] /]]] file]} {
					#could not normalize - invalid source
					return -1
				} else {
					#make sure it is a valid path ie /blah/*.txt
					#/blah.*
					#C:/blah.*
					#/abs/*
					if {[string compare -nocase [file normalize ${incoming-path}] $file] == 1} {
						# trying to move a source outside of allowed bounds so use relative root
						set file [file normalize [file join ${incoming-path} $file]]
						#for all *relative paths to be normalized place a / before normalizing
						
						
					} else {
						if {[string compare -nocase [file normalize ${incoming-path}] $dest] == 1} {
							#tried to move to a destination outside of allowed bounds
							
						} else {
							
						}
					}
					glob -nocomplain -directory / -tails -- *.txt
				}
			}
		}
	}
}