#! /usr/bin/env python
# -.- coding: utf-8 -.-
#
# GNOME Activity Journal
#
# Copyright © 2009 Seif Lotfy <seif@lotfy.com>
# Copyright © 2010 Siegfried Gevatter <siegfried@gevatter.com>
# Copyright © 2010 Peter Lund <peterfirefly@gmail.com>
#
# 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/>.

import os
import sys
import gtk
import gettext
import optparse

# Where this file lives
our_dir = os.path.abspath(os.path.dirname(__file__))

# Check for critical modules from other repositories first.
# Support side-by-side or nested repositories during development.
#
# If zeitgeist is installed locally (with --prefix=<home>/.local) then Python
# should have added ~/.local/lib/python<ver>/site-packages to the search path
# automatically.  If it hasn't, then take a closer look at site.USER_SITE,
# site.USER_BASE, and site.ENABLE_USER_SITE.
try:
    # repo/module names
    other_repos = ['zeitgeist', 'fungtk']

    # paths of potential repos next to us and below us
    repo_paths = ([os.path.join(our_dir, '..', repo) for repo in other_repos] +
                  [os.path.join(our_dir, '.' , repo) for repo in other_repos])

    # look for (and import) the needed modules
    from imp import load_module, find_module
    for module in other_repos:
        m = find_module(module, repo_paths + sys.path)
        if "--debug" in sys.argv: # OptParse isn't instantiated yet, here!
            print "Using the \"%s\" module from %s" % (module, os.path.abspath(m[1]))
        load_module(module, *m)

except ImportError, e:
    print "-" * 60
    print "ERROR: %s." % e.message
    print "Did you remember to fetch the 'lp:zeitgeist' and 'lp:fungtk' repositories?"
    print
    print "Please see the README file for more information, or contact us"
    print "in IRC channel #zeitgeist on Freenode (irc.freenode.net)."
    print "-" * 60
    sys.exit(1)

from src import config

# Import zeitgeist.datamodel before running gettext.install, so that it doesn't
# override the location we set (ie., "build/mo").
from zeitgeist import datamodel

gettext.install('gnome-activity-journal', config.GETTEXT_PATH, unicode=True)

parser = optparse.OptionParser(version=config.VERSION)
parser.add_option("--debug",
    action = "store_true", default=False, dest="debug",
    help = _("print additional debugging information"))
options, arguments = parser.parse_args()

try:
    from zeitgeist.client import ZeitgeistClient
    CLIENT = ZeitgeistClient()
except RuntimeError, e:
    print "%s: %s" % (_("ERROR"), _("Unable to connect to Zeitgeist:"))
    print "%s" % e
    sys.exit(1)

if CLIENT.get_version() < [0, 3, 1, 99]:
    print "You need Zeitgeist 0.3.2 or later for the GNOME Activity Journal to work."
    print "https://launchpad.net/zeitgeist"
    sys.exit(1)

from src.main import Portal

if __name__ == "__main__":
    portal = Portal()
    gtk.gdk.threads_init()
    gtk.main()
