Import('env', 'install_perms')
from os.path import join, basename

languages = []
for src in env.Glob('*.po'):
    lang = basename(str(src))[:-3]
    languages.append(lang)
    dst = join(lang, "LC_MESSAGES", 'libmypaint.mo')
    env.Command(dst, src, 'msgfmt $SOURCE -o $TARGET')

# translations
for lang in languages:
    install_perms(env, '$prefix/share/locale/%s/LC_MESSAGES' % lang,
                 '%s/LC_MESSAGES/libmypaint.mo' % lang)

lang = ARGUMENTS.get('translate')
if lang:
    if lang == 'all':
        # For programmers and program maintainers.
        # Update the .pot file and all po/*.po.
        # Use this after changing any source string.
        # WebLate will receive a new template and flag string changes
        # for review by translators.
        env.Execute('cd .. && ls *.[ch] | sort > po/POTFILES.in')
        env.Execute('intltool-update -g libmypaint --pot')
        translate = languages
    elif lang == 'pot':
        # For programmers and program maintainers.
        # Update the .pot file only.
        # Handy for doing local diffs,
        # but it's best not to commit alone - .po files on
        # WebLate sould be kept in sync with their .pot.
        env.Execute('cd .. && ls *.[ch] | sort > po/POTFILES.in')
        env.Execute('intltool-update -g libmypaint --pot')
        translate = []
    else:
        # For individual language maintainers.
        # Update the named language only from the most recently .pot
        # and do not update the .pot file itself.
        translate = [lang]
    for lang in translate:
        env.Execute('intltool-update -g libmypaint --dist %s' % lang)

Return('languages')
