function _hs () {
    local cur prev
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    local settings_file
    settings_file=~/'.heidisql/portable_settings.txt'
    if [[ ! -r "${settings_file}" ]]; then
        COMPREPLY="$(printf -- '%s' '<File '"${settings_file}"' not readable, nothing to auto-complete>')"
        return 1
    fi

    local saved_sessions
    # Find all saved sessions where NetType is set to '2' aka 'MariaDB or
    # MySQL (SSH tunnel)'
    saved_sessions="$(grep -Pio -- '(?<=^Servers\\)[^\\]+(?=\\NetType<\|\|\|>[[:digit:]]<\|\|\|>2)' "${settings_file}" | sort | uniq)"

    # Commands below depend on this IFS
    local IFS=$'\n'

    if [[ "${prev}" != 'hs' ]]; then
        return 0
    fi

    if [[ "${cur}" == * ]]; then
        # Filter our candidates
        CANDIDATES=($(compgen -W "${saved_sessions[*]}" -- "${cur}"))

        # Correctly set our candidates to COMPREPLY
        if [[ "${#CANDIDATES[*]}" -eq '0' ]]; then
            COMPREPLY=()
        else
            COMPREPLY=($(printf ''"'"'%s'"'"'\n' "${CANDIDATES[@]}"))
        fi
        return 0
    fi
}

complete -F _hs hs
