#!/bin/bash
#
# A little helper to overload default datalad executables with the one ran
# through coverage

set -eu

bin=$(basename $0)
curbin=$(which "$bin")
curdatalad=$(which datalad)
curdir=$(dirname $curdatalad)

COVERAGE_RUN="-m coverage run"
export COVERAGE_PROCESS_START=$PWD/../.coveragerc
export PYTHONPATH="$PWD/../tools/coverage-bin/"
export PATH=${PATH//$curdir:/}
newdatalad=$(which datalad)
newbin=$(which $bin)
newpython=$(sed -ne '1s/#!//gp' $newdatalad)

if [ $newdatalad = $curdatalad ]; then
   echo "E: binary remained the same: $newdatalad" >&2
   exit 1
fi

touch /tmp/coverages
export COVERAGE_FILE=/tmp/.coverage-entrypoints-$RANDOM
echo "Running now $newpython $COVERAGE_RUN --include=datalad/* -a $newbin $@" >> /tmp/coverages
#$newpython $COVERAGE_RUN --include=datalad/* -a $newbin "$@"
$newpython $COVERAGE_RUN -a $newbin "$@"
