#!/usr/bin/python3

# 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/>.
#
#
# Copyright (C) 2024 Tim Lauridsen
#
#

import os
import sys
import signal
import locale
import shutil
import gettext

is_local = "False" == "True"
pkgdatadir = "/usr/share/yumex"
localedir = "/usr/share/locale"

sys.dont_write_bytecode = True

signal.signal(signal.SIGINT, signal.SIG_DFL)
gettext.install("yumex", localedir)

locale.bindtextdomain("yumex", localedir)
locale.textdomain("yumex")


if __name__ == "__main__":
    if is_local:
        # In the local use case, use yumex module from the sourcetree
        sys.path.insert(1, "/usr/bin/python3")

        # In the local use case the installed schemas go in <builddir>/data
        os.environ["XDG_DATA_DIRS"] = "/usr/share/yumex:" + os.environ.get("XDG_DATA_DIRS", "")

        shutil.copyfile(
            os.path.join("/builddir/build/BUILD/yumex-5.4.0/noarch-rosa-linux-gnu", "yumex", "constants.py"),
            os.path.join("/builddir/build/BUILD/yumex-5.4.0", "yumex", "constants.py"),
        )

    import gi

    gi.require_version("Flatpak", "1.0")
    gi.require_version("Gtk", "4.0")
    gi.require_version("Adw", "1")

    from gi.repository import Gio  # type: ignore

    resource = Gio.Resource.load(os.path.join(pkgdatadir, "yumex.gresource"))
    Gio.Resource._register(resource)

    from yumex import main

    sys.exit(main.main())
