#!/bin/bash

function ti_cmd_tpch_wait_syncing()
{
	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 db=''
	if [ ! -z "${1+x}" ]; then
		local db="${1}"
		shift 1
	fi

	if [ -z "${db}" ]; then
		local db=`"${integrated}/ops/ti.sh" -h "${cmd_hosts}" -m "${cmd_mod_names}" \
			-i "${cmd_indexes}" -k "${ti_args}" "${ti_file}" 'tpch/db' | trim_host`
	fi
	if [ -z "${db}" ]; then
		echo "[cmd tpch/wait] auto select tpch database failed" >&2
		return 1
	fi

	local tables=(lineitem orders partsupp part customer supplier nation part region)
	for table in ${tables[@]}; do
		if [ -z "${1+x}" ]; then
			"${integrated}/ops/ti.sh" -h "${cmd_hosts}" -m "${cmd_mod_names}" \
				-i "${cmd_indexes}" -k "${ti_args}" "${ti_file}" 'wait/syncing' "${db}" "${table}"
		else
			"${integrated}/ops/ti.sh" -h "${cmd_hosts}" -m "${cmd_mod_names}" \
				-i "${cmd_indexes}" -k "${ti_args}" "${ti_file}" 'wait/syncing' "${db}" "${table}" "${@}"
		fi
		if [ $? -ne 0 ]; then
			echo "[cmd tpch/wait] wait/syncing failed for ${db} ${table} " >&2
			return 1
		fi
	done
}

source "`cd $(dirname ${BASH_SOURCE[0]}) && pwd`/_env.sh"
auto_error_handle
ti_cmd_tpch_wait_syncing "${@}"
