#!/bin/sh
set -ex

# HOME may not be set in an autopkgtest environment, but we want to be on the
# same page as ssh on where ~/.ssh is. See LP: #2012514.
if [ "$HOME" = "" ]; then
	export HOME=$(getent passwd `whoami`|cut -d: -f6)
fi

echo 'mosh motd test' > /etc/motd
mkdir -pm700 "$HOME"/.ssh
if [ ! -f "$HOME"/.ssh/id_rsa ]; then
	ssh-keygen -N '' -C mosh-smoke -f "$HOME"/.ssh/id_rsa
fi
if [ ! -f "$HOME"/.ssh/authorized_keys ] || ! grep -q mosh-smoke\$ "$HOME"/.ssh/authorized_keys; then
	cat "$HOME"/.ssh/id_rsa.pub >> "$HOME"/.ssh/authorized_keys
fi
python3 <<EOT
import os
import pexpect
# Set TERM=dumb so the bash prompt doesn't embed escape sequences to make it
# easier to match
env = os.environ.copy()
env["TERM"] = "dumb"
child = pexpect.spawn('mosh --ssh="ssh -o StrictHostKeyChecking=no" localhost', env=env)
child.expect('mosh motd test')
child.expect(r'(\w+)@([-\w]+):~#')
EOT
