#!/usr/bin/python

import sys
import os
import locale
import gettext

if 'LOLLYPOP_TRACE' in os.environ:
    from pycallgraph import PyCallGraph
    from pycallgraph.output import GraphvizOutput
# Make sure we'll find the pygobject module, even in JHBuild
sys.path.insert(1, '/usr/lib/python3.7/site-packages')

from gi.repository import Gio

localedir = '/usr/share/locale'
pkgdatadir = '/usr/share/lollypop'

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

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()

    locale.bindtextdomain('lollypop', localedir)
    locale.textdomain('lollypop')
    gettext.bindtextdomain('lollypop', localedir)
    gettext.textdomain('lollypop')

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

    from lollypop.application import Application
    app = Application("1.1.1")
    if 'LOLLYPOP_TRACE' in os.environ:
        graphviz = GraphvizOutput()
        graphviz.output_file = 'lollypop.png'
        with PyCallGraph(output=graphviz):
            exit_status = app.run(sys.argv)
            sys.exit(exit_status)
    else:
        exit_status = app.run(sys.argv)
        sys.exit(exit_status)
