from __future__ import division, print_function, unicode_literals

import os

Import('env') # inherit from parent

mlib = ['$RAD_MLIB']
progs = []

# compose paths
def radbin(name): return os.path.join('$RAD_BUILDBIN', name)
def radlib(name): return os.path.join('$RAD_BUILDLIB', name)
def tclscr(name): return os.path.join('$RAD_BUILDRLIB', 'tcl', name)

cmatrix = env.Object(source='cmatrix.c')
cmbsdf = env.Object(source='cmbsdf.c')

# standard targets
PROGS = [
('findglare', Split('findglare.c glareval.c glaresrc.c setscan.c'), ['rtrad']),
('glarendx',  Split('glarendx.c'), ['rtrad']),
('vwright',   Split('vwright.c'),  ['rtrad']),
('vwrays',    Split('vwrays.c'),   ['rtrad']),
('rad',       Split('rad.c'),      ['rtrad']),
('rpiece',    Split('rpiece.c') + [env.version], ['rtrad']),
('ranimate',  ['ranimate.c', '$RAD_NETCOMPAT'], ['rtrad', '$RAD_SOCKETLIB']),
('dctimestep', Split('dctimestep.c')+[cmatrix, cmbsdf], ['rtrad']),
('rttree_reduce', Split('rttree_reduce.c'), ['rtrad']),
('rcollate', Split('rcollate.c'), ['rtrad']),
('eplus_adduvf', Split('eplus_adduvf.c eplus_idf.c'), ['rtrad']),
('rfluxmtx', Split('rfluxmtx.c'), ['rtrad']),
('rmtxop', Split('rmtxop.c rmatrix.c')+[cmatrix, cmbsdf], ['rtrad']),
('wrapBSDF', Split('wrapBSDF.c'), ['rtrad']),
('evalglare', Split('evalglare.c pictool.c'), ['rtrad', 'reetz']),
]

for p in PROGS:
	prog = env.Program(target=radbin(p[0]), source=p[1], LIBS=p[2]+mlib)
	progs.append(prog)

# targets with different includes/libs
rs = Split('ranimove.c ranimove1.c ranimove2.c')
ranimove = env.Program(target=radbin('ranimove'), source=rs,
		CPPPATH=env.get('CPPPATH', [])+ ['#src/rt'],
		LIBS=['raycalls','rttrace','rtrad'] + mlib)
progs.append(ranimove)
rsensor = env.Program(target=radbin('rsensor'), source='rsensor.c',
		CPPPATH=env.get('CPPPATH', [])+ ['#src/rt'],
		LIBS=['raycalls','rttrace','rtrad'] + mlib)
progs.append(rsensor)

getinfo = env.Program(target=radbin('getinfo'), source='getinfo.c',
		LIBS=['rtrad'])
progs.append(getinfo)


# X11 targets
if env.has_key('X11LIB'):
	addobj = env.get('ADDOBJNOTIFY')
	xincl = env.get('CPPPATH', []) + ['$X11INCLUDE']
	xlibp = env.get('LIBPATH', []) + ['$X11LIB']
	xglaresrc = env.Program(target=radbin('xglaresrc'),
			source=Split('xglaresrc.c') + [env.x11findwind], # XXX remote magic
			LIBPATH=xlibp, CPPPATH=xincl,
			LIBS=['rtrad', 'X11']+ mlib)
	progs.append(xglaresrc)

	# OpenGL targets that also depend on X11
	if env.has_key('OGL'):
		glrad = env.Program(target=radbin('glrad'),
				source=['glrad.c', addobj],
				CPPFLAGS=env.get('CPPFLAGS', []) + ['$RAD_STEREO'],
				LIBPATH=xlibp, CPPPATH=xincl,
				LIBS=['rgl','rtrad','GL', 'GLU','X11'] + mlib,)
		progs.append(glrad)

pyscripts = Split('''rlux''')
if os.name == 'posix':
	for s in Split('''objline glare dayfact
			debugcal raddepend compamb vinfo fieldcomb'''):
		Default(env.InstallScript(radbin(s), s + '.csh'))
	for s in Split('''objview objpict
			genambpos genklemsamp genskyvec genBSDF bsdfview ltview ltpict'''):
		Default(env.InstallScript(radbin(s), s + '.pl'))
	for s in Split('''do_action.tcl do_file.tcl do_options.tcl do_results.tcl
		do_scene.tcl do_views.tcl do_zone.tcl file.hlp getfile.tcl gethelp.tcl
		help.hlp tclIndex trad.hlp trad.icon util.tcl'''):
		Default(env.InstallScript(tclscr(s), s))
	Default(env.InstallTCLScript(radbin('trad'), 'trad.wsh'))
	for s in pyscripts:
		Default(env.InstallScript(radbin(s), s + '.py'))
else:
	for s in pyscripts:
		Default(env.InstallScript(radbin(s + '.py'), s + '.py'))
	

Default('#src/util')
env.Install('$RAD_BINDIR', progs)

# vim: set syntax=python:
# vi: set ts=4 sw=4 :
