#!/usr/bin/python3

import sys
import os
import locale
import gettext

sys.path.insert(1, '/usr/lib/python3.9')
sys.path.insert(1, '/usr/lib/python3.9/site-packages/')

import gi
gi.require_version('Gtk', '3.0')
gi.require_version("Handy", "1")
from gi.repository import Gio, Gtk

localedir = '/usr/share/locale'
pkgdatadir = '/usr/share/passwordsafe'
extensiondir = '/usr/lib/passwordsafe'


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__":
    install_excepthook()

    try:
        locale.bindtextdomain('passwordsafe', localedir)
        locale.textdomain('passwordsafe')
    except AttributeError as e:
        # Python built without gettext support doesn't have bindtextdomain() and textdomain()
        print("Couldn't bind the gettext translation domain. Some translations won't work.\n{}".format(e))

    gettext.bindtextdomain('passwordsafe', localedir)
    gettext.textdomain('passwordsafe')

    resource = Gio.resource_load(os.path.join(pkgdatadir, 'passwordsafe.gresource'))
    Gio.Resource._register(resource)

    from passwordsafe.application import Application

    if "" == 'Devel':
    	Application.development_mode = True
    	Application.application_id = 'org.gnome.PasswordSafeDevel'
    else:
    	Application.development_mode = False

    app = Application()

    exit_status = app.run(sys.argv)
    sys.exit(exit_status)
