#! /usr/bin/env python

#   Copyright (C) 2007-2011 Ahmet Öztürk (aoz_2@yahoo.com)
#
#   This file is part of Lifeograph.
#
#   Lifeograph is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Lifeograph is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Lifeograph.  If not, see <http://www.gnu.org/licenses/>.


# Parts of this script was created using the waf script of Midori as a source
# of inspiration. Thank you guys.


import Options


lifeograph_sources = [
    'src/settings.cpp', 'src/diary.cpp', 'src/diarydata.cpp', 'src/entry.cpp',
    'src/gui_tag.cpp',
    'src/dialog_export.cpp', 'src/dialog_password.cpp', 'src/dialog_preferences.cpp',
    'src/helpers.cpp',
    'src/lifeobase.cpp', 'src/lifeograph.cpp', 'src/main.cpp',
    'src/view_entry.cpp','src/views.cpp',
    'src/widget_textview.cpp', 'src/widget_entrylist.cpp', 'src/widget_chart.cpp',
    'src/widget_panel_exp.cpp', 'src/panel_main.cpp', 'src/panel_diary.cpp',
    'src/undo.cpp'
    ]

APPNAME='lifeograph'
VERSION='0.7.4'

srcdir = '.'
blddir = 'build'


def set_options(opt):
    opt.tool_options('compiler_cxx')
    opt.tool_options('intltool')

    opt.add_option('--update-po', action='store_true', default=False,
        help='update the message catalogs for translation', dest='update_po')

    opt.add_option('--debug', action='store_true', default=False,
        help='create a debug build', dest='debug_build')


def configure(conf):
    import os

    # checks
    conf.check_tool('compiler_cxx')
    conf.check_cfg(package='gtkmm-2.4', atleast_version='2.16', args='--cflags --libs', uselib_store='gtkmm', mandatory=1)
    conf.check_cfg(package='gtkspell-2.0', args='--cflags --libs', uselib_store='gtkspell', mandatory=1)
    conf.check(lib='gcrypt',  uselib_store='gcrypt', mandatory=True)
    conf.check_tool('intltool')

    # defines
    conf.define('PACKAGE', APPNAME)
    conf.define('VERSION', VERSION)
    # TODO: use the methods explained here: http://live.gnome.org/ThemableAppSpecificIcons
    # and here: http://live.gnome.org/GnomeGoals/AppIcon)
    conf.define('PIXMAPDIR', os.path.join(conf.env['PREFIX'], 'share/pixmaps'))
    conf.define('ICONDIR', os.path.join(conf.env['PREFIX'], 'share', APPNAME, 'icons'))
    conf.define('UIDIR', os.path.join(conf.env['PREFIX'], 'share', APPNAME, 'ui'))

    # default prefix
    #conf.env.PREFIX = '/usr'

    if Options.options.debug_build:
        conf.env.CXXFLAGS = ['-DHAVE_CONFIG_H', '-Wall', '-O0', '-g']
        conf.define('LIFEOGRAPH_DEBUG_BUILD', 1)
    else:
        conf.env.CXXFLAGS = ['-DHAVE_CONFIG_H', '-Wall', '-O2']
        conf.env.LINKFLAGS = ['-s']

    conf.write_config_header('config.h')

    if Options.options.update_po:
        import Utils

        os.chdir('%s/po' % srcdir)
        try:
            try:
                size_old = os.stat(APPNAME + '.pot').st_size
            except:
                size_old = 0
            Utils.exec_command('intltool-update -p -g %s' % APPNAME)
            size_new = os.stat(APPNAME + '.pot').st_size
            #if size_new <> size_old:
            Utils.pprint ('YELLOW', "Updated po template.")
            try:
                Utils.exec_command('intltool-update -r -g %s' % APPNAME)
                Utils.pprint ('YELLOW', "Updated translations.")
            except:
                Utils.pprint ('RED', "Failed to update translations.")
            #else:
                #print('Po template is up to date.')
        except:
            Utils.pprint('RED', 'Failed to generate po template.')
        os.chdir('..')


def build(bld):
    binary = bld.new_task_gen(
        features    = 'cxx cprogram',
        source      = lifeograph_sources,
        uselib      = 'gtkmm gtkspell gcrypt',
        includes    = '.',
        target      = 'lifeograph')

    if bld.env['INTLTOOL']:
        bld.new_task_gen(
            features        = 'intltool_po',
            podir           = 'po',
            install_path    = '${LOCALEDIR}',
            appname         = 'lifeograph')

    # .desktop
    if bld.env['INTLTOOL']:
        bld.new_task_gen(
            features        = 'intltool_in',
            source          = 'lifeograph.desktop.in',
            flags           = [ '-d', '-q', '-u', '-c' ],
            install_path    = '${DATADIR}/applications'
            )

    # lifeograph.1
    """bld.new_task_gen(
        features        = 'subst',
        source          = 'lifeograph.1.in',
        target          = 'lifeograph.1',
        dict            = { 'VERSION' : VERSION,
                            'LIFEOGRAPH_DATA_DIR': bld.env['DATADIR'] + '/lifeograph' },
        install_path    = '${MANDIR}/man1'
    )"""

    # icons
    bld.install_as('${DATADIR}/icons/hicolor/16x16/apps/lifeograph.png',
                   'icons/lifeograph-16.png')

    bld.install_as('${DATADIR}/icons/hicolor/22x22/apps/lifeograph.png',
                    'icons/lifeograph-22.png')
    
    bld.install_as('${DATADIR}/icons/hicolor/24x24/apps/lifeograph.png',
                   'icons/lifeograph-24.png')

    bld.install_as('${DATADIR}/icons/hicolor/32x32/apps/lifeograph.png',
                   'icons/lifeograph-32.png')

    bld.install_as('${DATADIR}/icons/hicolor/48x48/apps/lifeograph.png',
                   'icons/lifeograph-48.png')

    bld.install_as('${DATADIR}/icons/hicolor/scalable/apps/lifeograph.svg',
                   'icons/scalable/lifeograph.svg')

    bld.install_as('${DATADIR}/pixmaps/lifeograph.png',
                   'icons/lifeograph-48.png')

    bld.install_files('${DATADIR}/lifeograph/icons', [ 'icons/category_tags-16.png',
                                                       'icons/category_tags-32.png',
                                                       'icons/chapter-16.png',
                                                       'icons/chapter-32.png',
                                                       'icons/diary-16.png',
                                                       'icons/diary-32.png',
                                                       'icons/entry-16.png',
                                                       'icons/entry-32.png',
                                                       'icons/favorite-16.png',
                                                       'icons/filter-16.png',
                                                       'icons/filter_remove-16.png',
                                                       'icons/month-16.png',
                                                       'icons/month-32.png',
                                                       'icons/tag-16.png',
                                                       'icons/tag-32.png' ] )

    bld.install_files( '${DATADIR}/pixmaps/lifeograph/backgrounds',
                       'backgrounds/*.png')

    # ui
    bld.install_files('${DATADIR}/lifeograph/ui', 'ui/*.ui')
