from distutils.core import setup
from glob import glob

import os, subprocess

PACKAGE = "domainjoin"
GET_TEXT_DOMAIN = PACKAGE

def create_mo_files():
    data_files = []
    linguas_file = 'po/LINGUAS'
    install_base_path = 'share'
    with open(linguas_file, 'r') as f:
        all_linguas = [x.strip() for x in f.readlines()]
        for lang in all_linguas:
            po_file = "po/{}.po".format(lang)
            
            base_path = os.path.join('locale', lang, 'LC_MESSAGES')
            mo_dir = os.path.join('po', base_path)
            mo_file = os.path.join(mo_dir, "{}.mo".format(GET_TEXT_DOMAIN))
            if not os.path.exists(mo_dir):
                os.makedirs(mo_dir)

            msgfmt_cmd = 'msgfmt {} -o {}'.format(po_file, mo_file)
            subprocess.call(msgfmt_cmd, shell=True)

            data_files.append((os.path.join(install_base_path, base_path), [mo_file]))

    return data_files

setup(name=PACKAGE,
    version='1.0',
    description='Manage domains in computer',
    author='Aldy León',
    author_email='algarcia@uci.cu',
    packages=[
        'libdomainjoin',
        'DomainjoinGUI'
    ],
    scripts=[
        'domain-join-service',
        'domain-join'
    ],
    data_files=[
        ('share/domain-join/', glob('data/*.ui') + glob('data/*.png') + glob('data/*.svg') + glob('data/*.css')),
        ('share/applications/', ['data/cu.nova.domainjoin.desktop']),
        ('share/dbus-1/system-services/', ['data/cu.nova.domainjoin.controller.service']),
        ('share/polkit-1/actions/', ['data/cu.nova.domainjoin.controller.policy']),
        ('/etc/dbus-1/system.d/', ['data/cu.nova.domainjoin.controller.conf']),
    ] + create_mo_files()
)
