#!/bin/bash
# parameter 1: work directory
WORK_DIR=$1

set +e

logs=$(ls $WORK_DIR/stdout*.log)

if [ $? -eq 1 ]; then
	echo "no stdout log found"
	exit 0
fi

echo "log files: $logs"

grep -q -i 'DATA RACE' $WORK_DIR/stdout*.log

if [ $? -eq 0 ]; then
	# check each log file to see if it contains DATA RACE
	for log in $logs; do
		echo "check $log"
		grep -q -i 'DATA RACE' $log
		if [ $? -eq 1 ]; then
			continue
		fi
		# Contains reportStoreReplicaFlows means it is a kv client issue, not a TiCDC issue, so we can ignore it
		grep -q -i 'reportStoreReplicaFlows' $log
		if [ $? -eq 1 ]; then
			echo "found DATA RACE in $log, please check the logs"
			exit 1
		fi
	done
	echo "found DATA RACE, but it is not a TiCDC issue, please check the logs"
	exit 0
else
	echo "no DATA RACE found"
	exit 0
fi

grep -q -i 'panic' $WORK_DIR/stdout*.log

if [ $? -eq 0 ]; then
	# check each log file to see if it contains panic
	for log in $logs; do
		echo "check $log"
		grep -q -i 'panic' $log
		if [ $? -eq 1 ]; then
			continue
		fi
	done
	echo "found panic, but it is not a TiCDC issue, please check the logs"
	exit 0
else
	echo "no panic found"
	exit 0
fi
