#!/usr/bin/python
# -*-python-*-

# Author: Robert Park <r@robru.ca>, (C) 2010-2015
# Copyright: See COPYING file included with this distribution.

"""GottenGeography Application Launcher

Check for all needed dependencies, then launch app.
"""

import sys
import gi

try:
    gi.require_version('Gtk', '3.0')
    gi.require_version('Champlain', '0.12')
    gi.require_version('GtkChamplain', '0.12')
    gi.require_version('GtkClutter', '1.0')
    gi.require_version('GExiv2', '0.10')
    from gi.repository import Gtk
except ValueError as err:
    sys.exit(err)


def need(dependency):
    """Exit the program and tell the user what dependency was missing."""
    sys.exit('GottenGeography requires at least ' + dependency)


try:
    assert sys.version_info > (3, 1)
except Exception as err:
    print(err)
    need('Python 3.2')


try:
    from dateutil.parser import parse
    parse('2017-05-14')  # Suppress pyflakes warning.
except Exception as err:
    print(err)
    need('python3-dateutil 2.0')


try:
    from gi.repository import Champlain
    # Raises AttributeError with libchamplain 0.8 or earlier.
    assert Champlain.PathLayer

    # Raises TypeError with libchamplain 0.12.0
    assert [source.get_id() for source in
            Champlain.MapSourceFactory.dup_default().get_registered()]
except Exception as err:
    print(err)
    need('libchamplain 0.12.2 (with introspection, gir1.2-champlain-0.12)')


try:
    from gi.repository import GtkChamplain
    assert GtkChamplain
except Exception as err:
    print(err)
    need('libchamplain 0.12.2 (with introspection, gir1.2-gtkchamplain-0.12)')


try:
    from gi.repository import GtkClutter
    assert GtkClutter
except Exception as err:
    print(err)
    need('clutter (with introspection, gir1.2-gtkclutter-1.0)')


try:
    from gi.repository import GExiv2
    assert GExiv2.Metadata().__getitem__
except Exception as err:
    print(err)
    need('gexiv2 0.4.90 (with introspection, gir1.2-gexiv2-*)')


OLD_HOOK = sys.excepthook


def new_hook(*args):
    """Exit cleanly if there was an unhandled exception."""
    OLD_HOOK(*args)
    while Gtk.main_level():
        Gtk.main_quit()
    sys.exit()
sys.excepthook = new_hook


# If we got this far, it looks like we have everything we need!
# Time to launch the app.


from gg.app import GottenGeography
GottenGeography(not sys.argv[1:]).run(sys.argv)
