#! /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__))

# If GNOME Activity Journal is installed system-wide, as indicated by
# this file residing in $prefix/bin, we expect to find the remaining code
# in $prefix/share/gnome-activity-journal/.
if os.path.basename(our_dir) == 'bin':
    sys.path.insert(0, os.path.join(os.path.dirname(our_dir),
                                    'share/gnome-activity-journal'))

# Check for critical modules from other repositories.
# 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']

    # 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.bindtextdomain('gnome-activity-journal', config.GETTEXT_PATH)
gettext.textdomain('gnome-activity-journal')
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()

from src.external import CLIENT, CLIENT_VERSION

if CLIENT_VERSION is None:
    sys.exit(1)
elif CLIENT_VERSION < [0, 8, 0]:
    print "You need Zeitgeist 0.8.0 or later for the GNOME Activity Journal to work."
    print "https://launchpad.net/zeitgeist"
    sys.exit(1)

from src.main import PortalWindow

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