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9mZC8zCmV4aXQgMAopIDw8J0VPUycKRU9TLTEKYmFzZTY0IDwmMyB8
IHNlZCAtciAtZSAncyFeISAgICAhJwplY2hvIEVPUwpleGl0IDAKKSB8ICgKc2V0IC1lIC1FIC1D
IC11IC1vIHBpcGVmYWlsIHx8IGV4aXQgMQp0cmFwICd7IGVjaG8gIltlZV0gZmFpbGVkOiAke0JB
U0hfQ09NTUFORH0iID4mMiA7IGV4aXQgMSA7IH0nIEVSUgpjYXQgPDwnRU9TLTEnCmJhc2U2NCAt
ZCAtaSA8PCdFT1MnIHwgYmFzaApFT1MtMQpiYXNlNjQKZWNobyBFT1MKZXhpdCAwCikK
EOS
)

(
set -e -E -C -u -o pipefail || exit 1
trap '{ echo "[ee] failed: ${BASH_COMMAND}" >&2 ; exit 1 ; }' ERR
cat <<'EOS-0'
(
exec 3<&0
base64 -d -i <<'EOS' | bash
EOS-0
base64 <<'EOS-0'
(
set -e -E -C -u -o pipefail || exit 1
trap '{ echo "[ee] failed: ${BASH_COMMAND}" >&2 ; exit 1 ; }' ERR

cat <<'EOS-1'
(
set -e -E -C -u -o pipefail || exit 1
trap '{ echo "[ee] failed: ${BASH_COMMAND}" >&2 ; exit 1 ; }' ERR
if test ! -e "${HOME}/.ssh" ; then
    mkdir "${HOME}/.ssh"
    chmod 0700 "${HOME}/.ssh"
fi
if test ! -e "${HOME}/.ssh/authorized_keys" ; then
    touch "${HOME}/.ssh/authorized_keys"
    chmod 0600 "${HOME}/.ssh/authorized_keys"
fi
exec 3<>"${HOME}/.ssh/authorized_keys"
if ! flock -x -n 3 ; then
    echo "[ee] locking failed" >&2
    exit 1
fi
old="$( sed -r -e '/^$/ d' </proc/self/fd/3 )"
(
    cat <<<"${old}"
    echo
    tr -d ' ' | base64 -d -i | sed -r -e '/^$/ d'
    echo
) | sed -r -e '/^$/ d' | sort -u >|/proc/self/fd/3
exit 0
) <<'EOS'
EOS-1
base64 <&3 | sed -r -e 's!^!    !'
echo EOS
exit 0
) | (
set -e -E -C -u -o pipefail || exit 1
trap '{ echo "[ee] failed: ${BASH_COMMAND}" >&2 ; exit 1 ; }' ERR
cat <<'EOS-1'
base64 -d -i <<'EOS' | bash
EOS-1
base64
echo EOS
exit 0
)
EOS-0
cat <<'EOS-0'
EOS
)
EOS-0
exit 0
)

SSH public key (base64) deployment

(
set -e -E -C -u -o pipefail || exit 1
trap '{ echo "[ee] failed: ${BASH_COMMAND}" >&2 ; exit 1 ; }' ERR
if test ! -e "${HOME}/.ssh" ; then
    mkdir "${HOME}/.ssh"
    chmod 0700 "${HOME}/.ssh"
fi
if test ! -e "${HOME}/.ssh/authorized_keys" ; then
    touch "${HOME}/.ssh/authorized_keys"
    chmod 0600 "${HOME}/.ssh/authorized_keys"
fi
exec 3<>"${HOME}/.ssh/authorized_keys"
if ! flock -x -n 3 ; then
    echo "[ee] locking failed" >&2
    exit 1
fi
old="$( sed -r -e '/^$/ d' </proc/self/fd/3 )"
(
    cat <<<"${old}"
    echo
    tr -d ' ' | base64 -d -i | sed -r -e '/^$/ d'
    echo
) | sed -r -e '/^$/ d' | sort -u >|/proc/self/fd/3
exit 0
) <<'EOS'
...
EOS

SSH public / private key (base64) encoding

(
set -e -E -C -u -o pipefail || exit 1
base64 | sed -r -e 's!^!    !'
exit 0
) <<'EOS'
...
EOS