#!/usr/bin/env python3

import gi
import os
import sys

gi.require_version('Gio', '2.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gio, Gtk
gtk_version = Gtk.get_major_version(), Gtk.get_minor_version()
if gtk_version < (3, 22):
    print("Version of GTK is too old, 3.22 required", file=sys.stderr)
    sys.exit(1)


resource = Gio.resource_load(os.path.join('/usr/share/piper', 'piper.gresource'))
Gio.Resource._register(resource)


def install_excepthook():
    """Make sure we exit when an unhandled exception occurs."""
    old_hook = sys.excepthook

    def new_hook(etype, evalue, etb):
        old_hook(etype, evalue, etb)
        while Gtk.main_level():
            Gtk.main_quit()
        sys.exit()
    sys.excepthook = new_hook


if __name__ == "__main__":
    import gettext
    import locale
    import signal
    from piper.application import Application

    install_excepthook()

    locale.bindtextdomain('piper', '/usr/share/locale')
    locale.textdomain('piper')
    gettext.bindtextdomain('piper', '/usr/share/locale')
    gettext.textdomain('piper')

    signal.signal(signal.SIGINT, signal.SIG_DFL)
    exit_status = Application().run(sys.argv)
    sys.exit(exit_status)
