from __future__ import division, print_function, unicode_literals

import os

Import('env')

# math libs
mlib = env['RAD_MLIB']

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

# common objects
viewbeams = env.Object(source='viewbeams.c')
holo = env.Object(source='holo.c')
holofile = env.Object(source='holofile.c')
clumpbeams = env.Object(source='clumpbeams.c')

# standard targets
PROGS = (
('rholo', Split('''rholo.c rholo2.c rholo2l.c rholo3.c rholo4.c''')
	 + [env.version, holofile, holo, viewbeams], ['rtrad'],0),
('rhpict', Split('rhpict.c rhpict2.c')+[env.version, holofile, holo, viewbeams],
	['rtrad'],1),
('rhcopy', Split('rhcopy.c') + [clumpbeams, holofile, holo], ['rtrad'],1),
('rhinfo', Split('rhinfo.c') + [holofile, holo], ['rtrad'],1),
('rhoptimize', Split('rhoptimize.c') + [clumpbeams,holofile,holo], ['rtrad'],1),
('genrhgrid', Split('genrhgrid.c') + [holofile, holo], ['rtrad'],1),
)
if 1:
	for p in PROGS:
		if not p[3] and os.name == 'nt': continue 
		prog = env.Program(target=radbin(p[0]),
				source=p[1], LIBS=p[2] + mlib)
		Default(prog)
		env.Append(RAD_BININSTALL=[env.Install(env['RAD_BINDIR'], prog)])

# display drivers
if env.has_key('X11INCLUDE'):
	xenv = env.Clone(CPPPATH=[env['X11INCLUDE']] + env['CPPPATH'],
			LIBPATH=[env['X11LIB']] + env['LIBPATH'],)
	# common objects
	rhdisp = xenv.Object(source='rhdisp.c')
	rhdisp2 = xenv.Object(source='rhdisp2.c')
	rhd_qtree = env.Object(source='rhd_qtree.c')
	xcommon = [rhdisp, rhdisp2, holo, viewbeams]
	# other stuff
	devdir = os.path.join(xenv['RAD_BUILDBIN'],'dev')
	idevdir = os.path.join(xenv['RAD_BINDIR'],'dev')
	ocppflags = xenv.get('CPPFLAGS', [])
	addobj = xenv.get('ADDOBJNOTIFY')

	# XXX the .hdi extension will cause problems on Windows
	xenv['PROGSUFFIX'] = '.hdi'

	def make_hdi(p): # build them
		obj = xenv.Object(target=p[3], source=p[4], CPPFLAGS=ocppflags + p[5])
		prog = xenv.Program(target=radbin(p[0]),
				source=[obj] + p[1] + xcommon, LIBS=p[2])
		Default(prog)
		inst = xenv.Install(idevdir, prog)
		# XXX what are the alternative "<driver>h" names good for?
		insth = xenv.InstallAs(target=radbin(
					p[0] + 'h' + xenv['PROGSUFFIX']), source=prog)
		env.Append(RAD_BININSTALL=[inst, insth])

	# (name, sources,   libs,   obj, objsrc, objflags)
	XHDI = (('x11',  Split('rhd_ctab.c rhd_qtree2r.c') + [rhd_qtree],
				['rtrad', 'X11'] + mlib,
				'rhd_x11', 'rhd_x11.c', []),
		   )
	for p in XHDI:
		make_hdi(p)

	if env.has_key('OGL'):
		rgllibs = ['rgl','rtrad']
		ogl_common_s = Split('rhd_odraw.c rhd_geom.c')
		ogl_common = map(lambda s:xenv.Object(source=s), ogl_common_s)
		rhdobj = xenv.Object(source='rhdobj.c')

		GLHDI = (
			# glx1h
			('glx1', ['rhd_qtree2c.c', rhd_qtree],
			 ['rtrad', 'GLU','GL','X11'] + mlib,
			 'rhd_glx1', 'rhd_glx1.c', ['-DNOSTEREO']),

			# oglh
			('ogl', ogl_common + [addobj],
			 rgllibs+['rtrad', 'GLU','GL','X11'] + mlib,
			 'rhd_ogl0', 'rhd_ogl.c', ['-DNOSTEREO']),

			# ogloh
			('oglo', [rhdobj] + ogl_common + [addobj],
			 rgllibs+['rtrad']+['rtrad', 'GLU','GL','X11'] + mlib,
			 'rhd_oglo', 'rhd_ogl.c', ['-DDOBJ', '-DNOSTEREO']),
			)
		for p in GLHDI:
			make_hdi(p)
		if xenv['RAD_STEREO'] == '-DSTEREO':

			GLSHDI = (
				# oglsh
				('ogls', ogl_common,
				 rgllibs+['rtrad', 'GLU','GL','X11','Xext'] + mlib,
				 'rhd_ogls', 'rhd_ogl.c', ['-DSTEREO']),
					
				# oglsoh
				('oglso', [rhdobj] + ogl_common,
				 rgllibs+['rtrad', 'GLU','GL','X11','Xext'] + mlib,
				 'rhd_oglso', 'rhd_ogl.c', ['-DDOBJ', '-DSTEREO']),
					)
			for p in GLHDI:
				make_hdi(p)



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