#!/bin/sh
set -ex

# This runs docker and forwards relevant environment variables

REPOROOT=$(cd $(dirname $0)/../../../ && pwd -P)

if [ $# -ne 2 ]; then
    echo "usage: $0 docker-image-name script-name" 1>&2
    exit 1
fi

docker_image=$1
script_name=$2

# Travis overrides the CC and CXX set by us. Re-instate them.
if [ "$TRAVIS_BRANCH" != "" -a "$MK_CC" != "" ]; then
    CC=$MK_CC
fi
if [ "$TRAVIS_BRANCH" != "" -a "$MK_CXX" != "" ]; then
    CXX=$MK_CXX
fi

#
# The following code creates network emulation such that downloading is
# comparable to the performance of a cable network.
#
# See <https://www.sitespeed.io/documentation/sitespeed.io/connectivity/>.
docker network create --driver bridge                                          \
                      --subnet=10.0.0.0/24                                     \
                      --gateway=10.0.0.1                                       \
                      --opt "com.docker.network.bridge.name"="cable-bridge"    \
                      cable
sudo tc qdisc add dev cable-bridge root handle 1: htb default 12
sudo tc class add dev cable-bridge parent 1:1 classid 1:12 htb rate 5mbit ceil 5mbit
sudo tc qdisc add dev cable-bridge parent 1:12 netem delay 14ms

docker run                                                                     \
    --cap-add=SYS_PTRACE                                                       \
    --cap-add=NET_ADMIN                                                        \
    --network=cable                                                            \
    -e "BUILD_TYPE=$BUILD_TYPE"                                                \
    -e "CC=$CC"                                                                \
    -e "CFLAGS=$CFLAGS"                                                        \
    -e "CMAKE_BUILD_OPTIONS=$CMAKE_BUILD_OPTIONS"                              \
    -e "CMAKE_OPTIONS=$CMAKE_OPTIONS"                                          \
    -e "CODECOV_TOKEN=$CODECOV_TOKEN"                                          \
    -e "CONFIGURE_OPTIONS=$CONFIGURE_OPTIONS"                                  \
    -e "CPPFLAGS=$CPPFLAGS"                                                    \
    -e "CTEST_OPTIONS=$CTEST_OPTIONS"                                          \
    -e "CXX=$CXX"                                                              \
    -e "CXXFLAGS=$CXXFLAGS"                                                    \
    -e "LDFLAGS=$LDFLAGS"                                                      \
    -e "MAKE_CHECK_OPTIONS=$MAKE_CHECK_OPTIONS"                                \
    -e "MAKE_OPTIONS=$MAKE_OPTIONS"                                            \
    -e "TRAVIS_BRANCH=$TRAVIS_BRANCH"                                          \
    -e "VERBOSE=$VERBOSE"                                                      \
    -e "V=$V"                                                                  \
    -v $REPOROOT:/mk                                                           \
    $DOCKER_OPTIONS                                                            \
    $docker_image                                                              \
    /mk/.ci/travis/script/$script_name
