#!/usr/bin/env python

"""

Gwibber Client
SegPhault (Ryan Paul) - 05/29/2007

"""

import sys, logging, gtk, optparse
from os.path import join, dirname, exists, realpath, abspath


######################################################################
# Options and configuration
def parse_opts():
    usage = "usage: %prog [options] <config-file>"

    oparse = optparse.OptionParser(usage=usage)

    oparse.add_option("-v", "--verbose", action="store_true",
                      help="Verbose messages", default=False)
    oparse.add_option("-d", "--debug", action="store_true",
                      help="Debug messages", default=False)

    opts, rest = oparse.parse_args()
    return opts
    
opts = parse_opts()

if opts.debug:
    logging.root.setLevel(logging.DEBUG)
elif opts.verbose:
    logging.root.setLevel(logging.INFO)


######################################################################
# Setup path

LAUNCH_DIR = abspath(sys.path[0])
logging.debug("Launched from %s", LAUNCH_DIR)
source_tree_gwibber = join(LAUNCH_DIR, "..", "gwibber")

# If we were invoked from a Gwibber source directory add that as the
# preferred module path ...
if exists(join(source_tree_gwibber, "client.py")):
    logging.info("Running from source tree; adjusting path")
    sys.path.insert(0, realpath(dirname(source_tree_gwibber)))
    try:
        from gwibber.client import GwibberClient
    finally:
        del sys.path[0]
else:
    logging.debug("Assuming path is correct")
    from gwibber.client import GwibberClient


######################################################################
# Go...

import dbus, dbus.exceptions
from gwibber.gintegration import DBUS_NAME, DBUS_PATH

try:
    bus = dbus.SessionBus()
    proxy = bus.get_object(DBUS_NAME, DBUS_PATH)
    iface = dbus.Interface(proxy, DBUS_NAME)

    logging.info("Found existing Gwibber interface: invoking...")
    iface.external_invoke()

except dbus.exceptions.DBusException:
    logging.info("No existing Gwibber session: starting...")
    GwibberClient()
    gtk.main()



