target = "NSIS"

files = Split("""
	nsismenu/nsismenu.cpp
	nsismenu/nslinks.cpp
""")

resources = Split("""
	nsismenu/nsisicon.ico
""")

rc = 'nsismenu/nsismenu.rc'

libs = Split("""
	advapi32
	comctl32
	gdi32
	shell32
	user32
	wxbase28
	wxmsw28_core
	wxmsw28_html
""")

html = Split("""
	#Menu/compiler.html
	#Menu/docs.html
	#Menu/index.html
	#Menu/intro.html
	#Menu/notinstalled.html
	#Menu/update.html
	#Menu/websites.html
""")

images = Split("""
	#Menu/images/clear.gif
	#Menu/images/header.gif
	#Menu/images/line.gif
	#Menu/images/menu.gif
	#Menu/images/menud.gif
	#Menu/images/site.gif
""")

Import('BuildUtil env')

import os
from os.path import join

if env['PLATFORM'] != 'win32' and WhereIs('wx-config') or \
   env['PLATFORM'] == 'win32' and os.environ.has_key('WXWIN'):

	# build

	if env['PLATFORM'] != 'win32':
		rc = libs = []

	nsis_menu = BuildUtil(
		target,
		files,
		libs,
		res = rc,
		resources = resources,
		flags = ['$MSVCRT_FLAG', '$EXCEPTION_FLAG'],
		root_util = True,
		cross_platform = True
	)

	# manifest

	if env.has_key('MSVS_VERSION'):
		if float(env['MSVS_VERSION'].replace('Exp','')) >= 8.0:
			manifest = env.SideEffect('%s.exe.manifest' % target, nsis_menu)
			env.DistributeBin(manifest, alias='install-utils')

	# set directories

	if nsis_menu[0].env['PLATFORM'] == 'win32':
		wxlib = [join(os.environ['WXWIN'], 'lib', 'vc_libnsis')]
		wxinc = [
			join(os.environ['WXWIN'], 'include'),
			join(os.environ['WXWIN'], 'lib', 'vc_libnsis', 'msw')
		]
		nsis_menu[0].env.Append(LIBPATH = wxlib)
		nsis_menu[0].env.Append(CPPPATH = wxinc)
	else:
		nsis_menu[0].env.ParseConfig('wx-config --cxxflags --libs')

	# install menu files

	env.DistributeMenu(html, alias='install-utils')
	env.DistributeMenu(images, path='images', alias='install-utils')

else:
	
	# no wxWidgets
	def err(target, source, env):
		if env['PLATFORM'] == 'win32':
			print '*** error: WXWIN must be set to build NSIS Menu!'
		else:
			print '*** error: wx-config must be in the PATH to build NSIS Menu!'
		return 1

	cmd = env.Command(target, files, Action(err, ''))
	env.Alias('NSIS Menu', cmd)
