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)

warp3d = env.Object(source="warp3d.c")
clrtab = env.Object(source="clrtab.c")
neuclrtab = env.Object(source="neuclrtab.c")

# standard targets
PROGS = (
# name       files            libs
('macbethcal', Split('macbethcal.c pmapgen.c mx3.c')+[warp3d], ['rtrad']),
('pcond',    Split('pcond.c pcond2.c pcond3.c pcond4.c')+[warp3d], ['rtrad']),
('pfilt',    Split('pfilt.c pf2.c pf3.c'), ['rtrad']),
('pcwarp',   ['pcwarp.c', warp3d], 	 ['rtrad']),
('pvalue',   ['pvalue.c'],    ['rtrad']),
('pcompos',  ['pcompos.c'],   ['rtrad']),
('psign',    ['psign.c'],     ['rtrad']),
('protate',  ['protate.c'],   ['rtrad']),
('pextrem',  ['pextrem.c'],   ['rtrad']),
('pflip',    ['pflip.c'],     ['rtrad']),
('pcomb',    ['pcomb.c'],     ['rtrad']),
('pinterp',  ['pinterp.c'],   ['rtrad']),
('psketch',  ['psketch.c'],   ['rtrad']),

('ra_gif',   ['ra_gif.c', clrtab, neuclrtab], ['rtrad']),
('ra_ps',    ['ra_ps.c'],     ['rtrad']),
('ra_ppm',   ['ra_ppm.c'],    ['rtrad']),
('ra_bmp',   ['ra_bmp.c'],    ['rtrad']),
('ra_t8',    ['ra_t8.c', clrtab, neuclrtab], ['rtrad']),
('ra_t16',   ['ra_t16.c'],    ['rtrad']),
('ra_rgbe',  ['ra_rgbe.c'],   ['rtrad']),
('ra_pict',  ['ra_pict.c'],   ['rtrad']),
('ra_hexbit',['ra_hexbit.c'], ['rtrad']),
('ra_xyze',  ['ra_xyze.c'],   ['rtrad']),
('pmblur2',  ['pmblur2.c'],   ['rtrad']),

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


# tiff library (building it is unix-specific for the moment)
#  If we haven't found one supporting LogLuv installed, build our own.
libtiff_fn = radlib(env['LIBPREFIX'] + 'tiff' + env['LIBSUFFIX'])
if os.name == 'posix' and not env.get('TIFFLIB_INSTALLED'):
	cwd = os.getcwd()
	upperdir = os.path.split(cwd)[0]
	libtiff = env.Command(libtiff_fn, None,
	[('cd "%(tiffdir)s"; '
		'env CC="$CC" '
		'./configure -C '
		'"libdir=%(upperdir)s/lib" '
		'--enable-static --disable-shared --disable-cxx '
		'--enable-logluv --disable-jpeg --disable-zlib --disable-pixarlog; '
		'cd %(portdir)s; make all;'
		'cd %(libtiffdir)s; make install-exec;' # don't install headers
	 ) % {
	'upperdir':upperdir,
	'tiffdir':os.path.join(cwd, 'tiff'),
	'portdir':os.path.join(cwd, 'tiff', 'port'),
	'libtiffdir':os.path.join(cwd, 'tiff', 'libtiff')
	}
	])

	env['TIFFLIB_INSTALLED'] = 1

if os.name == 'posix': # cleanup action for a previously built tifflib
	Clean(libtiff_fn, [libtiff_fn, radlib(env['LIBPREFIX'] + 'tiff.la')])

if env.get('TIFFLIB_INSTALLED'):
	libtiff = env.get('RAD_LIBTIFF')
	# tiff programs
	tiffi = env.get('RAD_TIFFINCLUDE')
	if tiffi: tiffincl = [tiffi] + env.get('CPPPATH', [])
	else: tiffincl = env.get('CPPPATH', [])
	tiffl = env.get('RAD_TIFFLIB')
	if tiffl: tifflib = [tiffl] + env.get('LIBPATH', [])
	else: tifflib = env.get('LIBPATH', [])

	ra_tiff = env.Program(target=radbin('ra_tiff'), source=['ra_tiff.c'],
		CPPPATH = tiffincl, LIBPATH=tifflib,
		LIBS=[libtiff,'rtrad'] + mlib)
	progs.append(ra_tiff)

	normtiff = env.Program(target=radbin('normtiff'),source=['normtiff.c'],
		CPPPATH = tiffincl, LIBPATH=tifflib,
		LIBS=[libtiff,'rtrad'] + mlib)
	progs.append(normtiff)


# X11 targets
if env.has_key('X11LIB'):
	xincl = env.get('CPPPATH', []) + ['$X11INCLUDE']
	xlibp = env.get('LIBPATH', []) + ['$X11LIB']
	xlibs = ['X11','rtrad']
	env.x11findwind = env.Object(source='../common/x11findwind.c',
		CPPPATH=xincl)

	ximage = env.Program(target=radbin('ximage'),
		source=Split('x11image.c x11raster.c')+[clrtab],
		CPPPATH=xincl, LIBPATH=xlibp, LIBS=xlibs + mlib)
	progs.append(ximage)

	xshowtrace = env.Program(target=radbin('xshowtrace'),
		source=Split('xshowtrace.c') + [env.x11findwind],
		CPPPATH=xincl, LIBPATH=xlibp, LIBS=xlibs + mlib)
	progs.append(xshowtrace)

pyscripts = Split('''falsecolor phisto''')
if os.name == 'posix': # XXX ignoring trad.wsh
	for s in Split('''normpat pdfblur pmblur pmdblur
			xyzimage pgblur ra_pfm ran2tiff pbilat'''):
		Default(env.InstallScript(radbin(s), s + '.csh'))
	for s in pyscripts:
		Default(env.InstallScript(radbin(s), s + '.py'))
else:
	for s in pyscripts:
		Default(env.InstallScript(radbin(s + '.py'), s + '.py'))
	

# ignored by Rmakefile: pacuity phisteq psquish pveil vlpic

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

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