#!/bin/sh

NEEDS_REBOOT=/tmp/needs-reboot

set -ex

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

# ensure our PATH is right
. /etc/profile.d/apps-bin-path.sh

tmp="${ADT_ARTIFACTS}/build"
mkdir -p $tmp
export GOPATH=$tmp
mkdir -p $GOPATH/src/github.com/ubuntu-core/snappy
cp -R ./* $GOPATH/src/github.com/ubuntu-core/snappy/
mkdir -p $GOPATH/src/github.com/ubuntu-core/snappy/integration-tests/data/output
cp debian/tests/testconfig.json $GOPATH/src/github.com/ubuntu-core/snappy/integration-tests/data/output/
cd $GOPATH/src/github.com/ubuntu-core/snappy

# don't install deps nor compile tests after reboot
if [ -z "$ADT_REBOOT_MARK" ]; then
    ./get-deps.sh
    go test -tags classiconly -c ./integration-tests/tests
fi

./tests.test  -check.vv -check.f 'snapHelloWorldExampleSuite|snapOpSuite|searchSuite|installAppSuite|authSuite|networkInterfaceSuite|networkBindInterfaceSuite|homeInterfaceSuite|unity7|snapPersistsSuite'

if [ -e ${NEEDS_REBOOT} ]; then
    mark=`cat ${NEEDS_REBOOT}`
    echo "Rebooting..."
    sudo /tmp/autopkgtest-reboot "$mark"
fi
