#!/bin/bash
# Installs a host SSH key onto the VM via ssh-copy-id (password auth, one
# time), so later scripts don't need to handle password prompts at all for
# plain commands (sudo still needs the password unless cached).
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source ./00-config.sh

if [ ! -f ~/.ssh/id_ed25519 ]; then
    ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519 -C "host-to-${VM_NAME}-vm"
fi

expect -c "
set timeout 15
spawn ssh-copy-id -o StrictHostKeyChecking=accept-new ${SSH_USER}@${VM_IP}
expect \"password:\"
send \"${VM_PASSWORD}\r\"
expect eof
"

ssh -o BatchMode=yes -o ConnectTimeout=10 "${SSH_USER}@${VM_IP}" "echo PASSWORDLESS_SSH_OK"
