#!/bin/bash
# parameter 1: work directory
# parameter 2: config file for sync_diff_inspector
# parameter 3: max check times
# parameter 4: check diff should fail or not

workdir=$1
conf=$2
if [ $# -ge 3 ]; then
	check_time=$3
else
	check_time=20
fi

PWD=$(pwd)
binary=$PWD/bin/sync_diff_inspector
OUTPUT_DIR=/tmp/ticdc_dm_test/output/
STDOUT=$workdir/sync_diff_stdout.log

mkdir -p /tmp/ticdc_dm_test

cd $workdir
i=0
while [ $i -lt $check_time ]; do
	rm -rf $OUTPUT_DIR
	$binary --config=$conf -L=debug >$STDOUT 2>&1
	ret=$?
	if [ "$ret" == 0 ]; then
		echo "check diff successfully"
		if [[ $4 = "fail" ]]; then
			exit 1
		fi
		break
	fi
	((i++))
	echo "check diff failed $i-th time, retry later"
	sleep 2
done

if [ $i -ge $check_time ]; then
	echo "check diff failed at last"
	if [[ $4 != "fail" ]]; then
		# show \n and other blanks
		printf "$STDOUT\n"
		cat $STDOUT
		printf "$OUTPUT_DIR/sync_diff.log\n"
		cat $OUTPUT_DIR/sync_diff.log
		printf "\n"
		exit 1
	fi
fi
cd $PWD
