#!/bin/bash

function cmd_ti_mapped_table()
{
	local ti_file="${1}"
	local ti_args="${2}"
	local cmd_mod_names="${3}"
	local cmd_hosts="${4}"
	local cmd_indexes="${5}"
	local mods="${6}"
	shift 6

	local mod=`from_mods_random_mod "${mods}" 'tiflash' 'false'`
	if [ -z "${mod}" ]; then
		echo "[cmd ch/mapped_table] no tiflash selected" >&2
		return 1
	fi

	if [ -z "${2+x}" ]; then
		echo "[cmd ch/mapped_table] usage: <cmd> database table [qualify=true]" >&2
		return 1
	fi
	
	local db_name="${1}"
	local tab_name="${2}"
	shift 2
	if [ ! -z "${1+x}" ] && [ ! -z "${1}" ]; then
		local query_str="DBGInvoke mapped_table(${db_name}, ${tab_name}, '${1}')"
	else
		local query_str="DBGInvoke mapped_table(${db_name}, ${tab_name})"
	fi

	local dir=`from_mod_get_dir "${mod}"`
	local host=`from_mod_get_host "${mod}"`
	local port=`from_mod_get_proc_info "${mod}" "tcp_port"`
	if [ -z "${port}" ]; then
		echo "[cmd ch/queries] getting port from proc.info failed" >&2
		return 1
	fi

	local tiflash_bin_dir="${dir}/tiflash"
	local result=`run_query_through_ch_client "${tiflash_bin_dir}/tiflash" --host="${host}" --port="${port}" \
		-f "TabSeparated" --query="${query_str}"`
	echo $result

	# Check if we get mapped name successfully
	if [ -z "`echo $result | grep -i '^Table .* not found.$'`" ]; then
		return 0
	else
		return 1
	fi
}

set -euo pipefail
cmd_ti_mapped_table "${@}"
