#!/usr/bin/env python

import intltool, platform, os
import Environment, Scripting, Runner, Options

NAME = 'Val(a)IDE'
VERSION = '0.7.2'
APPNAME = 'valide'
WEBSITE = 'http://www.valaide.org/'
COPYRIGHT = "Copyright \xc2\xa9 2008-2011 The Val(a)IDE team"

VERSION_MAJOR_MINOR = '.'.join (VERSION.split ('.')[0:2])
VERSION_MAJOR = '.'.join (VERSION.split ('.')[0:1])
srcdir = '.'
blddir = '_build_'

def set_options (opt):
  opt.tool_options ('compiler_cc')

  opt.add_option ('--with-libdir',
    help = 'The library directory',
    type = 'string',
    dest = 'libdir')
  opt.add_option ('--debug',
    help = 'Debug mode',
    action = 'store_true',
    default = False)
  opt.add_option ('--with-target-platform',
    help = 'Force a target platform (cross-compilation)',
    type = 'string',
    dest = 'target_platform')
  opt.add_option ('--without-libunique',
    help = '',
    action = 'store_true',
    default = False)

  gr = opt.add_option_group ('build options')
  gr.add_option ('--doc',
    help = 'Build the API documentation',
    action = 'store_true',
    default = False)

def configure (conf):
  conf.check_tool ('compiler_cc cc vala intltool valadoc')

  conf.check_cfg (package='glib-2.0',          uselib_store='GLIB',          atleast_version='2.18.0', args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='gio-2.0',           uselib_store='GIO',           atleast_version='2.18.0', args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='gobject-2.0',       uselib_store='GOBJECT',       atleast_version='2.18.0', args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='gmodule-2.0',       uselib_store='GMODULE',       atleast_version='2.18.0', args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='gtk+-2.0',          uselib_store='GTK+',          atleast_version='2.18.0', args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='libxml-2.0',        uselib_store='LIBXML',        atleast_version='2.5.0',  args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='gtksourceview-2.0', uselib_store='GTKSOURCEVIEW', atleast_version='2.10.0', args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='libvala-0.12',      uselib_store='VALA',          atleast_version='0.12.0', args='--cflags --libs', mandatory=True)
  conf.check_cfg (package='gdl-1.0',           uselib_store='GDL',           atleast_version='2.28',   args='--cflags --libs', mandatory=True)
  if not Options.options.without_libunique:
    conf.check_cfg (package='unique-1.0',        uselib_store='UNIQUE',        atleast_version='1.0.0',  args='--cflags --libs', mandatory=False)

  conf.define ('PACKAGE_NAME', APPNAME)
  conf.define ('GETTEXT_PACKAGE', APPNAME)
  conf.define ('VERSION', VERSION)
  conf.define ('COPYRIGHT', COPYRIGHT)
  conf.define ('WEBSITE', WEBSITE)
  conf.define ('APPNAME', NAME)
  conf.define ('VAPI_VERSION', VERSION_MAJOR)

  # libyaml-glib
  conf.define ('YAML_VERSION_MAJOR', 0)
  conf.define ('YAML_VERSION_MINOR', 1)
  conf.define ('YAML_VERSION_PATCH', 2)
  conf.define ('YAML_VERSION_STRING', '0.1.2')

  if Options.options.target_platform:
    target_platform = Options.options.target_platform
  else:
    target_platform = platform.system ()
  if target_platform == 'Windows':
    prefix = '..'
    conf.define ('VALIDE_CTAGS_EXEC', 'ctags-vala')
    conf.define ('OS', 'win32')
  else:
    prefix = conf.env['PREFIX']
    conf.define ('VALIDE_CTAGS_EXEC', prefix + '/bin/ctags-vala')
    conf.define ('OS', 'unix')

  if Options.options.libdir:
    libdir = Options.options.libdir
    conf.env['LIBDIR'] = libdir
    conf.env['shlib_INST_VAR'] = 'LIBDIR'
    conf.env['shlib_INST_DIR'] = ''
    conf.env['staticlib_INST_VAR'] = 'LIBDIR'
    conf.env['staticlib_INST_DIR'] = ''
  else:
    libdir = prefix + '/lib'

  datadir = prefix + '/share/' + APPNAME
  conf.define ('DATA_DIR', datadir)
  conf.define ('VAPI_DIR', datadir + '/vapi/')
  conf.define ('PLUGINS_DIR', libdir + '/' + APPNAME + '/plugins/')
  conf.define ('TEMPLATES_DIR', datadir + '/template/')

  conf.define ('LOCALE_DIR', prefix + '/share/locale/')
  conf.define ('PIXMAPS_DIR', prefix + '/share/pixmaps/' + APPNAME)

  if prefix != '/usr' and prefix != '/usr/local' and prefix != '/usr/' and prefix != '/usr/local/':
    prefix = (os.getenv ('HOME') or os.getenv('USERPROFILE')) + '/.local'

  conf.define ('DESKTOP_DIR', prefix + '/share/applications/')
  conf.define ('MAN_DIR', prefix + '/share/man/')
  conf.define ('MIME_DIR', prefix + '/share/mime/')
  conf.define ('MIME_ICON_DIR', prefix + '/share/icons/hicolor/48x48/mimetypes')

  conf.define ('VALA_VERSION', conf.env['LIB_VALA'][0][5:])

  # set 'default' variant
  conf.define ('DEBUG', 0)
  conf.env['CCFLAGS']=['-O2']
  conf.write_config_header ('config.h')

  # set 'debug' variant
  env_debug = conf.env.copy ()
  env_debug.set_variant ('debug')
  conf.set_env_name ('debug', env_debug)

  conf.setenv ('debug')
  conf.define ('DEBUG', 1)
  conf.env['CCFLAGS'] = ['-O0', '-g3']
  conf.env['VALAFLAGS'] = ['-g', '-v']
  conf.write_config_header ('config.h', env=env_debug)

  conf.sub_config ('plugins')

def build (bld):
  if Options.options.doc:
    bld.add_subdirs ('doc')
  else:
    bld.add_subdirs ('libvalide')
    bld.add_group ()
    bld.add_subdirs ('po doc ctags-vala src pixmaps data plugins')
    bld.install_files (bld.env['DATA_DIR'], 'AUTHORS')

def dist ():
  # set the compression type to gzip (default is bz2)
  import Scripting
  Scripting.g_gz = 'gz'
  Scripting.dist (APPNAME, VERSION)

def shutdown ():
  if Options.commands['install']:
    if platform.system () != 'Windows':
      import Build
      os.system ('update-mime-database ' + Build.bld.env['MIME_DIR'])

