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

output_filename="/tmp/localize_info_monitor.log"
[ -f "${output_filename}" ] && sudo rm -rf ${output_filename}

DBNAME="${GAME_DB_NAME}1"

COUNT=$(psql -U postgres ${DBNAME} -c "COPY (SELECT COUNT(*) FROM xl_localization_check WHERE tw_full_v IS NOT NULL AND (server_major_v <> tw_major_v OR server_major_v <> en_major_v OR server_major_v <> jp_major_v OR server_major_v <> cn_major_v)) TO STDOUT;")

if [ "${COUNT}" -ne 0 ]; then
    psql -U postgres ${DBNAME} -c "COPY (SELECT filename, server_major_v, tw_major_v, en_major_v, jp_major_v, cn_major_v FROM xl_localization_check WHERE tw_full_v IS NOT NULL AND (server_major_v <> tw_major_v OR server_major_v <> en_major_v OR server_major_v <> jp_major_v OR server_major_v <> cn_major_v) ORDER BY filename) TO '${output_filename}' CSV HEADER"

    OUTPUT=$(cat ${output_filename} | grep -v item_id | wc -l)
    if [ "${OUTPUT}" -ne 0 ]; then
    	send_messages ERROR localize_info "版本與翻譯版號有差異"
    fi
else
    if [ "$1" == "maintenance" ]; then
	if [ "${COUNT}" -eq 0 ]; then
		echo "OK!" > ${output_filename}
    		send_messages INFO localize_info "版本與翻譯版號無差異"
	fi
    fi
fi
