#!/bin/bash

function modify_services_for_testing() {
    # Remove all published ports for testing
    sed -e '/9000:9000/d' -e '/8080:8080/d' -e '/443:443/d' -e '/ports:/d' integration/docker-compose.demo.yml > integration/docker-compose.testing.yml
    # disable download speed limits
    sed -e 's/DOWNLOAD_SPEED/#DOWNLOAD_SPEED/' -i integration/docker-compose.testing.yml
    # whitelist *all* IPs/DNS names in the gateway (will be accessed via dynamically assigned IP in tests)
    sed -e 's/ALLOWED_HOSTS: .*/ALLOWED_HOSTS: ~./' -i integration/docker-compose.testing.yml
}

DIR=$1

if [[ -z "$DIR" ]]; then
    echo "tests dir must be specified"
    exit 1
fi

git clone -b master https://github.com/mendersoftware/integration.git

modify_services_for_testing

mv integration $PWD/tests

go build -o $PWD/tests/mender-cli;

curl --fail "https://d1b0l86ne08fsf.cloudfront.net/mender-artifact/master/mender-artifact" \
    -o $DIR/mender-artifact

if [[ $? -ne 0 ]]; then
    echo "failed to download mender-artifact"
    exit 1
fi

chmod +x $DIR/mender-artifact

docker build -t testing $DIR
