#!/usr/bin/python
# nicked off gwibber

import os, sys, gobject, dbus

from dbus.mainloop.glib import DBusGMainLoop
import locale, gettext
from hamster import defs

DBusGMainLoop(set_as_default=True)
loop = gobject.MainLoop()

if "org.gnome.Hamster" in dbus.SessionBus().list_names():
    print "Found hamster-service already running, exiting"
    quit()


LAUNCH_DIR = os.path.abspath(sys.path[0])
SOURCE_DIR = LAUNCH_DIR
STORAGE = os.path.join(SOURCE_DIR, "hamster", "storage.py")

if __name__ == '__main__':
    # Setup i18n
    gettext.install("hamster-applet", unicode=True)
    locale_dir = os.path.abspath(os.path.join(defs.DATA_DIR, "locale"))
    for module in (gettext, locale):
        module.bindtextdomain('hamster-applet', locale_dir)
        module.textdomain('hamster-applet')

        if hasattr(module, 'bind_textdomain_codeset'):
            module.bind_textdomain_codeset('hamster-applet','UTF-8')

    # adjust path if running from source
    if os.path.exists(SOURCE_DIR):
        sys.path.insert(0, os.path.realpath(os.path.dirname(SOURCE_DIR)))


    from hamster import db
    print "hamster-service up"

    storage = db.Storage(loop)
    loop.run()
