import click


def read_env(env_file, set_alt):
    """
    Read config vars from file

    :param env_file: output file
    :param set_alt: Flag to check if alternate values must be used.
    :return: dict of read config vars
    """
    # init
    config_dict = {}
    use_alt = False
    alt_value = None

    with open(env_file) as e:

        for line in e:
            line = line.strip()
            if line:
                # check comments
                if line.startswith("#"):
                    # enable --set-alt
                    if set_alt and "alt_value=" in line:
                        alt_value = line.split("alt_value=", 1)[1]
                        if alt_value is not None:
                            # use this value for the next env var
                            use_alt = True
                    # any kind of comment warrants a skip
                    continue

                # set env vars
                if "=" in line:
                    kv_pair = line.split("=", 1)
                    if len(kv_pair) > 1:  # has to at least be of form: k=
                        key = kv_pair[0]

                        if use_alt:
                            if alt_value == '':
                                # skip the value if its an empty string
                                continue
                            elif alt_value == '-':
                                # set to None if alt_value is '-'
                                # this allows removal of a config var.
                                value = None
                            else:
                                value = alt_value

                            # reset
                            use_alt = False
                        else:
                            value = kv_pair[1]

                        # finally, set to config dict
                        # an empty value is fine but obviously not an empty key.
                        if key:
                            config_dict[key] = value
                            # confirm
                            click.secho(u'\u2713 ' + key, fg='green', bold=True)

                else:
                    click.echo("Skipping line : Not of the form key=value.")

    return config_dict