# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2016 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os

from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _

from weblate.trans.data import data_dir
from weblate.trans.vcs import GitRepository, RepositoryException
from weblate.trans.util import add_configuration_error


class TransConfig(AppConfig):
    name = 'weblate.trans'
    label = 'trans'
    verbose_name = _('Weblate translations')

    def ready(self):
        # Configure merge driver for Gettext PO
        try:
            GitRepository.global_setup()
        except RepositoryException as error:
            add_configuration_error(
                'Git global setup',
                'Failed to do git setup: {0}'.format(error)
            )

        # Use it for *.po by default
        configdir = os.path.join(data_dir('home'), '.config', 'git')
        configfile = os.path.join(configdir, 'attributes')
        if not os.path.exists(configfile):
            if not os.path.exists(configdir):
                os.makedirs(configdir)
            with open(configfile, 'w') as handle:
                handle.write('*.po merge=weblate-merge-gettext-po\n')
