#!/usr/bin/env python

Import('env', 'user_options_dict')

# .po handling code stolen from Ardour

#
# mo_builder: builder function for (binary) message catalogs (.mo)
# first source:  .po file
#

def mo_builder(target,source,env):
    args = [ 'msgfmt',
             '-c',
             '-o',
             target[0].get_path(),
             source[0].get_path()
           ]
    return os.spawnvp(os.P_WAIT, 'msgfmt', args)

mo_bld = Builder(action = mo_builder)
env.Append(BUILDERS = {'MoBuild' : mo_bld})

#
# po_builder: builder function to copy po files to the parent directory while updating them
# first source:  .po file
# second source: .pot file
#

def po_builder(target,source,env):
    trgt = str(target[0])
    args = [ 'msgmerge',
             '-v',
             '--output-file=' + trgt,
             str(source[0]),
             str(source[1]),
           ]
    print 'Updating ' + trgt
    return os.spawnvp(os.P_WAIT, 'msgmerge', args)

po_bld = Builder (action = po_builder)
env.Append(BUILDERS = {'PoBuild' : po_bld})

import os, glob
cwd = str(Dir('.').srcnode())
PackageName = 'bombono-dvd'

po_files  = [ os.path.basename(po)  for po in glob.glob(os.path.join(cwd, '*.po')) ]
languages = [ po.replace('.po', '') for po in po_files ]

for lang in languages:
    mo_file = 'locale/' + lang + '/LC_MESSAGES/bombono-dvd.mo'
    po_file = lang + ".po"
    po_src_file = env.File(po_file).srcnode()
    tgt = env.MoBuild(mo_file, po_src_file)

    env.Alias('msgupdate', tgt)
    inst_tgt = env.InstallAs(os.path.join(user_options_dict['DEST_PREFIX'], 'share', mo_file), tgt)
    env.Alias("install", inst_tgt)

    # :TRICKY: update po files from generated .pot, not from kept one
    env.PoBuild(po_file, [po_src_file, 'bombono-dvd.pot'])

# :BUG: updated *.mo (with scons msgudpate) in build/po/locale do not install to DEST_PREFIX/share/locale
# if the folder exists already. So we install them explicitly (see above).
#import BuildVars as BV
#env.Alias("install", BV.InstallDir(env, os.path.join(user_options_dict['DEST_PREFIX'], 'share'), 'locale'))

#
# pot_builder: builder function for message templates (.pot)
# source: list of C/C++ etc. files to extract messages from
#

def pot_builder(target,source,env):
    args = [ 'xgettext',
             '--keyword=_',
             '--keyword=N_',
             '--keyword=C_:1c,2',
             '--keyword=F_',
             '--boost',
             '--keyword=BF_', # = boost::format(gettext()), boost-format comment is auto added
             '--keyword=DOTS_',
             '--keyword=SMCLN_',
             '--add-comments',
             '--from-code=UTF-8',
             '-o', target[0].get_path(),
             "--default-domain=" + PackageName,
             '''--copyright-holder="Ilya Murav'jov"''' ]
    args += [ src.get_path() for src in source ]
    
    return os.spawnvp(os.P_WAIT, 'xgettext', args)

pot_bld = Builder(action = pot_builder)
env.Append(BUILDERS = {'PotBuild' : pot_bld})

sources = user_options_dict['XGETTEXT_SOURCES'] # + ['#src/mgui/tests/test_gettext.cpp']
env.Alias('potupdate', env.PotBuild('bombono-dvd.pot', sources))

