# -*- python -*-
# Waf script to build eLectrix
# Martin Linder <mali2297@users.sf.net>

VERSION = '0.2.0'
APPNAME = 'electrix'

top = '.'
out = 'build'

def options (opt):
  import sys
	
  opt.load ('compiler_c glib2')
		  
  if 'configure' in sys.argv:
    group = opt.add_option_group ('Options specific to ' + APPNAME 
                                   + ' (configuration)')
    group.add_option ('--disable-pdf', action = 'store_true',
                      help = 'disable pdf support')
    group.add_option ('--disable-ps', action = 'store_true',
                      help = 'disable postscript support')
    group.add_option ('--disable-nls', action = 'store_true',
                      help = 'disable native language support')
    group.add_option ('--disable-dbus', action = 'store_true',
                      help = 'disable single instance mode with D-Bus')


def configure (conf):
  import Options
 
  conf.define ('PACKAGE', APPNAME)
  conf.define ('VERSION', VERSION)
  conf.define ('GETTEXT_PACKAGE', APPNAME)
  
  conf.load ('compiler_c glib2')
  conf.check_cfg (package='gtk+-2.0', uselib_store='GTK',
                  atleast_version='2.20.0', args='--cflags --libs',
                  mandatory = True)
  conf.check_cfg (package='cairo', uselib_store='CAIRO', 
                  args='--cflags --libs', mandatory = True)

  conf.start_msg('Checking for feature PDF')
  if Options.options.disable_pdf:
    message = 'disabled'
  else:
    try:
      conf.check_cfg (package = 'poppler-glib', uselib_store = 'POPPLER_GLIB', 
                      atleast_version='0.12.0', args = '--cflags --libs')
      conf.check_cfg (package = 'poppler-cairo', uselib_store = 'POPPLER_CAIRO', 
                      args = '--cflags --libs')
    except:
      pass
    if conf.env['HAVE_POPPLER_GLIB' and 'HAVE_POPPLER_CAIRO']:
      message = 'enabled'
      conf.define ('ENABLE_PDF', 1)
      conf.env['ENABLE_PDF'] = 1
    else:
      message = 'not available'
  conf.end_msg (message)

  conf.start_msg('Checking for feature PS')
  if Options.options.disable_ps:
    message = 'disabled'
  else:
    try:
      conf.check_cfg (package = 'libspectre', uselib_store = 'LIBSPECTRE', 
                      args = '--cflags --libs')
    except:
      pass
    if conf.env['HAVE_LIBSPECTRE']:
      message = 'enabled'
      conf.define ('ENABLE_PS', 1)
      conf.env['ENABLE_PS'] = 1
    else:
      message = 'not available'
  conf.end_msg (message)

  conf.start_msg('Checking for feature NLS')
  if Options.options.disable_nls:
    message = 'disabled'
  else:
    try:
      conf.check_tool ('intltool')
    except:
      pass
    if conf.env['INTLTOOL'] and conf.env['MSGFMT']:
      message = 'enabled'
      conf.define ('ENABLE_NLS', 1)
      conf.env['ENABLE_NLS'] = 1
    else:
      message = 'not available'   
  conf.end_msg (message)
  
  conf.start_msg('Checking for feature D-Bus')
  if Options.options.disable_dbus:
    message = 'disabled'
  else:
    try:
      conf.check_cfg (package = 'dbus-glib-1', uselib_store = 'DBUS-GLIB', 
                      args = '--cflags --libs')
    except:
      pass
    if conf.env['HAVE_DBUS_GLIB']:
      message = 'enabled'
      conf.define ('ENABLE_DBUS', 1)
      conf.env['ENABLE_DBUS'] = 1
    else:
      message = 'not available'
  conf.end_msg (message)

  conf.write_config_header ('config.h')
  conf.env.append_value ('CFLAGS', '-DHAVE_CONFIG_H')
  conf.env.append_value ('CFLAGS', '-Wall')

  
def build (bld):
  obj = bld.new_task_gen()
  obj.features = ['c', 'cprogram']
  obj.target = APPNAME
  obj.includes = ['.', 'src']
  obj.source = ['src/e6x-dialogs.c', 'src/e6x-page-selector.c', 
                'src/e6x-pref.c', 'src/e6x-util.c', 
                'src/e6x-window.c', 'src/e6x-document.c', 
                'src/e6x-properties-dialog.c', 'src/e6x-att-view.c', 
                'src/e6x-scale-selector.c', 'src/e6x-view.c', 
                'src/main.c', 'src/e6x-enc-document.c', 
                'src/e6x-toc-view.c']
  obj.uselib = 'GTK'
  if bld.env['ENABLE_PDF']:
    obj.source.append ('src/e6x-pdf-document.c')
    obj.uselib += ' POPPLER_GLIB POPPLER_CAIRO'
  if bld.env['ENABLE_PS']:
    obj.source.append ('src/e6x-ps-document.c')
    obj.uselib += ' LIBSPECTRE'
  if bld.env['ENABLE_DBUS']:
    obj.uselib += ' DBUS-GLIB'
  
  if bld.env['ENABLE_NLS']:
    obj = bld.new_task_gen (features = 'intltool_po',
                            podir = 'po',
                            appname = APPNAME)
    obj = bld.new_task_gen (features = 'intltool_in',
                            source = APPNAME + '.desktop.in',
                            podir = 'po',
                            flags  = ['-d', '-u', '-c'],
                            install_path = '${PREFIX}/share/applications')
  else:
    infile = open (APPNAME + '.desktop.in', 'r')
    outfile = open (APPNAME + '.desktop', 'w')
    for line in infile:
      if line != '':
        if line[0] == '_':
          outfile.write (line[1:])
        else:
          outfile.write (line)
    outfile.close ()
    infile.close ()
    bld.install_files ('${PREFIX}/share/applications', 
                       APPNAME +'.desktop')

def dist(ctx):
  from waflib import Node, Scripting
  Node.exclude_regs += ' **/*.desktop **/.intlcache **/*.tar.bz2 **/.waf*'
  Scripting.dist(ctx)


def electrix(foo):
  pass

																																					 
