#!/bin/sh

data_dir=$PWD/QBS-1359
build_dir=/tmp/QBS-1359

function findProblem
{
    while :
    do
        index=$(shuf -i 1-$# -n 1)
        eval commit="\$\{${index}\}"
        eval commit="$commit"
        git checkout $commit || exit 1
        echo $commit >> "$data_dir/commits.txt"
        qbs -vd "$build_dir" > "$data_dir/out.$commit.1" 2> "$data_dir/err.$commit.1" || { echo $commit >> "$data_dir/broken.txt"; continue; }
        qbs -vd "$build_dir" > "$data_dir/out.$commit.2" 2> "$data_dir/err.$commit.2"
        grep compiling "$data_dir/out.$commit.2" && { echo 'Bingo!'; exit 0; }
        grep "rcc " "$data_dir/out.$commit.2" && { echo 'Bingo!'; exit 0; }
    done
}

test $# -eq 2 || { echo "Need two arguments describing the commit range." > /dev/stderr; exit 1; }
rm -rf "$data_dir" "$build_dir"
mkdir "$data_dir"
all_commits=$(git log $1..$2 --format=format:%h)
findProblem $all_commits
