#!/usr/bin/python

# Copyright (C) 2013:
#    Gabes Jean, j.gabes@shinken-solutions.com
#
# This file is part of Shinken Enterprise, all rights reserved.


import os
import sys
import optparse

# # We try to raise up recursion limit on
# # but we don't have resource module on windows
# if os.name != 'nt':
#     import resource
#
#     # All the pickle will ask for a lot of recursion, so we must make
#     # sure to set it at a high value. The maximum recursion depth depends
#     # on the Python version and the process limit "stack size".
#     # The factors used were acquired by testing a broad range of installations
#     stacksize_soft, stacksize_hard = resource.getrlimit(3)
#     if sys.version_info < (2, 6):
#         sys.setrecursionlimit(int(stacksize_soft * 0.65 + 1100))
#     elif sys.version_info < (3,):
#         sys.setrecursionlimit(int(stacksize_soft * 1.9 + 3200))
#     else:
#         sys.setrecursionlimit(int(stacksize_soft * 2.4 + 3200))

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])), "..")]))
    from shinkensolutions.localinstall import VERSION

from shinken.synchronizer.synchronizerdaemon import Synchronizer

parser = optparse.OptionParser("%prog [options] -c configfile [-c additional_config_file]", version="%prog: " + VERSION)
parser.add_option('-c', '--config', action='append', dest="config_files", metavar="CONFIG-FILE", help=('Config file (your nagios.cfg). Multiple -c can be used, it will be like if all files was just one'))
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 arbiter")
parser.add_option('--debugfile', dest='debug_file', help=("Debug file. Default: not used (why debug a bug free program? :) )"))
parser.add_option("-v", "--verify-config", dest="verify_only", action="store_true", help="Verify config file and exit")
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 not opts.config_files:
    parser.error("Requires at least one config file (option -c/--config)")
if args:
    parser.error("Does not accept any argument. Use option -c/--config")

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