#! /bin/bash -e

export GAIATEST_ACKNOWLEDGED_RISKS=true
export GAIATEST_SKIP_WARNING=true

# Get the mulet binary...
get_mulet_bin() {
  local folder=$(find . -d 1 -name 'mulet' | cut -f 1 -d " ")
  # Download mulet ...
  if [ -z "$folder" ];
  then
    # Everything goes to stderr...
    make mulet 1>&2
  fi
  find $folder -follow -name "firefox-bin" | tail -n 1
}


# Get the gaia profile...
get_profile() {
  # TODO: Make configurable
  local folder=profile
  if [ ! -d $folder ];
  then
    # Everything goes to stderr...
    make $folder 1>&2
  fi
  echo $folder
}

mulet=$(get_mulet_bin)
root=tests/python/gaia-ui-tests/gaiatest
profile=$(get_profile)
gaiatest="$root/cli.py"

echo "Using mulet: $mulet"

# Enter the virtual env.
source ./tests/ci/venv.sh &&
# CD into the directory to install python libs.
cd $root/../ && \
# Ensure all dependencies are set.
python setup.py develop && \
# Back to root.
cd $OLDPWD && \
# Run the tests.
python $gaiatest --app=b2gdesktop \
          --binary=$mulet \
          --profile=$profile \
          --type=b2g \
          --timeout=10000 \
          --restart \
          $@
