#!/bin/bash

function load_tpch()
{
	local scale="${1}"
	local table="${2}"
	local sync_to_tiflash="${3}"
	local ti_file="${4}"

	"${integrated}/ops/ti.sh" "${ti_file}" "tpch/load" "${scale}" "${table}" "${sync_to_tiflash}"
}
export -f load_tpch

function run_cluster()
{
	local ti_file="${1}"
	"${integrated}/ops/ti.sh" "${ti_file}" run
}
export -f run_cluster

function wait_table()
{
	local db="${1}"
	local table="${2}"
	local ti_file="${3}"
	local timeout='1200'
	"${integrated}/ops/ti.sh" "${ti_file}" wait/available "${db}" "${table}"
}
export -f wait_table

function enable_failpoint()
{
	local failpoint="${1}"
	local action="${2}"
	local ti_file="${3}"
	"${integrated}/ops/ti.sh" "${ti_file}" tidb/failpoint/enable "${failpoint}" "${action}"
}
export -f enable_failpoint

function disable_failpoint()
{
	local failpoint="${1}"
	local ti_file="${2}"
	"${integrated}/ops/ti.sh" "${ti_file}" tidb/failpoint/disable "${failpoint}"
}
export -f disable_failpoint

function reload_config()
{
	local module="${1}"
	local key="${2}"
	local value="${3}"
	local ti_file="${4}"
	if [ `uname` == "Darwin" ]; then
		sed -i '' "s/^\(${key} =\).*$/\1 ${value}/g" "${integrated}/conf/${module}.toml"
	else
		sed -i "s/^\(${key} =\).*$/\1 ${value}/g" "${integrated}/conf/${module}.toml"
	fi
	"${integrated}/ops/ti.sh" -m "${module}" "${ti_file}" stop ":" up
}
export -f reload_config

function run_file()
{
	local target="${1}"
	local ti_file="${2}"
	local run_test_entry="${3}"
	local ti="${4}"
	local verbose="${5}"

	python "${run_test_entry}" "${target}" "${ti}" "${ti_file}" "true" "${verbose}"
	echo "${target}": OK
}

function run_dir()
{
	local target="${1}"
	local ti_file="${2}"
	local run_test_entry="${3}"
	local ti="${4}"
	local verbose="${5}"

	find "${target}" -maxdepth 1 -name "*.test" -type f | sort | while read file; do
		if [ -f "${file}" ]; then
			run_file "${file}" "${ti_file}" "${run_test_entry}" "${ti}" "${verbose}"
		fi
	done

	find "${target}" -maxdepth 1 -type d | sort -r | while read dir; do
		if [ -d "${dir}" ] && [ "${dir}" != "${target}" ]; then
			run_dir "${dir}" "${ti_file}" "${run_test_entry}" "${ti}" "${verbose}"
		fi
	done
}

function cmd_ti_fullstatck_run()
{
	local file="${1}"
	local args="${2}"
	shift 6
	local ti="${integrated}/ops/ti.sh"
	local here="`cd $(dirname ${BASH_SOURCE[0]}) && pwd`"
	if [ -z "${1+x}" ]; then
		local test_cases="${here}/fullstack_tests"
	else
		local test_cases="${here}/fullstack_tests/${1}"
	fi
	local gen_tests="${2:-"true"}"
	local verbose="${3:-"false"}"

	if [ -d "${test_cases}" ]; then
		run_dir "${test_cases}" "${file}" "${here}/fullstack_run.py" "${ti}" "${verbose}"
	elif [ -f "${test_cases}" ]; then
		run_file "${test_cases}" "${file}" "${here}/fullstack_run.py" "${ti}" "${verbose}"
	else
		echo "${test_cases} not found" >&2
		return 1
	fi

	if [ "${gen_tests}" == "true" ]; then
		local gen_tests_dir="${here}/fullstack_tests/ci/gen_tests"
		local temp_gen_tests_dir="${gen_tests_dir}.`date +%s`.${RANDOM}"
		python "${here}/fullstack_generate.py" "test" "t" "${temp_gen_tests_dir}"
		rm -rf "${gen_tests_dir}"
		mv "${temp_gen_tests_dir}" "${gen_tests_dir}"
		run_dir "${gen_tests_dir}" "${file}" "${here}/fullstack_run.py" "${ti}" "${verbose}"
	fi
}

set -euo pipefail
cmd_ti_fullstatck_run "${@}"
