#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import os
import subprocess

from setuptools import setup, find_packages


def create_mo_files():
    podir = "po"
    mo = []
    for po in os.listdir(podir):
        if po.endswith(".po"):
            os.makedirs("{}/{}/LC_MESSAGES".format(podir, po.split(".po")[0]), exist_ok=True)
            mo_file = "{}/{}/LC_MESSAGES/{}".format(podir, po.split(".po")[0], "ufficiozero-software.mo")
            msgfmt_cmd = 'msgfmt {} -o {}'.format(podir + "/" + po, mo_file)
            subprocess.call(msgfmt_cmd, shell=True)
            mo.append(("/usr/share/locale/" + po.split(".po")[0] + "/LC_MESSAGES",
                       ["po/" + po.split(".po")[0] + "/LC_MESSAGES/ufficiozero-software.mo"]))
    return mo


changelog = "debian/changelog"
version = "0.1.0"
if os.path.exists(changelog):
    head = open(changelog).readline()
    try:
        version = head.split("(")[1].split(")")[0]
    except:
        print("debian/changelog format is wrong for get version")
    f = open("src/__version__", "w")
    f.write(version)
    f.close()

data_files = [
                 ("/usr/bin", ["ufficiozero-software"]),
                 ("/usr/share/applications",
                  ["tr.org.ufficiozero.software.desktop",
                   "tr.org.ufficiozero.software-open.desktop"]),
                 ("/usr/share/polkit-1/actions",
                  ["tr.org.ufficiozero.pkexec.ufficiozero-software.policy"]),
                 ("/usr/share/ufficiozero/ufficiozero-software/css",
                  ["css/adw.css",
                   "css/all.css",
                   "css/base.css"]),
                 ("/usr/share/ufficiozero/ufficiozero-software/images",
                  ["images/rating.svg",
                   "images/rating-unrated.svg",
                   "images/rating-hover-empty.svg",
                   "images/rating-hover-full.svg",
                   "images/rating-0.svg",
                   "images/rating-0-3.svg",
                   "images/rating-0-5.svg",
                   "images/rating-0-8.svg",
                   "images/rating-1.svg"]),
                 ("/usr/share/ufficiozero/ufficiozero-software/ui",
                  ["ui/MainWindow.glade"]),
                 ("/usr/share/ufficiozero/ufficiozero-software/src",
                  ["src/Actions.py",
                   "src/AppDetail.py",
                   "src/AppImage.py",
                   "src/AppRequest.py",
                   "src/AutoAptUpdate.py",
                   "src/CellRendererButton.py",
                   "src/GnomeComment.py",
                   "src/GnomeRatingServer.py",
                   "src/Group.py",
                   "src/Logger.py",
                   "src/Main.py",
                   "src/MainWindow.py",
                   "src/Package.py",
                   "src/PardusComment.py",
                   "src/Server.py",
                   "src/SysActions.py",
                   "src/UserSettings.py",
                   "src/Utils.py",
                   "src/__version__"]),
                 ("/usr/share/icons/hicolor/scalable/apps/",
                  ["images/ufficiozero-software.svg"]),
                 ("/usr/share/mime/packages/",
                  ["ufficiozero-software.xml"]),
                 ("/var/lib/polkit-1/localauthority/50-local.d/",
                  ["ufficiozero-software-group.pkla"]),
                 ("/etc/ufficiozero/",
                  ["ufficiozero-software.conf"])
             ] + create_mo_files()

setup(
    name="Pardus Software",
    version=version,
    packages=find_packages(),
    scripts=["ufficiozero-software"],
    install_requires=["PyGObject"],
    data_files=data_files,
    author="Fatih Altun",
    author_email="fatih.altun@ufficiozero.org.tr",
    description="UfficioZero Software Center",
    license="GPLv3",
    keywords="software center",
    url="https://www.ufficiozero.org.tr",
)
