# -*- python -*-

Import('env')
import os
import sys

if env['nowx']:
	files = ['Src/MainNoGUI.cpp']
else:
	files = [
		'Src/ARCodeAddEdit.cpp',
		'Src/AboutDolphin.cpp',
		'Src/CheatsWindow.cpp',
		'Src/ConfigMain.cpp',
		'Src/Debugger/BreakpointDlg.cpp',
		'Src/Debugger/BreakpointView.cpp',
		'Src/Debugger/BreakpointWindow.cpp',
		'Src/Debugger/CodeView.cpp',
		'Src/Debugger/CodeWindow.cpp',
		'Src/Debugger/CodeWindowFunctions.cpp',
		'Src/Debugger/DSPDebugWindow.cpp',
		'Src/Debugger/DSPRegisterView.cpp',
		'Src/Debugger/DebuggerPanel.cpp',
		'Src/Debugger/DebuggerUIUtil.cpp',
		'Src/Debugger/JitWindow.cpp',
		'Src/Debugger/MemoryCheckDlg.cpp',
		'Src/Debugger/MemoryView.cpp',
		'Src/Debugger/MemoryWindow.cpp',
		'Src/Debugger/RegisterView.cpp',
		'Src/Debugger/RegisterWindow.cpp',
		'Src/FifoPlayerDlg.cpp',
		'Src/Frame.cpp',
		'Src/FrameAui.cpp',
		'Src/FrameTools.cpp',
		'Src/GameListCtrl.cpp',
		'Src/GeckoCodeDiag.cpp',
		'Src/HotkeyDlg.cpp',
		'Src/ISOFile.cpp',
		'Src/ISOProperties.cpp',
		'Src/InputConfigDiag.cpp',
		'Src/InputConfigDiagBitmaps.cpp',
		'Src/LogConfigWindow.cpp',
		'Src/LogWindow.cpp',
		'Src/Main.cpp',
		'Src/MemcardManager.cpp',
		'Src/MemoryCards/WiiSaveCrypted.cpp',
		'Src/NetWindow.cpp',
		'Src/PHackSettings.cpp',
		'Src/PatchAddEdit.cpp',
		'Src/TASInputDlg.cpp',
		'Src/UDPConfigDiag.cpp',
		'Src/VideoConfigDiag.cpp',
		'Src/WXInputBase.cpp',
		'Src/WiimoteConfigDiag.cpp',
		'Src/WxUtils.cpp',
		]

if sys.platform == 'win32':
	files += ['Src/stdafx.cpp']
elif sys.platform == 'darwin':
	env['CPPPATH'] += ['#Externals']
	env['FRAMEWORKPATH'] += ['Externals/Cg']
	env['FRAMEWORKS'] += ['ApplicationServices', 'Carbon', 'Cocoa']
	env['FRAMEWORKS'] += ['AudioUnit', 'IOBluetooth', 'IOKit', 'OpenGL']
	env['FRAMEWORKSFLAGS'] = ['-Wl,-lazy_framework,Cg']
	env['FRAMEWORKSFLAGS'] += ['-Wl,-weak_framework,OpenCL']
	env['LIBS'] += ['iconv', 'z']

	# XXX
	env['FRAMEWORKSFLAGS'] += ['-Wl,-weak_framework,QuickTime']
	env['FRAMEWORKSFLAGS'] += ['-Wl,-no_arch_warnings']

	exe = '#' + env['prefix'] + '/Dolphin.app/Contents/MacOS/Dolphin'
	if env['nowx']:
		exe += 'NoGUI'

	env.Install('#' + env['prefix'] + '/Dolphin.app/Contents/' +
		'Frameworks/Cg.framework', source = [
		'#Externals/Cg/Cg.framework/Cg',
		'#Externals/Cg/Cg.framework/Resources'
		])

	env.Install(env['data_dir'], '#Data/Sys')
	env.Install(env['data_dir'], '#Data/User')
	env.Install(env['data_dir'],
		'#Source/Core/DolphinWX/resources/Dolphin.icns')

	languages = [] 
	msgfmt = env.WhereIs('msgfmt')
	if not msgfmt == None:
		po_files = Glob('#Languages/po/*.po', strings = True)
		for po in po_files:
			index_lo = po.find('Languages/po/') + len('Languages/po/')
			index_hi = po.find('.po')
			lang = po[index_lo:index_hi]
			lproj = os.sep + lang + '.lproj'
			mo_dir = env['build_dir'] + '/Languages' + lproj
			mo_file = mo_dir + '/dolphin-emu.mo'
			env.Command('#' + mo_file, po, 'mkdir -p ' + mo_dir +
				' && ' + msgfmt + ' -o ' + mo_file + ' ' + po)
			env.Install(env['data_dir'] + lproj, '#' + mo_file)
			languages += [lang]

	from plistlib import writePlist
	def createPlist(target, source, env):
		for srcNode in source:
			writePlist(srcNode.value, str(target[0]))
	env.Append(BUILDERS = {'Plist' : Builder(action = createPlist)})
	env.Plist('#' + env['prefix'] + '/Dolphin.app/Contents/Info.plist',
		Value(dict(
		CFBundleExecutable = 'Dolphin',
		CFBundleIconFile = 'Dolphin.icns',
		CFBundleIdentifier = 'com.dolphin-emulator.dolphin',
		CFBundleLocalizations = languages,
		CFBundlePackageType = 'APPL',
		CFBundleShortVersionString = env['gitdesc'],
		CFBundleVersion = '3.0',
		LSMinimumSystemVersion = '10.5.4',
		LSRequiresCarbon = True,
		CFBundleDocumentTypes = [
			dict(CFBundleTypeExtensions =
			('ciso', 'dol', 'elf', 'gcm', 'gcz', 'iso', 'wad'),
			CFBundleTypeIconFile = 'Dolphin.icns',
			CFBundleTypeName = 'Nintendo GC/Wii file',
			CFBundleTypeRole = 'Viewer')]
		)))

	env.Command('dummy', '#' + env['prefix'],
		"find $SOURCES -name .svn -exec rm -rf {} +")
else:
	files += ['Src/X11Utils.cpp']
	exe = env['binary_dir'] + '/dolphin-emu'
	if env['nowx']:
		exe += '-nogui'
	env.InstallAs(env['data_dir'] + '/sys', '#Data/Sys')
	env.InstallAs(env['data_dir'] + '/user', '#Data/User')

env.Program(exe, files)