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

### DB Info ###
#dbname=${WEBTOOL_DB_NAME}
#dbip=$(cat /etc/hosts | grep CTRL | awk -F" " '{print $1}')
#dbuser=${WEBTOOL_DB_USER}
#dbpass=${WEBTOOL_DB_PASSWORD}
### DB Info ###

check_filename_old="/tmp/filelist.old"
check_filename_current="/tmp/filelist.now"
output_filename="/tmp/filelist_diff_monitor.log"

FILE_ARRAY=()
FILE_ARRAY+=("FullFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}.txt")
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+=("FullGameDataTranslateFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}_${LANG}.txt")
done

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

for file in $(echo "${FILE_ARRAY[@]}"); do
	cd $HOME/bin/;
	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 "### Generate File Mapping Start ###" | colorize cyan black
if [ "${md5sum_filename_current}" != "${md5sum_filename_old}" ]; then
	cat ~/bin/*${GAME_TYPE}${COUNTRY_CODE_DISPLAY}*.txt > /tmp/FullFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}_all.txt
	rsync -avz /tmp/FullFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}_all.txt CTRL:/tmp/.
	
	ssh CTRL "psql -U postgres WebTool -c \"CREATE TABLE IF NOT EXISTS filelist_mapping_tmp ( filename TEXT, path TEXT, nfs_filename TEXT )\""
	ssh CTRL "psql -U postgres WebTool -c \"CREATE TABLE IF NOT EXISTS filelist_mapping ( filename TEXT, path TEXT, nfs_filename TEXT )\""
	ssh CTRL "psql -U postgres WebTool -c \"CREATE TABLE IF NOT EXISTS filelist_mapping_old ( filename TEXT, path TEXT, nfs_filename TEXT )\""
	ssh CTRL "psql -U postgres WebTool -c \"TRUNCATE filelist_mapping_tmp\""
	ssh CTRL "psql -U postgres WebTool -c \"COPY filelist_mapping_tmp FROM '/tmp/FullFileList_${GAME_TYPE}${COUNTRY_CODE_DISPLAY}_all.txt' CSV\""
	#DIFF=$(ssh CTRL "psql -U postgres WebTool -c \"COPY (SELECT * FROM filelist_mapping_tmp FULL OUTER JOIN filelist_mapping ON filelist_mapping_tmp.filename = filelist_mapping.filename WHERE filelist_mapping_tmp.filename IS NULL OR filelist_mapping.filename IS NULL) TO STDOUT CSV\"")
	DIFF=$(ssh CTRL "psql -U postgres WebTool -c \"COPY (SELECT * FROM filelist_mapping_tmp FULL OUTER JOIN filelist_mapping ON (filelist_mapping_tmp.filename,filelist_mapping.path) = (filelist_mapping.filename,filelist_mapping_tmp.path) WHERE filelist_mapping_tmp.filename IS NULL OR filelist_mapping.filename IS NULL OR (filelist_mapping.filename,filelist_mapping.path,filelist_mapping.nfs_filename) <> (filelist_mapping_tmp.filename,filelist_mapping_tmp.path,filelist_mapping_tmp.nfs_filename)) TO STDOUT CSV\"")
	
	if [ ! -z "${DIFF}" ]; then
		ssh CTRL "psql -U postgres WebTool -c \"TRUNCATE filelist_mapping_old\""
		ssh CTRL "psql -U postgres WebTool -c \"INSERT INTO filelist_mapping_old SELECT * FROM filelist_mapping\""
		ssh CTRL "psql -U postgres WebTool -c \"TRUNCATE filelist_mapping\""
		ssh CTRL "psql -U postgres WebTool -c \"INSERT INTO filelist_mapping SELECT * FROM filelist_mapping_tmp\""
	
		CHECK=$(ssh CTRL "psql -U postgres WebTool -c \"COPY (SELECT COUNT(*) FROM filelist_mapping_old WHERE filename NOT IN (SELECT filename FROM filelist_mapping)) TO STDOUT;\"")
		if [ "${CHECK}" -gt 0 ]; then
			ssh CTRL "psql -U postgres WebTool -c \"COPY (SELECT * FROM filelist_mapping_old WHERE filename NOT IN (SELECT filename FROM filelist_mapping) ORDER BY filename) TO '${output_filename}' HEADER CSV\""
			rsync -avz CTRL:${output_filename} /tmp/
	
			LOG_SIZE=$(stat --printf="%s" ${output_filename})
			if [ "${LOG_SIZE}" -gt 0 ]; then
				send_messages WARNING filelist_diff "Client Patch (遊戲內檔案+翻譯檔) 檔案拔除確認"
			fi
		fi
	fi
else
	echo "Files are the same and won't be synchronized!"
fi
echo "### Generate File Mapping End ###" | colorize cyan black
