target="libxmms2java"
source=["xmmsclient.i", "misc.c", "callbacks.c"]
static=False
mydir="src/clients/lib/java"

import os
import sys

def walkDirs(path):
	"""helper function to get a list of all subdirectories"""
	def addDirs(pathlist, dirname, names):
		"""internal function to pass to os.path.walk"""
		for n in names:
			f = os.path.join(dirname, n)
			if os.path.isdir(f):
				pathlist.append(f)
	pathlist = [path]
	os.path.walk(path, addDirs, pathlist)
	return pathlist
	
def config(env):
	print "Checking for swig >= 1.3.25 ... ",

	if not env.get('SWIG'):
		print "no"
		raise ConfigError("Couldn't find swig")

	try:
		version = env.run(env.get('SWIG') + " -version").strip().split("\n")[0].split(' ')[2]
		
		if not version or ([int(a) for a in version.split(".")] < [1,3,25]) :
			print "no"
			raise ConfigError("Need Swig 1.3.25")
	
		print version
	except:
		print "no"
		raise ConfigError("Couldn't check version of swig")

	print "Checking for presence of java ... ",
	
	if not env.get('JAVAC') or not env.get('JAR'):
		print "not present"
		raise ConfigError("Couldn't find javac")
	
	print "present"
	
	# link with xmmsclient is nice.
	if env.platform == 'win32':
		env.Append(LIBS=["libxmmsclient"])
	else:
		env.Append(LIBS=["xmmsclient"])
	env.Prepend(LIBPATH="src/clients/lib/xmmsclient")

	# need to be .so even on darwin (for example)
	env['SHLIBPREFIX']=''
	if env.platform == "win32":		
		env['SHLIBSUFFIX']=".dll"
	else:
		env['SHLIBSUFFIX']=".so"
	
	java_base = os.environ.get('JAVA_HOME')
	if not java_base:
		if sys.platform == 'darwin':
			# Apple's OS X has its own special java base directory
			java_base = '/System/Library/Frameworks/JavaVM.framework'
		else:
			print "Your system is missing JAVA_HOME which is needed for javabindings"
			print "Please set JAVA_HOME and run scons with CONFIG=1 again"
			raise ConfigError("You need to define JAVA_HOME to install the Java bindings.")
			# Search for the java compiler
			print "JAVA_HOME environment variable is not set. Searching for java... ",
			jcdir = os.path.dirname(env.WhereIs('javac'))
			if not jcdir:
				print "not found."
				raise ConfigError("No javac found")
			# assuming the compiler found is in some directory like
			# /usr/jdkX.X/bin/javac, java's home directory is /usr/jdkX.X
			java_base = os.path.realpath(os.path.join(jcdir, ".."))
			print "found in %s" % java_base
			
	if sys.platform == 'cygwin':
  		# Cygwin and Sun Java have different ideas of how path names
		# are defined. Use cygpath to convert the windows path to
		# a cygwin path. i.e. C:\jdkX.X to /cygdrive/c/jdkX.X
		java_base = string.replace( \
                os.popen("cygpath -up '"+java_base+"'").read(), '\n', '')
 
	if sys.platform == 'darwin':
		# Apple does not use Sun's naming convention
		java_headers = [os.path.join(java_base, 'Headers')]
		java_libs = [os.path.join(java_base, 'Libraries')]
	else:
		# windows and linux
		java_headers = [os.path.join(java_base, 'include')]
		java_libs = [os.path.join(java_base, 'lib')]
		# Sun's windows and linux JDKs keep system-specific header
		# files in a sub-directory of include
	if java_base == '/usr' or java_base == '/usr/local':
		# standard location, probably we don't have to add anything
		pass
	else:
		# add all subdirs of 'include'. The system specific headers
		# should be in there somewhere
		java_headers = walkDirs(java_headers[0])
 
	# add Java's include and lib directory to the environment
	env.Append(CPPPATH = java_headers)
	env.Append(LIBPATH = java_libs)
 
	# add any special platform-specific compilation or linking flags
	if sys.platform == 'darwin':
		env.Append(SHLINKFLAGS = '-dynamiclib -framework JavaVM')
		env['SHLIBSUFFIX'] = '.jnilib'
	elif sys.platform == 'cygwin':
		env.Append(CCFLAGS = '-mno-cygwin')
		env.Append(SHLINKFLAGS = '-mno-cygwin -Wl,--kill-at')
 
	# Add extra potentially useful environment variables
	env['JAVA_HOME'] = java_base
	env['JNI_CPPPATH'] = java_headers
	env['JNI_LIBPATH'] = java_libs
	env['JARCHDIR'] = mydir
	
	env.Append(SWIGFLAGS=["-java", "-module", "Xmmsclient", "-package", "org.xmms2.wrapper.xmms2bindings", "-outdir", mydir + "/src/org/xmms2/wrapper/xmms2bindings", "-Ijava"])
	env.Append(CPPPATH = [mydir + '/include'])
	env.Append(LIBPATH=["src/clients/lib/xmmsclient"])
	env.Append(LIBS=["xmmsclient"])
	
	env.Java(target = mydir, source = [mydir + '/src'])
	env.Jar(target=mydir + '/xmms2java.jar', source=mydir + '/org')
	env.Install(env['PREFIX'] + '/share/xmms2/java', mydir + '/xmms2java.jar')
	def mkdir(target, source, env):
		try:
			os.mkdir(mydir + "/src/org/xmms2/wrapper/xmms2bindings")
		except:
			pass
	env.Execute(mkdir, lambda target, source, env: None)
