#!/usr/bin/env python2
 
#  Gnome15 - Suite of tools for the Logitech G series keyboards and headsets
#  Copyright (C) 2011 Brett Smith <tanktarta@blueyonder.co.uk>
#
#  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/>.

"""
DBUS System Service that is intended to replace 'lgsetled', the command line tool initially
used by the kernel driver support to set the brightness of keyboard lights (the device
files of which require root access, as they are in /sys). 
"""


import sys
import os
import glib

# Allow running from local path
path = os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), ".."))
if os.path.exists(path):
    sys.path.insert(0, path) 
    
# Logging
import gnome15.g15logging as g15logging
logger = g15logging.get_root_logger()

#
import gobject
gobject.threads_init()

# DBUS - Use to check current desktop service status or stop it
import dbus
from dbus.mainloop.glib import DBusGMainLoop
from dbus.mainloop.glib import threads_init
threads_init()
DBusGMainLoop(set_as_default=True)

# Server host class

def check_service_status(bus):
    return bus.name_has_owner('org.gnome15.SystemService')

def start_service(bus, no_trap=False,):    
    try :
        import setproctitle
        setproctitle.setproctitle(os.path.basename(os.path.abspath(sys.argv[0])))
    except ImportError as ie:
        # Not a big issue
        logger.debug("No setproctitle, process will be named 'python'", exc_info = ie)

    # Start the loop    
    try :
        import gnome15.g15system as g15system   
        service = g15system.G15SystemServiceController(bus, no_trap=no_trap)
        service.start_loop()
    except dbus.exceptions.NameExistsException as e:
        logger.debug("Gnome15 service already running", exc_info = e)
        print "Gnome15 desktop service is already running"
        sys.exit(1)
    
if __name__ == "__main__":
    import optparse
    parser = optparse.OptionParser()
    parser.add_option("-l", "--log", dest="log_level", metavar="INFO,DEBUG,WARNING,ERROR,CRITICAL",
        default="warning" , help="Log level")
    parser.add_option("-f", "--foreground", action="store_true", dest="foreground",
        default=False, help="Run desktop service in foreground.")
    parser.add_option("-s", "--session", action="store_true", dest="use_session_bus",
        default=False, help="Use the session bus instead of system bus.")
    parser.add_option("-n", "--notrap", action="store_true", dest="no_trap",
        default=False, help="Do not try to trap signals.")
    (options, args) = parser.parse_args()
    
    if options.log_level != None:      
        logger.setLevel(g15logging.get_level(options.log_level))
        
    if len(args) == 1 and ( args[0] == "stop" or args[0] == "restart" ):
        bus = dbus.SessionBus() if options.use_session_bus else dbus.SystemBus()
        if not check_service_status(bus):
            if args[0] == "stop":
                print "Gnome15 system service is not running"
        else:            
            service_object = bus.get_object('org.gnome15.SystemService', '/org/gnome15/SystemService')
            system_service = dbus.Interface(service_object, 'org.gnome15.SystemService')
            system_service.Stop()
            
    if len(args) == 0 or ( len(args) == 1 and ( args[0] == "start" or args[0] == "restart" ) ):
        bus = dbus.SessionBus() if options.use_session_bus else dbus.SystemBus()
        if check_service_status(bus):
            print "Gnome15 desktop service already running"
        else:
            if options.foreground or ( not options.foreground and os.fork() == 0 ):
                start_service(bus, options.no_trap)
