#!/bin/bash

# 載入 gamerc
source ~/.gamerc

# 台版任務ID與名稱對應
cat ${WORKING_DIRECTORY}/common-${SOURCE_OF_SYNC_TO_LIVE}/Data/db/s_passiveattr.ini | egrep '^[0-9].*' | sed 's/,/=>/g' | awk -F"|" '{print $1","$3","$42}' | grep '^[0-9]\{1,5\},' > /tmp/passiveattr2db_tw_dos.csv
cp -arf /tmp/passiveattr2db_tw_dos.csv /tmp/passiveattr2db_tw.csv
dos2unix /tmp/passiveattr2db_tw.csv

# 台版任務對應 CSV 同步到 Ctrl 機, 並更新到 passiveattr2db
scp /tmp/passiveattr2db_tw.csv ctrl:/tmp

ssh ctrl "psql -U postgres WebTool -c \"CREATE TABLE IF NOT EXISTS passiveattr2db (passiveattr_id integer primary key, passiveattrname text, tipinfo text)\""
ssh ctrl "psql -U postgres WebTool -c \"TRUNCATE passiveattr2db\""
ssh ctrl "psql -U postgres WebTool -c \"COPY passiveattr2db (passiveattr_id,passiveattrname,tipinfo) FROM '/tmp/passiveattr2db_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_passiveattr_${LANG}.ini | egrep '^[0-9].*' | sed 's/,/=>/g' | awk -F"|" '{print $1","$2}' | grep '^[0-9]\{1,5\},' > /tmp/passiveattr2db_${LANG}.csv

			# 翻譯任務對應 CSV 同步到 Ctrl 機
			scp /tmp/passiveattr2db_${LANG}.csv ctrl:/tmp/

        		ssh ctrl "psql -U postgres WebTool -c \"CREATE TABLE passiveattr2db_${LANG}_temp (passiveattr_id integer, passiveattr_name text)\""
        		ssh ctrl "psql -U postgres WebTool -c \"COPY passiveattr2db_${LANG}_temp FROM '/tmp/passiveattr2db_${LANG}.csv' CSV\""
			ssh ctrl "psql -U postgres WebTool -c \"ALTER TABLE passiveattr2db ADD COLUMN IF NOT EXISTS passiveattrname_${LANG} text\""
        		ssh ctrl "psql -U postgres WebTool -c \"UPDATE passiveattr2db SET passiveattrname_${LANG} = passiveattr2db_${LANG}_temp.passiveattr_name from passiveattr2db_${LANG}_temp WHERE passiveattr2db.passiveattr_id = passiveattr2db_${LANG}_temp.passiveattr_id\""
        		ssh ctrl "psql -U postgres WebTool -c \"DROP TABLE passiveattr2db_${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_passiveattr_${LANG}.ini | egrep '^[0-9].*' | sed 's/,/=>/g' | awk -F"|" '{print $1","$2}' | grep '^[0-9]\{1,5\},' > /tmp/passiveattr2db_${LANG}.csv

	# 翻譯任務對應 CSV 同步到 Ctrl 機
	scp /tmp/passiveattr2db_${LANG}.csv ctrl:/tmp/

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