# Julien Antille, Thomas Nagy, 2005
# BSD license (see COPYING)

BOLD   ="\033[1m"
RED    ="\033[91m"
GREEN  ="\033[92m"
YELLOW ="\033[93m"
CYAN   ="\033[96m"
NORMAL ="\033[0m"

""" Detect the clucene library"""

from SCons.Options import Options
import sys

def exists(env):
	return true

def generate(env):

	cachefile = env['CACHEDIR']+'/lucene.cache.py'
        opts = Options(cachefile)
        opts.AddOptions(
		( 'ENABLE_CLUCENE', 'has the lucene library' ),
		( 'CLUCENE_FLAGS', 'include flags for clucene' ),
        )
        opts.Update(env)

        if env['HELP']:
                print """
"""+BOLD+"""*** clucene backend options ***
-------------------"""+NORMAL+"""
"""+BOLD+"""* enable_kio_clucene=1     """+NORMAL+""": enable compilation of CLucene searchengine (default)
"""+BOLD+"""* enable_kio_clucene=0     """+NORMAL+""": disable compilation of CLucene searchengine
"""+NORMAL
		return

	if env['_CONFIGURE'] or not env.has_key('ENABLE_CLUCENE'):

		if 'enable_kio_clucene=0' in sys.argv:
			print BOLD+"CLucene support disabled          :  "+NORMAL
			env['ENABLE_CLUCENE']=0
			opts.Save(cachefile, env)
			return
		
		# default option
		if not 'enable_kio_clucene=1' in sys.argv:
			# the user did not use the enable_kio_clucene flag or used it wrong, so tell him howto do it
			print BOLD+"CLucene support enabled (default) :  "+NORMAL+"disable with the flag 'enable_kio_clucene=0'"

		print "Checking for clucene              : ",

		# clear cached values
		if env.has_key('ENABLE_CLUCENE'):
			env.__delitem__('ENABLE_CLUCENE')
		
		import os
		lst=['/usr', '/opt', '/usr/local']
		found=0
		for dir in lst:
			if os.path.isfile( dir+'/include/CLucene.h' ):
				found=2
				env['CLUCENE_FLAGS']= ('-I ' + dir + '/include')
# 			        if os.path.isfile( dir+'/include/CLucene/debug/condition.h' ):	
# 					found=2
		
		if found==0:
			print RED+'We really need the clucene library !'+NORMAL
			print RED+'                                     Install'+BOLD+' clucene-0.8.13 '+NORMAL+RED+'or disable the CLucene searchengine'+NORMAL
			env.Exit(1)
# 		elif found==1:
# 			print RED+'The clucene library has been found'
# 			print RED+'but it is missing the following header file:'
# 			print BOLD+dir+'/include/CLucene/debug/condition.h'+NORMAL
# 			print RED+'Sorry for the inconvenience, you must reinstall'
# 			print RED+'clucene-0.8.13 but '+BOLD+'run autoconf/automake/aclocal '+RED+' before running configure'
# 			print RED+'(see http://kioclucene.objectis.net/documentation/howtos/download_and_install)'
# 			env.Exit(1)

		else:
			print GREEN+'looks good, but cannot check it is the requested clucene-0.8.13 release'+NORMAL
		
		env['ENABLE_CLUCENE']=1
		opts.Save(cachefile, env)
