# 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"

LIBBEAGLE_MIN_VERSION = '0.1.3'

""" Detect the libbeagle library"""

from SCons.Options import Options
import sys

def exists(env):
	return true

def generate(env):

	cachefile = env['CACHEDIR']+'/libbeagle.cache.py'
        opts = Options(cachefile)
        opts.AddOptions(
		( 'ENABLE_LIBBEAGLE', 'has the libbeagle library' ),
		( 'LIBBEAGLE_FLAGS', 'include flags for libbeagle' ),
		( 'LIBBEAGLE_LDFLAGS', 'link flags for libbeagle' ),
        )
        opts.Update(env)

        if env['HELP']:
                print """
"""+BOLD+"""*** libbeagle options ***
-------------------"""+NORMAL+"""
"""+BOLD+"""* enable_kio_beagle=1      """+NORMAL+""": enable compilation of beagle support (default)
"""+BOLD+"""* enable_kio_beagle=0      """+NORMAL+""": disable compilation of beagle support
"""+NORMAL
		return

	if env['_CONFIGURE'] or not env.has_key('ENABLE_LIBBEAGLE'):
		
		if 'enable_kio_beagle=0' in sys.argv:
			print BOLD+"Beagle  support disabled          :"+NORMAL
			env['ENABLE_LIBBEAGLE']=0
			opts.Save (cachefile, env)
			return

		# default option
		if not 'enable_kio_beagle=1' in sys.argv:
			# the user did not use the enable_kio_beagle flag or used it wrong, so tell him howto do it
			print BOLD+"Beagle support enabled (default)  :  "+NORMAL+"disable with the flag 'enable_kio_beagle=0'"
	
		# check for presence of pkg-config
		def CheckPKGConfig(context):
			context.Message( 'Checking for pkg-config           :  (required for beagle backend) ' )
			ret = context.TryAction('pkg-config --atleast-pkgconfig-version=0.15')[0]
			context.Result( ret )
			return ret

		def CheckPKG(context, name):
			context.Message( 'Checking for %s  ' % name )
			ret = context.TryAction('pkg-config --exists \'%s\'' % name)[0]
			context.Result( ret )
			return ret

		conf = env.Configure (custom_tests = { 'CheckPKGConfig' : CheckPKGConfig,
						       'CheckPKG' : CheckPKG })
		if not conf.CheckPKGConfig():
			print RED+'pkg-config not found.'+NORMAL
			env.Exit(1)

		if not conf.CheckPKG('libbeagle-0.0 >= %s' % LIBBEAGLE_MIN_VERSION):
			print RED+'libbeagle >= %s not found. Install libbeagle for beagle backend.' % LIBBEAGLE_MIN_VERSION+NORMAL
			env.Exit (1)
		print ''
		env = conf.Finish ()
		env['ENABLE_LIBBEAGLE']=1
		opts.Save(cachefile, env)
