#! /bin/bash -e

source ./bin/buildbot_step

Xvfb :0 -nolisten tcp -screen 0 1600x1200x24 2>/dev/null &
export DISPLAY=:0

checkout_path="$PWD/git_checkout"

# Handle initial clone and caching of the initial clone...
step_checkout() {
  # Extract the cached clone (this is kept as a tarball to always keep a clean
  # working state without worrying about tree issues).
  if [ "$GITHUB_PULL_REQUEST" == "1" ];
  then
    tc-vcs checkout $checkout_path \
      $GITHUB_BASE_GIT \
      $GITHUB_HEAD_GIT \
      $GITHUB_HEAD_BRANCH
  else
    tc-vcs checkout $checkout_path \
      $GITHUB_BASE_GIT \
      $GITHUB_HEAD_GIT
  fi
}

# Set-up our npm-cache.
step_npm_cache() {
  taskcluster-npm-cache-get --namespace gaia.npm_cache ./package.json
}

buildbot_step 'git checkout' step_checkout

# From here on we to operate inside of the actual checkout mostly for
# convenience.
cd $checkout_path

# Current revision...
buildbot_step 'git revision...' git --no-pager log -n 1

# node modules
buildbot_step 'npm-cache-get' step_npm_cache

# virtualenv setup so any python packages can be abused without root access
buildbot_step 'virtualenv' virtualenv $HOME/.venv --system-site-packages
source $HOME/.venv/bin/activate

# run whatever command was intended on the branch (root of the repo)
buildbot_step "run $*" $@
