#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This file is part of pacman-mirrors.
#
# pacman-mirrors 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.
#
# pacman-mirrors 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 pacman-mirrors.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Ramon Buldó <rbuldo@gmail.com>

import os
import shutil
from manjariando_mirrors import manjariando_mirrors
from manjariando_mirrors import i18n


_ = i18n.language.gettext


def move_custom_country():
    """
    Move custom country file from /etc/pacman.d/manjariando-mirrors/ to
    /var/lib/manjariando-mirrors/
    """
    print(_("Moving 'Custom' mirrors file to ") + "/var/lib/manjariando-mirrors/")
    os.makedirs("/var/lib/manjariando-mirrors", mode=0o755, exist_ok=True)
    try:
        shutil.move("/etc/pacman.d/manjariando-mirrors/Custom",
                    "/var/lib/manjariando-mirrors/Custom")
    except OSError as e:
        print(_("Warning: Cannot move '{filename}' to '{filename2}': {error}"
              .format(filename=e.filename,
                      filename2=e.filename2,
                      error=e.strerror)))

if os.path.isfile("/etc/pacman.d/manjariando-mirrors/Custom"):
    if os.getuid() == 0:
        move_custom_country()

try:
    pm = manjariando_mirrors.ManjariandoMirrors()
    pm.run()
except KeyboardInterrupt:
    print("\n" + _("Error: interrupted by the user."))
    exit(1)
