#!/bin/bash
source $HOME/.gamerc

check_filename_old="/tmp/translate_filelist.old"
check_filename_current="/tmp/translate_filelist.now"

FILE_ARRAY=()
for LANG in $(cat ~/.gamerc | grep TRANSLATED_INI_UPLOAD_DIR_ | grep -v \# | awk -F"=" '{print $1}' | awk -F"_" '{print $NF}' | tr [A-Z] [a-z]); do
	FILE_ARRAY+=(".localize_ctime_${LANG}")
done

[ -f "${check_filename_current}" ] && mv ${check_filename_current} ${check_filename_old}

for file in $(echo "${FILE_ARRAY[@]}"); do
	cd $HOME/;
	md5sum ${file} >> ${check_filename_current}
done

[ -f "${check_filename_old}" ] && md5sum_filename_old=$(md5sum ${check_filename_old} | awk -F" " '{print $1}')
md5sum_filename_current=$(md5sum ${check_filename_current} | awk -F" " '{print $1}')

echo "### Convert Localization Mapping Start ###" | colorize cyan black
if [ "${md5sum_filename_current}" != "${md5sum_filename_old}" ]; then
	for multi_lang_directory in $(cat ${HOME}/.gamerc | grep TRANSLATED_INI_UPLOAD_DIR_ | grep -v '#' | awk -F"=" '{print $2}'); do
		LCOUNTRY=$(echo ${multi_lang_directory} | sed 's/"//g' | awk -F"/" '{print $(NF-1)}' | awk -F"_" '{print $NF}')
		COUNTRY=$(echo ${LCOUNTRY} | tr '[:lower:]' '[:upper:]')
		TRANSLATE_DIRECTORY=$(echo ${multi_lang_directory} | sed 's/"//g')
		FILELIST_NAME="FullGameDataTranslateFileList_${LCOUNTRY}.txt"
		OUTPUT_FILELIST_NAME="FullGameDataTranslateFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}_${LCOUNTRY}.txt"
	
		#echo "${COUNTRY} => ${TRANSLATE_DIRECTORY}"
		cd ${TRANSLATE_DIRECTORY}
		cat ${FILELIST_NAME} | awk -F"," '{print $1","$2","$3$9}' > $HOME/bin/${OUTPUT_FILELIST_NAME}
		sed -i '1d' $HOME/bin/${OUTPUT_FILELIST_NAME}
	done
else
	echo "Localization Files are the same and won't be synchronized!"
fi
echo "### Convert Localization Mapping End ###" | colorize cyan black
