#!/bin/bash

TIDB_TEST_STORE_NAME=$TIDB_TEST_STORE_NAME
TIKV_PATH=$TIKV_PATH

build=1
randgen_test="./randgen_test"
tidb_server=""
randgen_test_log="./tidb.log"
tests=""
record=0
record_case=""

set -eu

function help_message()
{
    echo "Usage: $0 [options]

    -h: Print this help message.

    -s <tidb-server-path>: Use tidb-server in <tidb-server-path> for testing.

    -b <y|Y|n|N>: \"y\" or \"Y\" for building test binaries [default \"y\" if this option is not specified].
                  \"n\" or \"N\" for not to build.
                  The building of tidb-server will be skiped if \"-s <tidb-server-path>\" is provided.

    -r <test-name>: Run tests in file \"t/<test-name>.test\" and record result to file \"r/<test-name>.result\".
                    \"all\" for running all tests and record their results.

    -t <test-name>: Run tests in file \"t/<test-name>.test\".
                    This option will be ignored if \"-r <test-name>\" is provided.

    -v <vendor-path>: Add <vendor-path> to \$GOPATH.
"
}

function build_tidb_server()
{
    tidb_server="./tidb-server-randgen-test"
    echo "BUILDING tidb-server: $tidb_server"
    rm -rf $tidb_server
    curr_path=$(pwd)
    cd ${GOPATH}/src/github.com/pingcap/tidb/tidb-server && GO111MODULE=on go build -race -o $curr_path/$tidb_server
    cd ${curr_path}
}

function build_randgen_test()
{
    echo "BUILDING randgen-test: $randgen_test"
    rm -rf $randgen_test
    GO111MODULE=on go build -o $randgen_test
}

while getopts "t:s:r:b:v:h" opt; do
    case $opt in
        t)
            tests="$OPTARG"
            ;;
        s)
            tidb_server="$OPTARG"
            ;;
        r)
            record=1
            record_case="$OPTARG"
            ;;
        b)
            case $OPTARG in
                y|Y)
                    build=1
                    ;;
                n|N)
                    build=0
                    ;;
                *)
                    help_messge 1>&2
                    exit 1
                    ;;
            esac
            ;;
        v)
            GOPATH="$OPTARG:$GOPATH"
            echo "GOPATH: $GOPATH"
            ;;
        h)
            help_message
            exit 0
            ;;
        *)
            help_message 1>&2
            exit 1
            ;;
    esac
done

if [ $build -eq 1 ]; then
    if [ -z "$tidb_server" ]; then
        build_tidb_server
    else
        echo "SKIP BUILDING tidb-server, USING EXISTING BINARY: $tidb_server"
    fi
    echo "GOPATH: $GOPATH"
    build_randgen_test
else
    if [ -z "$tidb_server" ]; then
        tidb_server="./tidb-server-randgen-test"
    fi
    if [ -z "$randgen_test" ]; then
        randgen_test="./randgen_test"
    fi
    echo "SKIP BUILDING tidb-server,  USING EXISTING BINARY: $tidb_server"
    echo "SKIP BUILDING randgen-test, USING EXISTING BINARY: $randgen_test"
fi

rm -rf $randgen_test_log

echo "START tidb_server, LOG FILE: $randgen_test_log"
if [ "${TIDB_TEST_STORE_NAME}" = "tikv" ]; then
    $tidb_server -config config.toml -store tikv -path "${TIKV_PATH}" > $randgen_test_log 2>&1 &
    SERVER_PID=$!
else
    $tidb_server -config config.toml -store unistore -path "" > $randgen_test_log 2>&1 &
    SERVER_PID=$!
fi
echo "tidb-server(PID: $SERVER_PID) STARTED"

sleep 5

if [ $record -eq 1 ]; then
    if [ "$record_case" = 'all' ]; then
        echo "RECORD RESULT FOR ALL CASES"
        $randgen_test --record --log-level=error --port=4001
    else
        echo "RECORD RESULT FOR CASE: \"$record_case\""
        $randgen_test --record $record_case --log-level=error --port=4001
    fi
else
    if [ -z "$tests" ]; then
        echo "RUN ALL RANDGEN TEST CASES"
    else
        echo "RUN RANDGEN TEST CASE: $tests"
    fi
    $randgen_test --log-level=error --port=4001 $tests
fi

race=`grep 'DATA RACE' $randgen_test_log || true`
if [ ! -z "$race" ]; then
    echo "tidb-server DATA RACE!"
    cat $randgen_test_log
    exit 1
fi
echo "RANDGEN TEST FINISHED SUCCESSFULLY!"
