#!/bin/sh

set -ex

# required for the debian adt host
mkdir -p /etc/systemd/system/snapd.service.d/
if [ "${http_proxy:-}" != "" ]; then
    cat <<EOF | tee /etc/systemd/system/snapd.service.d/proxy.conf
[Service]
Environment=http_proxy=$http_proxy
Environment=https_proxy=$http_proxy
EOF

    # ensure environment is updated
    echo "http_proxy=$http_proxy" >> /etc/environment
    echo "https_proxy=$http_proxy" >> /etc/environment
fi
systemctl daemon-reload

# ensure we can do a connect to localhost
echo ubuntu:ubuntu|chpasswd
sed -i 's/\(PermitRootLogin\|PasswordAuthentication\)\>.*/\1 yes/' /etc/ssh/sshd_config

service ssh restart

# Map snapd deb package pockets to core snap channels. This is intended to cope
# with the autopkgtest execution when testing packages from the different pockets
if apt -qq list snapd | grep -q -- -proposed; then
    export SPREAD_CORE_CHANNEL=candidate
elif apt -qq list snapd | grep -q -- -updates; then
    export SPREAD_CORE_CHANNEL=stable
fi

# and now run spread against localhost
# shellcheck disable=SC1091
. /etc/os-release
apt-get install golang-1.6-go
export GOPATH=/tmp/go
export PATH=/usr/lib/go-1.6/bin:${PATH}
go get -u github.com/snapcore/spread/cmd/spread
/tmp/go/bin/spread -v "autopkgtest:${ID}-${VERSION_ID}-$(dpkg --print-architecture)"
