#!/bin/bash
[ "$2" == "" ] && exec echo "Usage: $0 <CLIENT_PATCH_VERSION> <CLIENT_PATCH_CHECK_FILE>"
source $HOME/.gamerc

CLIENT_PATCH_VERSION=$1
CLIENT_PATCH_CHECK_FILE=$2

# Read the file line by line
while IFS= read -r line; do
	#echo ${line}
	CHECK="0"
	filename=$(basename ${line} | awk -F"." '{print $1}')
	filename_ext=$(basename ${line} | awk -F"." '{print $2}')
	dirname=$(dirname ${line})
	file_part1=$(echo ${filename} | cut -c 1-16)
	file_part2=$(echo ${filename} | cut -c 17-24)

	file_pattern="$HOME/www/${GAME_TYPE}-TEST_SYNCTOLIVE/${line}"
	LOG_SIZE=$(stat --printf="%s" ${file_pattern})
	if [ -f "${file_pattern}" ] && [ "${LOG_SIZE}" -gt 0 ]; then
		CHECK_FILE_EXIST="O"
	else
		CHECK_FILE_EXIST="X"
	fi

	client_info=$(cat $HOME/patch/${CLIENT_PATCH_VERSION}/FullFileList_* | egrep ${file_part1} | egrep ${file_part2})
	if [ "${client_info}" != "" ]; then
		file_location=$(echo ${client_info} | awk -F"," '{print $2}')
		file_realname=$(echo ${client_info} | awk -F"," '{print $1}')
		file_convert="${file_location}${file_realname}"
		echo "${line} => ${CHECK_FILE_EXIST} => ${file_convert}"
		CHECK="1"
	else
		for lang in $(cat $HOME/.gamerc | grep TRANSLATED_INI_UPLOAD_DIR_ | grep -v '#' | awk -F"=" '{print $1}' | sed 's/TRANSLATED_INI_UPLOAD_DIR_//' | tr 'A-Z' 'a-z'); do
			translate_info=$(cat $HOME/www/${GAME_TYPE}-TEST_SYNCTOLIVE/GameDataTranslateFileList_${lang}.txt* | egrep ${file_part1} | egrep ${file_part2})
			if [ "${translate_info}" ]; then
				UPPER_LANG=${lang^^}
				echo "${line} => ${CHECK_FILE_EXIST} => in TRANSLATE ${UPPER_LANG}"
				CHECK="1"
			fi
		done
	fi

	if [ "${CHECK}" == "0" ]; then
		echo "${line} => ${CHECK_FILE_EXIST} => X"
	fi
done < "${CLIENT_PATCH_CHECK_FILE}"
