#!/usr/bin/python

# Copyright (C) 2009-2011:
#    Gabes Jean, naparuba@gmail.com
#    Gerhard Lausser, Gerhard.Lausser@consol.de
#    Gregory Starck, g.starck@gmail.com
#    Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken.  If not, see <http://www.gnu.org/licenses/>.


import os
import sys
import optparse

try:
    from shinkensolutions.localinstall import VERSION
    import shinken
except ImportError:
    # If importing shinken fails, try to load from current directory
    # or parent directory to support running without installation.
    # Submodules will then be loaded from there, too.
    import imp
    imp.load_module('shinken', *imp.find_module('shinken', [os.path.realpath("."), os.path.realpath(".."), os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "..")]))
    import shinken
    # Ok we should add the shinken root directory to our sys.path so our sons
    # will be able to use the shinken import without problems
    shinken_root_path = os.path.dirname(os.path.dirname(shinken.__file__))
    os.environ['PYTHONPATH'] = os.path.join(os.environ.get('PYTHONPATH', ''), shinken_root_path)


from shinken.daemons.providerdaemon import Provider
from shinkensolutions.localinstall import VERSION

parser = optparse.OptionParser(
    "%prog [options]", version="%prog " + VERSION)
parser.add_option('-c', '--config',
                  dest="config_file", metavar="INI-CONFIG-FILE",
                  help='Config file')
parser.add_option('-d', '--daemon', action='store_true',
                  dest="is_daemon",
                  help="Run in daemon mode")
parser.add_option('-r', '--replace', action='store_true',
                  dest="do_replace",
                  help="Replace previous running broker")
parser.add_option('--debugfile', dest='debug_file',
                  help=("Debug file. Default: not used "
                        "(why debug a bug free program? :) )"))
parser.add_option("-p", "--profile",
                  dest="profile",
                  help="Dump a profile file. Need the python cProfile librairy")
parser.add_option("--daemon-id",
                  dest="daemon_id", type="int", default=0,
                  help="The daemon identifier. Optionnal, will use 0  if not provide")

opts, args = parser.parse_args()
if args:
    parser.error("Does not accept any argument.")

# Protect for windows multiprocessing that will RELAUNCH all
if __name__ == '__main__':
    daemon = Provider(debug=opts.debug_file is not None, **opts.__dict__)
    if not opts.profile:
        daemon.main()
    else:
        # For perf tuning:
        import cProfile
        cProfile.run('''daemon.main()''', opts.profile)
