#!/bin/bash

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

function _lightning_conf_template_setup()
{
	if [ -z "${12+x}" ] || [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] || [ -z "${4}" ] || [ -z "${5}" ] || [ -z "${6}" ] || [ -z "${7}" ] || [ -z "${8}" ] ; then
		echo "[func _lightning_conf_template_setup] usage: <func> lightning_dir conf_file log_file mods data_source check_checksum" >&2
		echo "                                            header separator delimiter not_null null backslash_escape trim_last_separator" >&2
		return 1
	fi

	local lightning_dir="${1}"
	local conf_file="${2}"
	local log_file="${3}"
	local conf_templ_dir="${integrated}/conf" # TODO: fix the hardcode ${integrated}
	local mods="${4}"
	local data_source="${5}"
	local check_checksum="${6}"

	local header="${7}"
	local separator="${8}"
	local delimiter="${9}"
	local not_null="${10}"
	local null="${11}"
	local backslash_escape="${12}"
	local trim_last_separator="${13}"
	# Generate random suffix if empty
	local suffix="${14:-"${RANDOM}"}"

	local tidb_mod=`from_mods_random_mod "${mods}" 'tidb' 'false'`
	local tidb_host=`from_mod_get_host "${tidb_mod}"`
	local tidb_port=`from_mod_get_proc_info "${tidb_mod}" "tidb_port"`
	local tidb_status_port=`from_mod_get_proc_info "${tidb_mod}" "status_port"`
	if [ -z "${tidb_mod}" ] || [ -z "${tidb_host}" ] || [ -z "${tidb_port}" ] || [ -z "${tidb_status_port}" ] ; then
		echo "[cmd lightning/load] getting tidb address from random instance failed" >&2
		return 1
	fi

	local pd_mod=`from_mods_random_mod "${mods}" 'pd' 'false'`
	local pd_host=`from_mod_get_host "${pd_mod}"`
	local pd_port=`from_mod_get_proc_info "${pd_mod}" "pd_port"`
	if [ -z "${pd_mod}" ] || [ -z "${pd_host}" ] || [ -z "${pd_port}" ]; then
		echo "[cmd lightning/load] getting pd address from random instance failed" >&2
		return 1
	fi

	local importer_backend="importer"
	local importer_sorted_dir=""
	local importer_mod=`from_mods_random_mod "${mods}" 'tikv_importer' 'false'`
	local importer_host=`from_mod_get_host "${importer_mod}"`
	local importer_port=`from_mod_get_proc_info "${importer_mod}" "listen_port"`
	if [ -z "${importer_mod}" ] || [ -z "${importer_host}" ] || [ -z "${importer_port}" ]; then
		local importer_backend="local"
		local tikv_mod=`from_mods_random_mod "${mods}" 'tikv' 'false'`
		local importer_sorted_dir="`from_mod_get_dir "${tikv_mod}"`/ti.sh.lightning.${suffix}"
		mkdir -p "${importer_sorted_dir}"
	fi

	# render str for config template
	local render_str="importer_backend=${importer_backend}"
	if [ "${importer_backend}" == "local" ]; then
		local render_str="${render_str}#importer_addr="
		local render_str="${render_str}#importer_sorted_dir=${importer_sorted_dir}"
	else
		local render_str="${render_str}#importer_addr=${importer_host}:${importer_port}"
		local render_str="${render_str}#importer_sorted_dir="""
	fi
	local render_str="${render_str}#lightning_log=${log_file}"
	local render_str="${render_str}#suffix=${suffix}"
	local render_str="${render_str}#tidb_listen_host=${tidb_host}"
	local render_str="${render_str}#tidb_listen_port=${tidb_port}"
	local render_str="${render_str}#tidb_status_port=${tidb_status_port}"
	local render_str="${render_str}#pd_addr=${pd_host}:${pd_port}"
	local render_str="${render_str}#lightning_dir=${lightning_dir}"
	local render_str="${render_str}#server_mode=false"
	local render_str="${render_str}#lightning_addr="
	# data sources
	local render_str="${render_str}#data_source=${data_source}"
	local render_str="${render_str}#check_checksum=${check_checksum}"
	# format args in toml
	local render_str="${render_str}#header=${header}"
	local render_str="${render_str}#separator=${separator}"
	local render_str="${render_str}#delimiter=${delimiter}"
	local render_str="${render_str}#not_null=${not_null}"
	local render_str="${render_str}#null=${null}"
	local render_str="${render_str}#backslash_escape=${backslash_escape}"
	local render_str="${render_str}#trim_last_separator=${trim_last_separator}"
	render_templ "${conf_templ_dir}/tidb-lightning.toml" "${conf_file}" "${render_str}"
}

function ti_cmd_lightning_load()
{
	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 "${3+x}" ] || [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ; then
		echo "[cmd lightning/load] usage: <cmd> database table data_source [check_checksum=true|false] [header] [separator] [delimiter] [not_null] [null] [backslash_escape] [trim_last_separator]" >&2
		echo "                            with check_checksum=false, loading will speed up a lot" >&2
		return 1
	fi

	local db="${1}"
	local table="${2}"
	local data_source="${3}"
	local check_checksum="${4:-"true"}"

	# mydumper.csv options. See `https://pingcap.com/docs-cn/stable/reference/tools/tidb-lightning/csv/` for details.
	# TPC-H DBGEN format by default.
	local header="${5:-"false"}"
	local separator="${6:-"|"}"
	local delimiter="${7:-""}"
	local not_null="${8:-"true"}"
	local null="${9:-""}"
	local backslash_escape="${10:-"false"}"
	local trim_last_separator="${11:-"true"}"

	if [ ! -d "${data_source}" ] ; then 
		echo "[func ti_cmd_lightning_load] data_source: ${data_source} is not exists" >&2
		return 1
	fi
	if [ "${check_checksum}" != "true" ] && [ "${check_checksum}" != "false" ]; then
		echo "[func ti_cmd_lightning_load] arg 'check_checksum' only can be true|false, got ${check_checksum}" >&2
		return 1
	fi

	# TODO: log files of tidb-lightning and tasks' toml is in /tmp/ti, maybe we should find a better place.
	local tmp_dir="/tmp/ti"
	local lightning_dir="${tmp_dir}/lightning"
	mkdir -p "${lightning_dir}"

	local num_files_to_load=`ls -l ${data_source}/${db}.${table}*.csv | wc -l | awk '{print $1}'`
	if [ "${num_files_to_load}" -eq 0 ]; then
		echo "[func ti_cmd_lightning_load] there is no valid files to load." >&2
		return 1
	fi

	echo "Loading ${num_files_to_load} files by tidb-lightning..."
	mkdir -p "${lightning_dir}/tasks"
	mkdir -p "${lightning_dir}/logs"
	local suffix="${RANDOM}"  # A random suffix for this task
	local conf_file="${lightning_dir}/tasks/tidb-lightning.${db}.${table}.`date +%s`.${suffix}.toml"
	local log_file="${lightning_dir}/logs/tidb-lightning.${suffix}.log"
	_lightning_conf_template_setup "${lightning_dir}" "${conf_file}" "${log_file}" "${mods}" \
		"${data_source}" "${check_checksum}" \
		"${header}" "${separator}" "${delimiter}" "${not_null}" "${null}" "${backslash_escape}" "${trim_last_separator}" \
		"${suffix}"
	if [ $? != 0 ]; then
		echo "[func ti_cmd_lightning_load] Fail to generate configuration file for lightning" >&2
		return 1
	fi

	# submit a job configured by ${conf_file}
	local bin_dir="${tmp_dir}/master/bins"
	ensure_bin_in_local_dir "tidb_lightning" "${bin_dir}"

	error_handle="$-"
	set +e

	echo "Start tidb-lightning with conf: ${conf_file}, log: ${log_file}"
	"${bin_dir}/tidb-lightning" --config "${conf_file}" > /dev/null
	# if tidb-lightning exit with status != 0, we should tell user to check log in ${lightning_dir}/tidb-lightning.log
	local lightning_result=$?
	if [ ${lightning_result} -ne 0 ]; then
		echo "[func ti_cmd_lightning_load] tidb-lightning exit with code ${lightning_result}. Check ${lightning_dir}/tidb-lightning.log for details." >&2
		return 1
	fi

	restore_error_handle_flags "${error_handle}"

	rm "${conf_file}"
}

ti_cmd_lightning_load "${@}"
