# vim: set ts=2 sw=2 tw=99 noet ft=python: 
import os
import shutil
import ambuild.osutil as osutil
from ambuild.command import Command

job = AMBuild.AddJob('package')

class DestroyPath(Command):
	def __init__(self, folder):
		Command.__init__(self)
		self.folder = folder

	def destroy(self, path):
		entries = os.listdir(path)
		for entry in entries:
			newpath = os.path.join(path, entry)
			if os.path.isdir(newpath):
				self.destroy(newpath)
				os.rmdir(newpath)
			elif os.path.isfile(newpath):
				os.remove(newpath)

	def run(self, runner, job):
		runner.PrintOut('rm -rf {0}/*'.format(self.folder))
		self.destroy(self.folder)

class CreateFolders(Command):
	def __init__(self, folders):
		Command.__init__(self)
		self.folders = folders
	
	def run(self, runner, job):
		for folder in self.folders:
			path = os.path.join(*folder)
			runner.PrintOut('mkdir {0}'.format(path))
			os.makedirs(path)

#Shallow folder copy
class CopyFolder(Command):
	def __init__(self, fromList, toList, excludes = []):
		Command.__init__(self)
		self.fromPath = os.path.join(AMBuild.sourceFolder, *fromList)
		self.toPath = os.path.join(*toList) 
		self.excludes = excludes
	
	def run(self, runner, job):
		entries = os.listdir(self.fromPath)
		for entry in entries:
			if entry in self.excludes:
				continue
			path = os.path.join(self.fromPath, entry)
			if not os.path.isfile(path):
				continue
			runner.PrintOut('copy {0} to {1}'.format(path, self.toPath))
			shutil.copy(path, self.toPath)

#Single file copy
class CopyFile(Command):
	def __init__(self, fromFile, toPath):
		Command.__init__(self)
		self.fromFile = fromFile
		self.toPath = toPath

	def run(self, runner, job):
		runner.PrintOut('copy {0} to {1}'.format(self.fromFile, self.toPath))
		shutil.copy(self.fromFile, self.toPath)


folders = [['addons', 'sourcemod', 'bin'],
           ['addons', 'sourcemod', 'plugins', 'disabled'],
           ['addons', 'sourcemod', 'gamedata'],
           ['addons', 'sourcemod', 'gamedata', 'core.games'],
		   ['addons', 'sourcemod', 'gamedata', 'sdkhooks.games'],
           ['addons', 'sourcemod', 'gamedata', 'sdktools.games'],
		   ['addons', 'sourcemod', 'gamedata', 'sm-cstrike.games'],
           ['addons', 'sourcemod', 'configs', 'geoip'],
           ['addons', 'sourcemod', 'translations'],
           ['addons', 'sourcemod', 'logs'],
           ['addons', 'sourcemod', 'extensions'],
           ['addons', 'sourcemod', 'data'],
           ['addons', 'sourcemod', 'scripting', 'include'],
           ['addons', 'sourcemod', 'scripting', 'admin-flatfile'],
           ['addons', 'sourcemod', 'scripting', 'adminmenu'],
           ['addons', 'sourcemod', 'scripting', 'testsuite'],
           ['cfg', 'sourcemod'],
           ['addons', 'sourcemod', 'configs', 'sql-init-scripts'],
           ['addons', 'sourcemod', 'configs', 'sql-init-scripts', 'mysql'],
           ['addons', 'sourcemod', 'configs', 'sql-init-scripts', 'sqlite'],
           ['addons', 'sourcemod', 'scripting', 'basecommands'],
           ['addons', 'sourcemod', 'scripting', 'basecomm'],
           ['addons', 'sourcemod', 'scripting', 'funvotes'],
           ['addons', 'sourcemod', 'scripting', 'basevotes'],
           ['addons', 'sourcemod', 'scripting', 'basebans'],
           ['addons', 'sourcemod', 'scripting', 'funcommands'],
           ['addons', 'sourcemod', 'scripting', 'playercommands'],
           ['addons', 'metamod'],
          ]

#Setup
job.AddCommand(DestroyPath(os.path.join(AMBuild.outputFolder, 'package')))
job.AddCommand(CreateFolders(folders))

#Copy Folders
job.AddCommand(CopyFolder(['configs'], ['addons', 'sourcemod', 'configs']))
job.AddCommand(CopyFolder(['configs', 'geoip'], ['addons', 'sourcemod', 'configs', 'geoip']))
job.AddCommand(CopyFolder(['configs', 'cfg'], ['cfg', 'sourcemod']))
job.AddCommand(CopyFolder(['configs', 'metamod'], ['addons', 'metamod']))
job.AddCommand(CopyFolder(['configs', 'sql-init-scripts'],
	['addons', 'sourcemod', 'configs', 'sql-init-scripts']))
job.AddCommand(CopyFolder(['configs', 'sql-init-scripts', 'mysql'],
	['addons', 'sourcemod', 'configs', 'sql-init-scripts', 'mysql']))
job.AddCommand(CopyFolder(['configs', 'sql-init-scripts', 'sqlite'],
	['addons', 'sourcemod', 'configs', 'sql-init-scripts', 'sqlite']))
job.AddCommand(CopyFolder(['gamedata'], ['addons', 'sourcemod', 'gamedata']))
job.AddCommand(CopyFolder(['gamedata', 'sdkhooks.games'],
	['addons', 'sourcemod', 'gamedata', 'sdkhooks.games']))
job.AddCommand(CopyFolder(['gamedata', 'sdktools.games'],
	['addons', 'sourcemod', 'gamedata', 'sdktools.games']))
job.AddCommand(CopyFolder(['gamedata', 'core.games'],
	['addons', 'sourcemod', 'gamedata', 'core.games']))
job.AddCommand(CopyFolder(['gamedata', 'sm-cstrike.games'],
	['addons', 'sourcemod', 'gamedata', 'sm-cstrike.games']))
job.AddCommand(CopyFolder(['plugins'], ['addons', 'sourcemod', 'scripting'], ['AMBuilder']))
job.AddCommand(CopyFolder(['plugins', 'include'],
	['addons', 'sourcemod', 'scripting', 'include']))
job.AddCommand(CopyFolder(['translations'], ['addons', 'sourcemod', 'translations']))
job.AddCommand(CopyFolder(['public', 'licenses'], ['addons', 'sourcemod']))
job.AddCommand(CopyFolder(['plugins', 'admin-flatfile'],
	['addons', 'sourcemod', 'scripting', 'admin-flatfile']))
job.AddCommand(CopyFolder(['plugins', 'adminmenu'],
	['addons', 'sourcemod', 'scripting', 'adminmenu']))
job.AddCommand(CopyFolder(['plugins', 'testsuite'],
	['addons', 'sourcemod', 'scripting', 'testsuite']))
job.AddCommand(CopyFolder(['plugins', 'basecommands'],
	['addons', 'sourcemod', 'scripting', 'basecommands']))
job.AddCommand(CopyFolder(['plugins', 'basecomm'],
	['addons', 'sourcemod', 'scripting', 'basecomm']))
job.AddCommand(CopyFolder(['plugins', 'funvotes'],
	['addons', 'sourcemod', 'scripting', 'funvotes']))
job.AddCommand(CopyFolder(['plugins', 'basevotes'],
	['addons', 'sourcemod', 'scripting', 'basevotes']))
job.AddCommand(CopyFolder(['plugins', 'basebans'],
	['addons', 'sourcemod', 'scripting', 'basebans']))
job.AddCommand(CopyFolder(['plugins', 'funcommands'],
	['addons', 'sourcemod', 'scripting', 'funcommands']))
job.AddCommand(CopyFolder(['plugins', 'playercommands'],
	['addons', 'sourcemod', 'scripting', 'playercommands']))

defPlugins = [
	'admin-flatfile',
	'adminhelp',
	'antiflood',
	'basecommands',
	'reservedslots',
	'basetriggers',
	'nextmap',
	'basechat',
	'funcommands',
	'basevotes',
	'funvotes',
	'basebans',
	'basecomm',
	'adminmenu',
	'playercommands',
	'clientprefs',
	'sounds'
]

disPlugins = [
	'admin-sql-prefetch',
	'admin-sql-threaded',
	'sql-admin-manager',
	'mapchooser',
	'randomcycle',
	'rockthevote',
	'nominations'
]

commands = []
for plugin in defPlugins:
	commands.append(CopyFile(os.path.join('..', 'plugins', plugin + '.smx'),
	                         os.path.join('addons', 'sourcemod', 'plugins')))

for plugin in disPlugins:
	commands.append(CopyFile(os.path.join('..', 'plugins', plugin + '.smx'),
	                         os.path.join('addons', 'sourcemod', 'plugins', 'disabled')))
job.AddCommandGroup(commands)

job.AddCommand(CopyFile(os.path.join('..', 'includes', 'version_auto.inc'),
	os.path.join('addons', 'sourcemod', 'scripting', 'include')))

bincopies = []

def AddNormalLibrary(name, dest):
	dest = os.path.join('addons', 'sourcemod', dest)
	bincopies.append(CopyFile(os.path.join('..', name, name + osutil.SharedLibSuffix()), dest))
	
	# Each platform's version of dump_syms needs the path in a different format.
	if AMBuild.target['platform'] == 'linux':
		debug_info.append(name + '/' + name + '.so')
	elif AMBuild.target['platform'] == 'darwin':
		debug_info.append(name + '/' + name + '.dylib.dSYM')
	elif AMBuild.target['platform'] == 'windows':
		debug_info.append(name + '\\' + name + '.pdb')

def AddHL2Library(name, dest):
	for i in SM.sdkInfo:
		sdk = SM.sdkInfo[i]
		if AMBuild.target['platform'] not in sdk['platform']:
			continue
		AddNormalLibrary(name + '.' + sdk['ext'], dest)

debug_info = []

if AMBuild.target['platform'] == 'linux':
	bincopies.append(CopyFile(os.path.join('..', 'loader', 'sourcemod_mm_i486.so'),
	                 os.path.join('addons', 'sourcemod', 'bin')))
	debug_info.append('loader/sourcemod_mm_i486.so')
elif AMBuild.target['platform'] == 'darwin':
	bincopies.append(CopyFile(os.path.join('..', 'loader', 'sourcemod_mm.dylib'),
	                 os.path.join('addons', 'sourcemod', 'bin')))
	debug_info.append('loader/sourcemod_mm.dylib.dSYM')
elif AMBuild.target['platform'] == 'windows':
	bincopies.append(CopyFile(os.path.join('..', 'loader', 'sourcemod_mm.dll'),
	                 os.path.join('addons', 'sourcemod', 'bin')))
	debug_info.append('loader\\sourcemod_mm.pdb')

AddHL2Library('sourcemod', 'bin')
AddNormalLibrary('sourcemod.logic', 'bin')
AddNormalLibrary('sourcepawn.jit.x86', 'bin')
AddNormalLibrary('geoip.ext', 'extensions')
if SM.hasMySql:
	AddNormalLibrary('dbi.mysql.ext', 'extensions')
AddNormalLibrary('dbi.sqlite.ext', 'extensions')

if 'css' in SM.sdkInfo:
	AddNormalLibrary('game.cstrike.ext.2.css', 'extensions')

if 'csgo' in SM.sdkInfo:
	AddNormalLibrary('game.cstrike.ext.2.csgo', 'extensions')

if 'ep2v' in SM.sdkInfo:
	AddNormalLibrary('game.tf2.ext.2.ep2v', 'extensions')

AddNormalLibrary('topmenus.ext', 'extensions')
AddNormalLibrary('regex.ext', 'extensions')
AddNormalLibrary('webternet.ext', 'extensions')
AddNormalLibrary('clientprefs.ext', 'extensions')
AddNormalLibrary('updater.ext', 'extensions')
AddNormalLibrary('bintools.ext', 'extensions')
AddHL2Library('sdkhooks.ext', 'extensions')
AddHL2Library('sdktools.ext', 'extensions')

bincopies.append(CopyFile(os.path.join('..', 'spcomp', 'spcomp' + osutil.ExecutableSuffix()),
                          os.path.join('addons', 'sourcemod', 'scripting')))

# Each platform's version of dump_syms needs the path in a different format.
if AMBuild.target['platform'] == 'linux':
	debug_info.append('spcomp' + '/' + 'spcomp')
elif AMBuild.target['platform'] == 'darwin':
	debug_info.append('spcomp' + '/' + 'spcomp' + '.dSYM')
elif AMBuild.target['platform'] == 'windows':
	debug_info.append('spcomp' + '\\' + 'spcomp' + '.pdb')

job.AddCommandGroup(bincopies)

if AMBuild.target['platform'] == 'windows':
	job.AddCommand(CopyFile(
		os.path.join(AMBuild.sourceFolder, 'sourcepawn', 'batchtool', 'compile.exe'),
		os.path.join('addons', 'sourcemod', 'scripting')))
	
pdblog = open(os.path.join(AMBuild.outputFolder, 'pdblog.txt'), 'wt')
for pdb in debug_info:
	pdblog.write(pdb + '\n')
pdblog.close()