#!python
#-*- coding: utf-8 -*-
#
# This file belongs to coshsh.
# Copyright Gerhard Lausser.
# This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

import sys
sys.dont_write_bytecode = True
import os
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
try:
    import importlib
    importlib.reload(sys)
except Exception:
    import imp
    imp.reload(sys)
from optparse import OptionParser

try:
    import coshsh
except Exception:
    if 'COSHSH_HOME' in os.environ:
        coshsh_home = os.environ['COSHSH_HOME']
    else:
        coshsh_home = os.path.join(os.path.dirname(__file__), '..')
        os.environ['COSHSH_HOME'] = coshsh_home
    sys.path.append(coshsh_home)
    try:
        import coshsh
    except Exception:
        print("Could not load package coshsh. Check your PYTHONPATH")
        sys.exit(3)
from coshsh.generator import Generator



if __name__ == '__main__':
    VERSION = "1.0"

    parser = OptionParser(
        "%prog [options] --cookbook cookbookfile [--recipe recipe]",
        version="%prog " + VERSION)
    parser.add_option('--cookbook', action='append',
                      dest="cookbook_files",
                      help='One or more Config files')
    parser.add_option('--recipe', action='store',
                      dest="default_recipe",
                      help="Cook a configuration following <recipe>")
    parser.add_option('--debug', action='store_const',
                      const="debug",
                      default="info",
                      dest="default_log_level",
                      help="Output additional messages on stdout")
    parser.add_option('--force', action='store_true',
                      default=False,
                      dest="force",
                      help="Force datasource to be read")
    parser.add_option('--safe-output', action='store_true',
                      default=False,
                      dest="safe_output",
                      help="Do not commit when hosts disappeared")

    opts, args = parser.parse_args()
    generator = Generator()
    generator.set_default_log_level(opts.default_log_level)
    if opts.cookbook_files:
        generator.read_cookbook(opts.cookbook_files, opts.default_recipe, opts.force, opts.safe_output)
    else:
        parser.error("Use option -c/--cookbook")
    if args:
        parser.error("Does not accept any argument. Use option -c/--cookbook")

    generator.run()

