"""
Global configuration of the project.

All configurable parameters are stored in the global variable CONFIG,
the only variable which is exported from the module.

Default values of all configuration parameters are specified
in the `_CONFIG` dictionary. Those parameters can be overridden
by three means:
    * config file `etc/config.yaml` located in the work dir
    * config file `etc/config.yaml` located in the project dir
      (if the work dir and the project dir are not the same)
    * environment variables prefixed with `CHEATSH_`

Configuration placement priorities, from high to low:
    * environment variables;
    * configuration file in the workdir
    * configuration file in the project dir
    * default values specified in the `_CONFIG` dictionary

If the work dir and the project dir are not the same, we do not
recommend that you use the config file located in the project dir,
except the cases when you use your own cheat.sh fork, and thus
configuration is a part of the project repository.
In all other cases `WORKDIR/etc/config.yaml` should be preferred.
Location of this config file can be overridden by the `CHEATSH_PATH_CONFIG`
environment variable.

Configuration parameters set by environment variables are mapped
in this way:
    * CHEATSH_ prefix is trimmed
    * _ replaced with .
    * the string is lowercased

For instance, an environment variable named `CHEATSH_SERVER_PORT`
specifies the value for the `server.port` configuration parameter.

Only parameters that imply scalar values (integer or string)
can be set using environment variables, for the rest config files
should be used. If a parameter implies an integer, and the value
specified by an environment variable is not an integer, it is ignored.
"""

from __future__ import print_function
import os

from pygments.styles import get_all_styles
#def get_all_styles():
#    return []

_ENV_VAR_PREFIX = "CHEATSH"

_MYDIR = os.path.abspath(os.path.join(__file__, '..', '..'))

def _config_locations():
    """
    Return three possible config locations
    where configuration can be found:
    * `_WORKDIR`, `_CONF_FILE_WORKDIR`, `_CONF_FILE_MYDIR`
    """

    var = _ENV_VAR_PREFIX + '_PATH_WORKDIR'
    workdir = os.environ[var] if var in os.environ \
        else os.path.join(os.environ['HOME'], '.cheat.sh')

    var = _ENV_VAR_PREFIX + '_CONFIG'
    conf_file_workdir = os.environ[var] if var in os.environ \
            else os.path.join(workdir, 'etc/config.yaml')

    conf_file_mydir = os.path.join(_MYDIR, 'etc/config.yaml')
    return workdir, conf_file_workdir, conf_file_mydir

_WORKDIR, _CONF_FILE_WORKDIR, _CONF_FILE_MYDIR = _config_locations()

_CONFIG = {
    "adapters.active": [
        "tldr",
        "cheat",
        "fosdem",
        "translation",
        "rosetta",