command -v ssh-agent >/dev/null 2>&1 || return
[ -d ~/.ssh/keys ] || return
[ "$(uname -s)" = Linux ] || return

# We manually create both, so no need to worry about -m only applying to the
# deepest
# shellcheck disable=SC2174
mkdir -m700 -p ~/.ssh ~/.ssh/agents

# Execute in subshell to avoid polluting environment
(
for key in ~/.ssh/keys/*; do
    [ -f "$key" ] || continue

    case $key in
        *.pub) continue ;;
    esac

    agent_file=$HOME/.ssh/agents/${key##*/}

    [ -r "$agent_file" ] && . "$agent_file" >/dev/null

    if [ "$(id -u)" -gt 0 ] && ! ssh-add -l >/dev/null 2>&1; then
        eval "$(ssh-agent | tee "$agent_file")" >/dev/null
        ssh-add "$key"
    fi
done
)

[ -e ~/.ssh/agents/personal ] && . ~/.ssh/agents/personal >/dev/null

stable_auth_dir=/tmp
[ -d "$XDG_RUNTIME_DIR" ] && stable_auth_dir="$XDG_RUNTIME_DIR"
stable_auth="$stable_auth_dir/ssh-auth-sock-$(id -u)"
if [ "$SSH_AUTH_SOCK" ] && [ "$SSH_AUTH_SOCK" != "$stable_auth" ]; then
    ln -sfn -- "$SSH_AUTH_SOCK" "$stable_auth"
    SSH_AUTH_SOCK=$stable_auth
fi
