function install_env_path(
    m::Module,
    options::Configs.Comonicon = Configs.read_options(m);
    shell::String = basename(ENV["SHELL"]),
    home_dir = homedir(),
    env = ENV,
    yes::Bool = false,
)

    rcfile = detect_rcfile(shell, home_dir)
    install_path = expanduser(options.install.path)
    msg = "cannot detect $(options.install.path)/bin in PATH, do you want to add it in PATH?"
    if !contains_path(rcfile, install_path, env) && Tools.prompt(msg, yes)
        write_path(rcfile, install_path)
    end

    msg = "cannot detect $(options.install.path)/completions in FPATH, do you want to add it in FPATH?"
    if !contains_fpath(rcfile, install_path, env) && Tools.prompt(msg, yes)
        write_fpath(rcfile, install_path)
    end

    @info "open a new terminal, or source $rcfile to enable the new PATH."
    return
end

function contains_path(rcfile::String, path::String, env = ENV)
    bin = joinpath(path, "bin")
    return contains_shell_var(rcfile, "PATH", bin, env)
end

function contains_fpath(rcfile::String, path::String, env = ENV)
    completions = joinpath(path, "completions")
    return contains_shell_var(rcfile, "FPATH", completions, env)
end

function write_path(rcfile::String, path::String)
    bin = joinpath(path, "bin")
    return export_shell_var(rcfile, "PATH", bin)
end

function write_fpath(rcfile::String, path::String)
    completions = joinpath(path, "completions")
    return export_shell_var(rcfile, "FPATH", completions)
end

function contains_shell_var(rcfile, var, value, env)
    if !haskey(env, var)
        for line in readlines(rcfile)
            if strip(line) == "export $var=\"$(relhome(value)):\$$var\"" ||
               strip(line) == "export $var=\"$value:\$$var\""
                return true
            end
        end
        return false
    end

    for each in split(env[var], ":")
        (each == relhome(value) || each == value) && return true
    end
    return false
end

function export_shell_var(rcfile, var, path)
    isempty(rcfile) && return
    script = """
    # generated by Comonicon
    export $var="$(relhome(path)):\$$var"
    """
    open(rcfile, "a") do io
        write(io, "\n" * script * "\n")
    end
end

relhome(path) = joinpath("\$HOME", relpath(path, homedir()))

function detect_rcfile(shell::String = basename(ENV["SHELL"]), home = homedir())
    if shell == "zsh"
        rcfile = joinpath((haskey(ENV, "ZDOTDIR") ? ENV["ZDOTDIR"] : home), ".zshrc")
    elseif shell == "bash"
        rcfile = joinpath(home, ".bashrc")
        if !isfile(rcfile)
            rcfile = joinpath(home, ".bash_profile")
        end
        return rcfile
    else
        error(
            "auto installation for $shell is not supported, " *
            "please open an issue under $COMONICON_URL",
        )
    end
end
