#!/usr/bin/env python

# Authors:
#   Jason Gerard DeRose <jderose@novacut.com>
#
# dmedia: distributed media library
# Copyright (C) 2010 Jason Gerard DeRose <jderose@novacut.com>
#
# This file is part of `dmedia`.
#
# `dmedia` is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# `dmedia` 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 Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with `dmedia`.  If not, see <http://www.gnu.org/licenses/>.

from dmedia.util import configure_logging
from dmedia.constants import BUS
import sys
import optparse
from os import path
import dbus
import dbus.service
import dbus.mainloop.glib
import gobject


parser = optparse.OptionParser()
parser.add_option('--bus',
    default=BUS,
    help='D-Bus bus name; default is %r' % BUS,
)
parser.add_option('--couchdir',
    metavar='DIR',
    default=None,
    help='temporary directory for desktopcouch Context',
)
parser.add_option('--no-gui',
    action='store_true',
    default=False,
    help='run without NotifyOSD and Application Indicators',
)


def exit(msg, code=1):
    parser.print_help()
    print('ERROR: ' + msg)
    sys.exit(code)


if __name__ == '__main__':
    (options, args) = parser.parse_args()
    if not (options.couchdir is None or path.isdir(options.couchdir)):
        exit('couchdir not a directory: %r' % options.couchdir)
    configure_logging('service')
    from dmedia import service
    mainloop = gobject.MainLoop()
    obj = service.DMedia(mainloop.quit,
        options.bus, options.couchdir, options.no_gui
    )
    mainloop.run()
