
import shutil
import vs
import os

Major, Minor, Maintenance, Platform = vs.GetVersion()

#Source Installation Folder
SrcFldFolder = outputFolder
# SrcFldFolder = 'D:\\Downloads\\Source\\'

#Company Name
kCompanyName 		= 'Landru Design'

#Source Content Folders
kPluginsSrcFld		= 'Plug-ins'
kLibrariesSrcFld	= 'Libraries'
kManualsSrcFld		= 'Landru Manuals'
kUtilitiesSrcFld	= 'Landru Utilities'

kAccess = 0o755

#Target Content Folders
kAppPlugInsFld		= vs.GetFolderPath(2)		# 'Plug-ins'
kPlugInsDestFld		= vs.GetFolderPath(-2)		# 'Plug-ins'
kLibrariesDestFld	= vs.GetFolderPath(-13)		# 'Libraries'
kDefaultsDestFld	= vs.GetFolderPath(-14)		# 'Libraries\Defaults\'
kVSDestFld			= vs.GetFolderPath(-144)	# 'Libraries\Defaults\Video Screen' 
kVSCRT43DestFld		= vs.GetFolderPath(-145)	# 'Libraries\Defaults\Video Screen\Casings CRT 4-3'
kVSCRT169DestFld	= vs.GetFolderPath(-146)	# 'Libraries\Defaults\Video Screen\Casings CRT 16-9'
kVSFlat43DestFld	= vs.GetFolderPath(-147)	# 'Libraries\Defaults\Video Screen\Casings Flat 4-3'
kVSFlat169DestFld	= vs.GetFolderPath(-148)	# 'Libraries\Defaults\Video Screen\Casings Flat 16-9'
kVSCrv43DestFld		= vs.GetFolderPath(-149)	# 'Libraries\Defaults\Video Screen\Casings CRT Curved 4-3'
kVSImagesDestFld	= vs.GetFolderPath(-150)	# 'Libraries\Defaults\Video Screen\Screen Images'
kVSPJsDestFld		= vs.GetFolderPath(-151)	# 'Libraries\Defaults\Video Screen\Projector Models'
kVSStandsDestFld	= vs.GetFolderPath(-152)	# 'Libraries\Defaults\Video Screen\Stand Models'
kSGCustDestFld		= vs.GetFolderPath(-203)	# 'Libraries\Defaults\Soft Goods - Custom Textures\'
if Major > 25:
	kATSBmpNewDestFld	= vs.GetFolderPath(-348)	# 'Libraries\Objects - Ent Audio\Bumpers'
	kATSSpkrNewDestFld	= vs.GetFolderPath(-347)	# 'Libraries\Objects - Ent Audio\Speakers'
else:
	kATSBmpNewDestFld	= os.path.join(kLibrariesDestFld, 'Objects - Ent Audio','Bumpers')
	kATSSpkrNewDestFld	= os.path.join(kLibrariesDestFld, 'Objects - Ent Audio','Speakers')

# Destination folders
kManualsDestFld		= 'Landru Manuals'
kUtilitiesDestFld	= 'Landru Utilities'
kToolsFolder		= 'Landru Tools'

# List of Landru Design tools
ToolList = []
ToolList = [	'AudioArray 2.vso',
				'AudioBox 2.vso',
				'AudioBumper 2.vso',
				'Connect Audio Records.vsm',
				'Lighting Angle.vso',
				'NoteBox.vso',
				'RigDatum.vso',
				'RigPoint Renumber.vsm',
				'RigPoint.vso',
				'SoftGoods 2.vso',
				'Convert To SoftGoods 2.vsm',
				'StageDeck 2.vso',
				'StagePlug 2.vso',
				'StageRamp 2.vso',
				'StageSteps 2.vso',
				'StageLift.vso',
				'Stanchion Layout.vso',
				'Convert To Stanchion Layout.vsm',
				'VideoTile.vsm',
				'VS4-Blended Projector.vso',
				'VS4-Blended Screen.vso',
				'VS4-LED.vso',
				'VS4-Projection.vso',
				'VS4-Television.vso',
				'VideoTile.vsm',
				'Check or Add Landru Keys.vsm',
				'Remove Landru Keys.vsm',
				'Resource Folder Names....vsm']

				
# >>>>>>>>>> IsLegitFile >>>>>>>>>>

def IsLegitFile(LocFileName):
	LocFileName = LocFileName.upper()
	if LocFileName[-4:] == '.VWX':
		IsValid = True
	elif LocFileName[-4:] == '.PDF':
		IsValid = True
	elif LocFileName[-4:] == '.VWR':
		IsValid = True
	elif LocFileName[-4:] == '.VSO':
		IsValid = True
	elif LocFileName[-4:] == '.VSM':
		IsValid = True
	else:
		IsValid = False
	return IsValid
	
# <<<<<<<<<< IsLegitFile <<<<<<<<<<


# >>>>>>>>>> SourceToDestPath >>>>>>>>>>
def SourceToDestPath(SourcePath):
	# Translates Source folder to Localized Destination
	if 'CRT 4-3' in SourcePath:
		LocalizedPath = kVSCRT43DestFld
	elif 'CRT 16-9' in SourcePath:
		LocalizedPath = kVSCRT169DestFld
	elif 'Flat 4-3' in SourcePath:
		LocalizedPath = kVSFlat43DestFld
	elif 'Flat 16-9' in SourcePath:
		LocalizedPath = kVSFlat169DestFld
	elif 'Curved 4-3' in SourcePath:
		LocalizedPath = kVSCrv43DestFld
	elif 'Screen Images' in SourcePath:
		LocalizedPath = kVSImagesDestFld
	elif 'Projector Models' in SourcePath:
		LocalizedPath = kVSPJsDestFld
	elif 'Stand Models' in SourcePath:
		LocalizedPath = kVSStandsDestFld
	elif 'Bumpers' in SourcePath:
		LocalizedPath = kATSBmpNewDestFld
	elif 'Speakers' in SourcePath:
		LocalizedPath = kATSSpkrNewDestFld
	elif 'Soft Goods' in SourcePath:
		LocalizedPath = kSGCustDestFld
	else:
		LocalizedPath = ''
	return LocalizedPath
# <<<<<<<<<< SourceToDestPath <<<<<<<<<<

	
# >>>>>>>>>> DistributeFiles >>>>>>>>>>
def DistributeFiles(LocalSourceFolder,LocalDestinationFolder,LocRemoveSource):
	# Moves files from source folders to User folders
	if 'Libraries' in LocalSourceFolder:
		if os.path.isdir(os.path.join(SrcFldFolder, LocalSourceFolder)):
			for (Root,DirList,FileList) in os.walk(os.path.join(SrcFldFolder, LocalSourceFolder)):
				for DirName in DirList:
					WholeFolderPath = SourceToDestPath(DirName)
					if WholeFolderPath:
						if not os.path.isdir(WholeFolderPath):
							try:
								os.makedirs(WholeFolderPath,kAccess)
							except:
								pass
			for (Root,DirList,FileList) in os.walk(os.path.join(SrcFldFolder, LocalSourceFolder)):
				for FileName in FileList:
					if IsLegitFile(FileName):
						try:
							shutil.move(os.path.join(Root,FileName),		os.path.join(SourceToDestPath(Root),FileName))
						except:
							pass
	else:
		SrceRtFldrNameLngth = len(LocalSourceFolder)
		if os.path.isdir(os.path.join(SrcFldFolder, LocalSourceFolder)):
			if not os.path.isdir(LocalDestinationFolder):
				try:
					os.makedirs(LocalDestinationFolder,kAccess)
				except:
					pass
					
			for (Root,DirList,FileList) in os.walk(os.path.join(SrcFldFolder, LocalSourceFolder)):
				for DirName in DirList:
					if not os.path.isdir(os.path.join(LocalDestinationFolder, DirName)):
						os.makedirs(os.path.join(LocalDestinationFolder,DirName),kAccess)
			
			for (Root,DirList,FileList) in os.walk(os.path.join(SrcFldFolder, LocalSourceFolder)):
				for FileName in FileList:
					SubFolder = Root[Root.index(LocalSourceFolder)+1+SrceRtFldrNameLngth:]
					while LocalSourceFolder in SubFolder:
						if SubFolder == LocalSourceFolder:
							SubFolder = ''
						else:
							SubFolder = SubFolder[SubFolder.index(LocalSourceFolder)+1+SrceRtFldrNameLngth:]
					if IsLegitFile(FileName):
						try:
							shutil.move(os.path.join(Root,FileName),		os.path.join(LocalDestinationFolder,SubFolder,FileName))
						except:
							pass
	if LocRemoveSource:
		try:
			shutil.rmtree(os.path.join(SrcFldFolder, LocalSourceFolder),ignore_errors=True)
		except:
			pass
# <<<<<<<<<< DistributeFiles <<<<<<<<<<


# >>>>>>>>>> AlignToolStructure >>>>>>>>>>
def AlignToolStructure(LocFolderToCheck,IsUserfolder):
	#  Checks Plug-ins folders for existing Landru tools 
	#  and moves them to "Landru Tools" in User folder   
	#  Plug-ins - Then, deletes newly-emptied folder(s)
	FolderList = []
	if IsUserfolder:
		# Moves existing Landru tools from misc. user "Plug-ins" folders into the user folder "Plug-ins/Landru Tools" folder
		for (Root,DirList,FileList) in os.walk(os.path.join(kPlugInsDestFld)):
			if Root != os.path.join(kPlugInsDestFld,kToolsFolder) and Root != os.path.join(kPlugInsDestFld,kPluginsSrcFld,kToolsFolder):
				for FileName in FileList:
					if FileName in ToolList:
						try:
							if IsLegitFile(FileName):
								shutil.move(os.path.join(Root,FileName),os.path.join(kPlugInsDestFld,kToolsFolder,FileName))
								FolderList.append(os.path.join(Root))
						except:
							pass
		else:
			# Moves existing Landru tools from misc. application "Plug-ins" folders into the user folder "Plug-ins/Landru Tools" folder	
			for (Root,DirList,FileList) in os.walk(os.path.join(kAppPlugInsFld)):
				for FileName in FileList:
					if FileName in ToolList:
						if IsLegitFile(FileName):
							try:
								shutil.move(os.path.join(Root,FileName),os.path.join(kPlugInsDestFld,kToolsFolder,FileName))
								FolderList.append(Root)
							except:
								pass					
	# Deletes newly-emptied folders
	if FolderList:
		for PotentialFolder in FolderList:
			if os.path.isdir(os.path.join(PotentialFolder)):
				if not os.listdir(os.path.join(PotentialFolder)):
					try:
						shutil.rmtree(os.path.join(PotentialFolder),ignore_errors=True)
					except:
						pass
# <<<<<<<<<< AlignToolStructure <<<<<<<<<<


# Creates "Landru Tools" in "Plug-ins" if it doesn't already exist		
if not os.path.isdir(os.path.join(kPlugInsDestFld,kToolsFolder)):
	os.makedirs(os.path.join(kPlugInsDestFld,kToolsFolder),kAccess)

# Moves existing tool files from Application and Obsolete User folders to "Landru Tools"		
AlignToolStructure(kAppPlugInsFld,	False)
AlignToolStructure(kPlugInsDestFld,	True)
	
# Moves files from installation folders to correct locations and deletes installation folders
DistributeFiles(kPluginsSrcFld,		os.path.join(kPlugInsDestFld), 								True)	# Plug-ins
DistributeFiles(kLibrariesSrcFld,	os.path.join(kLibrariesDestFld),							True)	# Libraries
DistributeFiles(kManualsSrcFld,		os.path.join(kPlugInsDestFld, 		kManualsDestFld),		False)	# Manuals
DistributeFiles(kUtilitiesSrcFld,	os.path.join(kPlugInsDestFld,		kUtilitiesDestFld), 	False)	# Utilities


