#!/bin/bash
set -e

cd $(dirname $0)/..

trap "rm -rf /scratch/*" exit

GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ -n "${DRONE_BRANCH}" ] && [ "${DRONE_BRANCH}" != "${GIT_BRANCH}" ]; then
    git checkout -b ${DRONE_BRANCH}
    GIT_BRANCH=${DRONE_BRANCH}
fi

if [ "$GIT_BRANCH" != "master" ]; then
  sed -i "s#master#$GIT_BRANCH#" ./scripts/repo.json
fi

if [ -d "/scratch" ]; then
    rsync -a --exclude .tox ./ /scratch
fi

pushd /scratch/integration

# Start rancher-catalog-service and wait for it to start.
/usr/bin/rancher-catalog-service -configFile ../scripts/repo.json -refreshInterval 7200 > /dev/null 2>&1 &
sleep 10
tox -e flake8,py27
popd