# Path: applications/configuration.py
# def generate_server_conf(installed=True):
#     """
#     Generates a fresh settings/10server.conf file using the arguments provided
#     on the command line to override defaults.
# 
#     If *installed* is ``False`` the defaults will be set under the assumption
#     that the user is non-root and running Gate One out of a download/cloned
#     directory.
#     """
#     logger.info(_(
#         u"Gate One settings are incomplete.  A new <settings_dir>/10server.conf"
#         u" will be generated."))
#     auth_settings = {} # Auth stuff goes in 20authentication.conf
#     options_config = define_options()
#     all_setttings = options_to_settings(options_config) # NOTE: options is global
#     settings_path = options_config['settings_dir']
#     server_conf_path = os.path.join(settings_path, '10server.conf')
#     if os.path.exists(server_conf_path):
#         logger.error(_(
#             "You have a 10server.conf but it is either invalid (syntax "
#             "error) or missing essential settings."))
#         #sys.exit(1)
#         return
#     config_defaults = all_setttings['*']['gateone']
#     # Don't need this in the actual settings file:
#     del config_defaults['settings_dir']
#     non_options = [
#         # These are things that don't really belong in settings
#         'new_api_key', 'help', 'kill', 'config', 'version', 'combine_css',
#         'combine_js', 'combine_css_container', 'configure'
#     ]
#     # Don't need non-options in there either:
#     for non_option in non_options:
#         if non_option in config_defaults:
#             del config_defaults[non_option]
#     # Generate a new cookie_secret
#     config_defaults['cookie_secret'] = generate_session_id()
#     # Separate out the authentication settings
#     authentication_options = [
#         # These are here only for logical separation in the .conf files
#         'api_timestamp_window', 'auth', 'pam_realm', 'pam_service',
#         'sso_keytab', 'sso_realm', 'sso_service', 'ssl_auth'
#     ]
#     # Provide some kerberos (sso) defaults
#     auth_settings['sso_realm'] = "EXAMPLE.COM"
#     auth_settings['sso_keytab'] = None # Allow /etc/krb5.conf to control it
#     for key, value in list(config_defaults.items()):
#         if key in authentication_options:
#             auth_settings.update({key: value})
#             del config_defaults[key]
#         if key == 'origins':
#             # As a convenience to the user, add any --port to the origins
#             if config_defaults['port'] not in [80, 443]:
#                 for i, origin in enumerate(list(value)):
#                     value[i] = "{origin}:{port}".format(
#                         origin=origin, port=config_defaults['port'])
#     # Make sure we have a valid log_file_prefix
#     if config_defaults['log_file_prefix'] == None:
#         web_log_dir = os.path.join(os.path.sep, "var", "log", "gateone")
#         if installed:
#             here = os.path.dirname(os.path.abspath(__file__))
#             web_log_dir = os.path.normpath(
#                 os.path.join(here, '..', '..', 'logs'))
#         web_log_path = os.path.join(web_log_dir, 'gateone.log')
#         config_defaults['log_file_prefix'] = web_log_path
#     else:
#         web_log_dir = os.path.split(config_defaults['log_file_prefix'])[0]
#     if not os.path.exists(web_log_dir):
#         # Make sure the directory exists
#         mkdir_p(web_log_dir)
#     if not os.path.exists(config_defaults['log_file_prefix']):
#         # Make sure the file is present
#         io.open(
#             config_defaults['log_file_prefix'],
#             mode='w', encoding='utf-8').write(u'')
#     auth_conf_path = os.path.join(settings_path, '20authentication.conf')
#     template_path = os.path.join(getsettings('BASE_DIR'),'templates/settings/generic.conf')
#     new_settings = settings_template(
#         template_path, settings=config_defaults)
#     with io.open(server_conf_path, mode='w') as s:
#         s.write(u"// This is Gate One's main settings file.\n")
#         s.write(new_settings)
#     #print auth_settings
#     new_auth_settings = settings_template(
#         template_path, settings=auth_settings)
#     with io.open(auth_conf_path, mode='w') as s:
#         s.write(u"// This is Gate One's authentication settings file.\n")
#         s.write(new_auth_settings)


import os
from applications.configuration import generate_server_conf

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

# Channels settings
CHANNEL_LAYERS = {
    "default": {
       "BACKEND": "asgi_redis.RedisChannelLayer",  # use redis backend
       "CONFIG": {
           "hosts": [("localhost", 6379)],  # set redis address
           },
       "ROUTING": "django_gateone.routing.channel_routing",  # load routing from our routing.py file
       },
}
settings_dir = os.path.join(BASE_DIR,'conf.d')


# Path: applications/configuration.py
# def generate_server_conf(installed=True):
#     """
#     Generates a fresh settings/10server.conf file using the arguments provided
#     on the command line to override defaults.
# 
#     If *installed* is ``False`` the defaults will be set under the assumption
#     that the user is non-root and running Gate One out of a download/cloned
#     directory.
#     """
#     logger.info(_(
#         u"Gate One settings are incomplete.  A new <settings_dir>/10server.conf"
#         u" will be generated."))
#     auth_settings = {} # Auth stuff goes in 20authentication.conf
#     options_config = define_options()
#     all_setttings = options_to_settings(options_config) # NOTE: options is global
#     settings_path = options_config['settings_dir']
#     server_conf_path = os.path.join(settings_path, '10server.conf')
#     if os.path.exists(server_conf_path):
#         logger.error(_(
#             "You have a 10server.conf but it is either invalid (syntax "
#             "error) or missing essential settings."))
#         #sys.exit(1)
#         return
#     config_defaults = all_setttings['*']['gateone']
#     # Don't need this in the actual settings file:
#     del config_defaults['settings_dir']
#     non_options = [
#         # These are things that don't really belong in settings
#         'new_api_key', 'help', 'kill', 'config', 'version', 'combine_css',
#         'combine_js', 'combine_css_container', 'configure'
#     ]
#     # Don't need non-options in there either:
#     for non_option in non_options:
#         if non_option in config_defaults:
#             del config_defaults[non_option]
#     # Generate a new cookie_secret
#     config_defaults['cookie_secret'] = generate_session_id()
#     # Separate out the authentication settings
#     authentication_options = [
#         # These are here only for logical separation in the .conf files
#         'api_timestamp_window', 'auth', 'pam_realm', 'pam_service',
#         'sso_keytab', 'sso_realm', 'sso_service', 'ssl_auth'
#     ]
#     # Provide some kerberos (sso) defaults
#     auth_settings['sso_realm'] = "EXAMPLE.COM"
#     auth_settings['sso_keytab'] = None # Allow /etc/krb5.conf to control it
#     for key, value in list(config_defaults.items()):
#         if key in authentication_options:
#             auth_settings.update({key: value})
#             del config_defaults[key]
#         if key == 'origins':
#             # As a convenience to the user, add any --port to the origins
#             if config_defaults['port'] not in [80, 443]:
#                 for i, origin in enumerate(list(value)):
#                     value[i] = "{origin}:{port}".format(
#                         origin=origin, port=config_defaults['port'])
#     # Make sure we have a valid log_file_prefix
#     if config_defaults['log_file_prefix'] == None:
#         web_log_dir = os.path.join(os.path.sep, "var", "log", "gateone")
#         if installed:
#             here = os.path.dirname(os.path.abspath(__file__))
#             web_log_dir = os.path.normpath(
#                 os.path.join(here, '..', '..', 'logs'))
#         web_log_path = os.path.join(web_log_dir, 'gateone.log')
#         config_defaults['log_file_prefix'] = web_log_path
#     else:
#         web_log_dir = os.path.split(config_defaults['log_file_prefix'])[0]
#     if not os.path.exists(web_log_dir):
#         # Make sure the directory exists
#         mkdir_p(web_log_dir)
#     if not os.path.exists(config_defaults['log_file_prefix']):
#         # Make sure the file is present
#         io.open(
#             config_defaults['log_file_prefix'],
#             mode='w', encoding='utf-8').write(u'')
#     auth_conf_path = os.path.join(settings_path, '20authentication.conf')
#     template_path = os.path.join(getsettings('BASE_DIR'),'templates/settings/generic.conf')
#     new_settings = settings_template(
#         template_path, settings=config_defaults)
#     with io.open(server_conf_path, mode='w') as s:
#         s.write(u"// This is Gate One's main settings file.\n")
#         s.write(new_settings)
#     #print auth_settings
#     new_auth_settings = settings_template(
#         template_path, settings=auth_settings)
#     with io.open(auth_conf_path, mode='w') as s:
#         s.write(u"// This is Gate One's authentication settings file.\n")
#         s.write(new_auth_settings)


# Path: django_gateone/settings.py
import os
from applications.configuration import generate_server_conf


TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

# Channels settings
CHANNEL_LAYERS = {
    "default": {
       "BACKEND": "asgi_redis.RedisChannelLayer",  # use redis backend
       "CONFIG": {
           "hosts": [("localhost", 6379)],  # set redis address
           },
       "ROUTING": "django_gateone.routing.channel_routing",  # load routing from our routing.py file
       },
}
settings_dir = os.path.join(BASE_DIR,'conf.d')


generate_server_conf(installed=False)