#!/bin/bash

function cmd_ti_ci_self()
{
	local file="${1}"
	local args="${2}"
	local mods="${6}"
	shift 6

	local ti="${integrated}/ops/ti.sh"

	"${ti}" -k "${args}" "${file}" stop : fstop : predeploy : up : sleep 5
	"${ti}" -k "${args}" "${file}" kill/tiflash : kill/tikv : kill/storage : kill/random
	"${ti}" -k "${args}" "${file}" up : sleep 5 : status : prop : du : log 1 : log 1 info
	"${ti}" -k "${args}" "${file}" pd/ctl_raw store : pd/ctl store : mysql/host : mysql/port
	"${ti}" -k "${args}" "${file}" byhost/do uptime : byhost/procs
	"${ti}" -k "${args}" "${file}" ver : ver ver : ver githash : ver full : sleep 1
	"${ti}" -k "${args}" "${file}" safe_point : safe_point ts : safe_point time
	"${ti}" -k "${args}" "${file}" perf/top : perf/top true : perf/top false

	"${ti}" -k "${args}" "${file}" repeat 2 status

	"${ti}" -k "${args}" "${file}" tikv/ctl_raw true --help 1>/dev/null
	"${ti}" -k "${args}" "${file}" perf/fio --help 1>/dev/null

	"${ti}" -k "${args}" "${file}" ch "show databases" 1>/dev/null
	"${ti}" -k "${args}" "${file}" ch "show databases" '' 1>/dev/null
	"${ti}" -k "${args}" "${file}" ch "show databases" '' 'tab' 1>/dev/null
	"${ti}" -k "${args}" "${file}" ch "show databases" '' 'title' 1>/dev/null
	"${ti}" -k "${args}" "${file}" ch "show databases" '' 'pretty' 1>/dev/null
	"${ti}" -k "${args}" "${file}" ch 'show databases' '' 'pretty' 'true' 1>/dev/null
	"${ti}" -k "${args}" "${file}" ch 'show databases' '' 'pretty' 'false' 1>/dev/null

	"${ti}" -k "${args}" "${file}" mysql 'show databases' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql 'show databases' 'test' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql 'show databases' 'test' 'true' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql 'show databases' 'test' 'false' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/tikv 'show databases' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/tikv 'show databases' 'test' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/tikv 'show databases' 'test' 'true' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/tikv 'show databases' 'test' 'false' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner 'show databases' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner 'show databases' 'test' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner 'show databases' 'test' 'true' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner 'show databases' 'test' 'false' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner_by_hint 'show databases' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner_by_hint 'show databases' 'test' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner_by_hint 'show databases' 'test' 'true' 1>/dev/null
	"${ti}" -k "${args}" "${file}" mysql/learner_by_hint 'show databases' 'test' 'false' 1>/dev/null

	local beeline_exists=`from_mods_random_mod "${mods}" 'spark_m'`
	if [ ! -z "${beeline_exists}" ]; then
		local res=`"${ti}" -k "${args}" "${file}" beeline 'show databases' default | grep test`
		test ! -z "${res}"
	fi

	# TODO: Fixme: flame dependences
	#local tiflash_mod=`from_mods_random_mod "${mods}" 'tiflash'`
	#if [ ! -z "${tiflash_mod}" ]; then
	#	"${ti}" -m tiflash -k "${args}" "${file}" 'perf/flame:perf/flame 5'
	#fi

	# TODO: Fixme: ssh failed
	#${ti}" -k "${args}" "${file}" 'perf/io_report:byhost/local/do date'

	# TODO test: loop, floop
	# TODO need sudo to test: perf/drop_page_cache, perf/hw
}

set -euo pipefail
cmd_ti_ci_self "${@}"
