#!/bin/bash

# 載入 gamerc
source ~/.gamerc

output_filename="/tmp/item2db_diff_monitor.log"

# 台版道具ID與名稱對應
cat ${WORKING_DIRECTORY}/common-${SOURCE_OF_SYNC_TO_LIVE}/Data/db/S_Item.ini ${WORKING_DIRECTORY}/common-${SOURCE_OF_SYNC_TO_LIVE}/Data/db/S_ItemMall.ini | egrep '^[0-9].*' | awk -F"|" '{print $1","$8}' | egrep -v ',$' > /tmp/powerupitem_tw.csv

# 台版道具對應 CSV 同步到 Ctrl 機, 並更新到 powerupitem
rsync -avz /tmp/powerupitem_tw.csv ctrl:/tmp

ssh ctrl "psql -U postgres WebTool -c \"CREATE TABLE IF NOT EXISTS powerupitem (item_id integer primary key, itemname text)\""
ssh ctrl "psql -U postgres WebTool -c \"CREATE TABLE IF NOT EXISTS powerupitem_old (item_id integer primary key, itemname text)\""
ssh ctrl "psql -U postgres WebTool -c \"TRUNCATE powerupitem_old\""
ssh ctrl "psql -U postgres WebTool -c \"INSERT INTO powerupitem_old SELECT item_id, itemname FROM powerupitem ORDER BY item_id\""
ssh ctrl "psql -U postgres WebTool -c \"TRUNCATE powerupitem\""
ssh ctrl "psql -U postgres WebTool -c \"COPY powerupitem (item_id,itemname) FROM '/tmp/powerupitem_tw.csv' csv\""

# 確認翻譯檔目錄架構
TRANSLATE_NUM=$(cat ${HOME}/.gamerc | grep TRANSLATED_INI_UPLOAD_DIR_ | grep -v '#' | wc -l)

if [ "${TRANSLATE_NUM}" -gt 0 ]; then
        # 多國語系翻譯檔架構
        echo "1: Multi Lang"

        for multi_lang_directory in $(cat ${HOME}/.gamerc | grep TRANSLATED_INI_UPLOAD_DIR_ | grep -v '#' | awk -F"=" '{print $2}'); do
                LANG=$(echo ${multi_lang_directory} | sed 's/"//g' | awk -F"/" '{print $(NF-1)}' | awk -F"_" '{print $NF}')
                LANG_DIRECTORY=$(echo ${multi_lang_directory} | sed 's/"//g')
                echo "${LANG} => ${LANG_DIRECTORY}"

                ULANG=$(echo ${LANG} | tr [a-z] [A-Z])
                if [[ ${TRANSLATED_LANG_TO_POWERUPITEM} =~ "${ULANG}" ]]; then
                        # 翻譯道具ID與名稱對應
                        cat ${LANG_DIRECTORY}/T_item_${LANG}.ini ${LANG_DIRECTORY}/T_itemmall_${LANG}.ini | egrep ^[0-9].* | sed 's/,/=>/g;s/"//g' | awk -F"|" '{print $1","$2}' | sed 's/, /,/' | grep '^[0-9]\{5\},' > /tmp/powerupitem_${LANG}.csv

                        # 翻譯道具對應 CSV 同步到 Ctrl 機
                        rsync -avz /tmp/powerupitem_${LANG}.csv ctrl:/tmp/

                        ssh ctrl "psql -U postgres WebTool -c \"CREATE TABLE powerupitem_${LANG}_temp (item_id integer, item_name text)\""
                        ssh ctrl "psql -U postgres WebTool -c \"COPY powerupitem_${LANG}_temp FROM '/tmp/powerupitem_${LANG}.csv' CSV\""
                        ssh ctrl "psql -U postgres WebTool -c \"ALTER TABLE powerupitem ADD COLUMN IF NOT EXISTS itemname_${LANG} text\""
                        ssh ctrl "psql -U postgres WebTool -c \"UPDATE powerupitem SET itemname_${LANG} = powerupitem_${LANG}_temp.item_name from powerupitem_${LANG}_temp WHERE powerupitem.item_id = powerupitem_${LANG}_temp.item_id\""
                        ssh ctrl "psql -U postgres WebTool -c \"DROP TABLE powerupitem_${LANG}_temp\""
                fi
        done


else
        # 單一語系翻譯檔架構
        echo "2: Single Lang"

        single_lang_directory=$(cat ${HOME}/.gamerc | grep 'TRANSLATED_INI_UPLOAD_DIR='| awk -F"=" '{print $2}')
        LANG=$(cat ${HOME}/.gamerc | grep 'COUNTRY_CODE=' | sed 's/"//g' | awk -F"=" '{print $2}' | tr [A-Z] [a-z])
        LANG_DIRECTORY=$(echo ${single_lang_directory} | sed 's/"//g')
        echo "${LANG} => ${LANG_DIRECTORY}"


        # 翻譯道具ID與名稱對應
        cat ${LANG_DIRECTORY}/T_item_${LANG}.ini ${LANG_DIRECTORY}/T_itemmall_${LANG}.ini | egrep ^[0-9].* | sed 's/,/=>/g;s/"//g' | awk -F"|" '{print $1","$2}' | sed 's/, /,/' | grep '^[0-9]\{5\},' > /tmp/powerupitem_${LANG}.csv

        # 翻譯道具對應 CSV 同步到 Ctrl 機
        rsync -avz /tmp/powerupitem_${LANG}.csv ctrl:/tmp/

        ssh ctrl "psql -U postgres WebTool -c \"CREATE TABLE powerupitem_${LANG}_temp (item_id integer, item_name text)\""
        ssh ctrl "psql -U postgres WebTool -c \"COPY powerupitem_${LANG}_temp FROM '/tmp/powerupitem_${LANG}.csv' CSV\""
        ssh ctrl "psql -U postgres WebTool -c \"ALTER TABLE powerupitem ADD COLUMN IF NOT EXISTS itemname_${LANG} text\""
        ssh ctrl "psql -U postgres WebTool -c \"UPDATE powerupitem SET itemname_${LANG} = powerupitem_${LANG}_temp.item_name from powerupitem_${LANG}_temp WHERE powerupitem.item_id = powerupitem_${LANG}_temp.item_id\""
        ssh ctrl "psql -U postgres WebTool -c \"DROP TABLE powerupitem_${LANG}_temp\""
fi

CHECK=$(ssh ctrl "psql -U postgres WebTool -c \"COPY (SELECT COUNT(*) FROM powerupitem_old WHERE item_id NOT IN (SELECT item_id FROM powerupitem)) TO STDOUT;\"")
if [ "${CHECK}" -gt 0 ]; then
	ssh ctrl "psql -U postgres WebTool -c \"COPY (SELECT item_id FROM powerupitem_old WHERE item_id NOT IN (SELECT item_id FROM powerupitem) ORDER BY item_id) 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 item2db_diff "S_Item.ini S_ItemMall.ini 道具拔除確認"
	fi
fi
