#!/bin/bash
set -euox pipefail

# Ensure asv now machine characteristics
asv machine --yes

echo check that system is ready for benchmark
echo "(use ASV_SKIP_SYSTEM_CHECK=1 to skip it)"

if [ -z "${ASV_SKIP_SYSTEM_CHECK:-}" ]; then
    # Check that the machine is ready for benchmark
    python -m perf system show
fi

# Ensure all repository are here

if [ ! -d mercurial ]; then
    hg clone https://www.mercurial-scm.org/repo/hg mercurial
fi

mkdir -p repos/
mkdir -p repos/.cache
make MAKE_VERBOSE=1 -f repos.make

./repo-scripts/rename-reference-directories repos

python create_skip_file.py

target="${1-1.9:: and tagged()}"
if [ "$#" -ge 1 ]; then
    shift
fi

# Launch asv
time HGRCPATH= taskset -c 2,3 asv run --verbose --show-stderr --skip-existing-successful "${target}" "$@"

# Publish results
asv publish --no-pull

echo "DONE"