#! /usr/bin/env python
# encoding: utf-8

import os
import Utils
import Options
import commands
g_maxlen = 40
import shutil
import re
import Logs

# used by waf dist and waf build
VERSION='0.7'
APPNAME='Jc_Gui'

# these variables are mandatory ('/' are converted automatically)
srcdir = '.'
blddir = 'build'

# options defined for waf configure
def set_options(opt):
    opt.tool_options('compiler_cxx')

    opt.add_option('--cxxflags', 
                   type='string', 
                   default='-O3  -march=native -Wall', 
                   dest='cxxflags', 
                   help='C++ compiler flags [Default: -O3  -march=native -Wall]')

# a bit of waf display formatting
def display_msg(msg, status = None, color = None):
    sr = msg
    global g_maxlen
    g_maxlen = max(g_maxlen, len(msg))
    if status:
        print "%s :" % msg.ljust(g_maxlen),
        Utils.pprint(color, status)
    else:
        print "%s" % msg.ljust(g_maxlen)

def error_msg(msg):
    Utils.pprint('RED', msg)

def display_feature(msg, build):
    if build:
        display_msg(msg, "yes", 'GREEN')
    else:
        display_msg(msg, "no", 'YELLOW')

# guitarix waf configuration
def configure(conf):
    conf.check_tool('compiler_cxx')

    conf.check_cfg(package='jack', atleast_version='0.109.1', args='--cflags --libs', uselib_store='JACK', mandatory=1)
    conf.check_cfg(package='sndfile', atleast_version='1.0.17', args='--cflags --libs', uselib_store='SNDFILE', mandatory=1)
    conf.check_cfg(package='gtk+-2.0', atleast_version='2.12.0', args='--cflags --libs', uselib_store='GTK2', mandatory=1)
   
    conf.env['SHAREDIR'] = conf.env['PREFIX'] + '/share'

    # defines for compilation
    conf.define('GX_STYLE_DIR', os.path.normpath(os.path.join(conf.env['SHAREDIR'], 'Jc_Gui')))
    conf.define('GX_PIXMAPS_DIR', os.path.normpath(os.path.join(conf.env['SHAREDIR'], 'pixmaps')))
    conf.define('GX_VERSION', VERSION)

    # writing config.h
    conf.write_config_header('config.h')

    conf.define('BINDIR', os.path.normpath(os.path.join(conf.env['PREFIX'], 'bin')))
    conf.define('DESKAPPS_DIR', os.path.normpath(os.path.join(conf.env['SHAREDIR'], 'applications')))
    conf.define('BIN_NAME', APPNAME)
    conf.define('CXXFLAGS', Options.options.cxxflags)

    # config subdirs
    conf.sub_config('src');
 
    # some output
    print
    display_msg("==================")
    version_msg = "Jc_Gui " + VERSION

    print version_msg

    print

    display_msg("C++ flags", conf.env['CXXFLAGS'], 'CYAN')
    display_msg("Install prefix", conf.env['PREFIX'], 'CYAN')
    display_msg("Install binary", conf.env['BINDIR'], 'CYAN')
    display_msg("Jc_Gui share directory", conf.env['GX_STYLE_DIR'], 'CYAN')
    display_msg("Jc_Gui pixmaps directory", conf.env['GX_PIXMAPS_DIR'], 'CYAN')

    print

def build(bld):
    # process subfolders from here
    #bld.add_subdirs('ladspa')
    bld.add_subdirs('src')
    bld.add_subdirs('pixmaps')

    bld.install_files(bld.env['DESKAPPS_DIR'], 'Jc_Gui.desktop', chmod=0644)

    
