#!/bin/bash

function cmd_ti_repeat()
{
	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

	if [ -z "${1+x}" ] || [ -z "${1}" ]; then
		echo "[cmd repeat] usage: ti.sh [flags] ti_file_path repeat repeat_times cmd [args]" >&2
		return 1
	fi

	local repeat_times="${1}"
	shift 1

	local cmds_str=`esc_args "${@}"`
	local is_cmd_chain=`echo "${cmds_str}" | grep '":"'`

	for (( r = 0; r < "${repeat_times}"; r++ )); do
		if [ -z "${is_cmd_chain}" ]; then
			print_hhr
			echo ":: repeat ${r}/$((repeat_times - 1))> ${@}"
			print_hr
		fi
		"${integrated}/ops/ti.sh" -h "${cmd_hosts}" -m "${cmd_mod_names}" \
			-i "${cmd_indexes}" -k "${ti_args}" "${ti_file}" "${@}"
		sleep 0.1
	done
}

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