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

DBNAME="${GAME_DB_NAME}1"

# Create Table
psql -U postgres ${DBNAME} -c "CREATE TABLE IF NOT EXISTS xl_localization_check ( filename TEXT primary key, server_full_v TEXT, server_major_v TEXT, server_check TEXT, tw_full_v TEXT, tw_major_v TEXT, tw_check TEXT, en_full_v TEXT, en_major_v TEXT, en_check TEXT, jp_full_v TEXT, jp_major_v TEXT, jp_check TEXT, cn_full_v TEXT, cn_major_v TEXT, cn_check TEXT );" 

# Parser Server ini
cd $HOME/common-TEST/Data/db/; for file in $(ls -hl S_*.ini | grep -v S_RootCmds.ini | awk -F" " '{print $NF}'); do output=$(echo ${file}|awk -F"_" '{print $2}'|sed 's/.ini//'|tr '[:upper:]' '[:lower:]'); version=$(cat ${file}|head -n 1|sed 's/ //g;s/\r//g;s/|//;s/\xEF\xBB\xBF//g'|awk -F"," '{print $1}'); echo "${file} => ${output},${version}"; psql -U postgres ${DBNAME} -c "INSERT INTO xl_localization_check (filename, server_full_v) VALUES ('${output}', '${version}') ON CONFLICT (filename) DO UPDATE SET server_full_v = excluded.server_full_v;"; done 

# Parser TW ini
cd $HOME/common-TEST/Data/db/; for file in $(ls -hl T_*.ini | awk -F" " '{print $NF}'); do output=$(echo ${file}|awk -F"_" '{print $2}'|sed 's/.ini//'|tr '[:upper:]' '[:lower:]'); version=$(cat ${file}|head -n 1|sed 's/ //g;s/\r//g;s/\xEF\xBB\xBF//g'); echo "${file} => ${output},${version}"; psql -U postgres ${DBNAME} -c "INSERT INTO xl_localization_check (filename, tw_full_v) VALUES ('${output}', '${version}') ON CONFLICT (filename) DO UPDATE SET tw_full_v = excluded.tw_full_v;"; done 

# Parser Localize EN ini
cd /home/translate/localize_en/; for file in $(ls -hl T_*.ini | awk -F" " '{print $NF}'); do output=$(echo ${file}|awk -F"_" '{print $2}'); version=$(cat ${file}|head -n 1|sed 's/ //g;s/\r//g;s/\xEF\xBB\xBF//g'); echo "${file} => ${output},${version}"; psql -U postgres ${DBNAME} -c "INSERT INTO xl_localization_check (filename, en_full_v) VALUES ('${output}', '${version}') ON CONFLICT (filename) DO UPDATE SET en_full_v = excluded.en_full_v;"; done 	

# Parser Localize JP ini
cd /home/translate/localize_jp/; for file in $(ls -hl T_*.ini | awk -F" " '{print $NF}'); do output=$(echo ${file}|awk -F"_" '{print $2}'); version=$(cat ${file}|head -n 1|sed 's/ //g;s/\r//g;s/\xEF\xBB\xBF//g'); echo "${file} => ${output},${version}"; psql -U postgres ${DBNAME} -c "INSERT INTO xl_localization_check (filename, jp_full_v) VALUES ('${output}', '${version}') ON CONFLICT (filename) DO UPDATE SET jp_full_v = excluded.jp_full_v;"; done

# Parser Localize CN ini
cd /home/translate/localize_cn/; for file in $(ls -hl T_*.ini | awk -F" " '{print $NF}'); do output=$(echo ${file}|awk -F"_" '{print $2}'); version=$(cat ${file}|head -n 1|sed 's/ //g;s/\r//g;s/\xEF\xBB\xBF//g'); echo "${file} => ${output},${version}"; psql -U postgres ${DBNAME} -c "INSERT INTO xl_localization_check (filename, cn_full_v) VALUES ('${output}', '${version}') ON CONFLICT (filename) DO UPDATE SET cn_full_v = excluded.cn_full_v;"; done	

# Get the major version for all ini
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET server_major_v = SPLIT_PART(server_full_v, '|', 1), tw_major_v = SPLIT_PART(tw_full_v, '|', 1), en_major_v = SPLIT_PART(en_full_v, '|', 1), jp_major_v = SPLIT_PART(jp_full_v, '|', 1), cn_major_v = SPLIT_PART(cn_full_v, '|', 1);"

# Update ccomparision check
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET server_check = 'O';"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET tw_check = 'O' WHERE server_major_v = tw_major_v AND tw_full_v IS NOT NULL;"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET tw_check = 'X' WHERE server_major_v <> tw_major_v AND tw_full_v IS NOT NULL;"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET en_check = 'O' WHERE server_major_v = en_major_v AND en_full_v IS NOT NULL;"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET en_check = 'X' WHERE server_major_v <> en_major_v AND en_full_v IS NOT NULL;"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET jp_check = 'O' WHERE server_major_v = jp_major_v AND jp_full_v IS NOT NULL;"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET jp_check = 'X' WHERE server_major_v <> jp_major_v AND jp_full_v IS NOT NULL;"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET cn_check = 'O' WHERE server_major_v = cn_major_v AND cn_full_v IS NOT NULL;"
psql -U postgres ${DBNAME} -c "UPDATE xl_localization_check SET cn_check = 'X' WHERE server_major_v <> cn_major_v AND cn_full_v IS NOT NULL;"

# Check Info to ChatBot
$HOME/bin/check_localize_info $1
