#!/usr/bin/env bash

[ "$4" == "" ] && exec echo "Usage: $0 <absolute path for the merge_setup.ini> <postfix for the db before merge> <postfix for the db after merge> <test/live>"

MERGE_SETUP_INI=$1
POSTFIX_BEFORE=$2
POSTFIX_AFTER=$3

# 檢查 environment $4 合法性
case "$4" in
    test)
        ENVIRONMENT="測試"
        SCRIPT1="send_chatbot_text_only_test"
        SCRIPT2="send_messages_test"
        ;;
    live)
        ENVIRONMENT="正式"
        SCRIPT1="send_chatbot_text_only"
        SCRIPT2="send_messages"
        ;;
    *)
        echo "Unknown environment: $4 (only 'test' or 'live' allowed)" >&2
        exit 1
        ;;
esac

# 指向你的設定檔
SETUP_FILE=${MERGE_SETUP_INI}

source $HOME/.gamerc

# 載入解析 library
source "$HOME/bin/load_merge_db_setup"

# 讀取設定檔，建立 Account/Game/World 陣列
load_merge_db_setup "$SETUP_FILE" || exit 1

echo "=== Account DB ==="
echo "IP:       ${account[AccountDBIP]}"
echo "Name:     ${account[AccountDBName]}"
echo "User:     ${account[AccountDBUser]}"
echo "Password: ${account[AccountDBPW]}"

echo
echo "=== Game DB ==="
echo "IP:       ${game[GameDBIP]}"
echo "DB:       ${game[GameDB]}"
echo "User:     ${game[GameDBUser]}"
echo "Password: ${game[GameDBPassword]}"

#echo
#echo "=== World 列表 ==="
#echo "All WorldIDs: ${!world_ids[@]}"
#sorted_world_ids=($(printf "%s\n" "${!world_ids[@]}" | sort))
#echo "All WorldIDs: ${sorted_world_ids[*]}"

# 範例：依 WorldID 取一組 World DB 資訊
#wid=1002
#
#echo
#echo "=== World $wid ==="
#eval 'echo "WorldID:        ${world_'$wid'[WorldID]}"'
#eval 'echo "World DB IP:    ${world_'$wid'[WorldDBIP]}"'
#eval 'echo "World DB Name:  ${world_'$wid'[WorldDBName]}"'
#eval 'echo "World DB User:  ${world_'$wid'[WorldDBUser]}"'
#eval 'echo "World DB Pass:  ${world_'$wid'[WorldDBPassword]}"'

# 如果要做 target / source 分類（第一組是 target，其餘是 source）
#echo
#sorted_world_ids=($(printf "%s\n" "${!world_ids[@]}" | sort))
sorted_world_ids=("${world_order[@]}")
target_world_id="${sorted_world_ids[0]}"
target_set_id=$((target_world_id / 100))
source_world_ids=("${sorted_world_ids[@]:1}")

for wid in ${source_world_ids[*]}; do
    echo
    echo "=== World $wid ==="
    eval 'echo "WorldID:        ${world_'$wid'[WorldID]}"'
    eval 'echo "World DB IP:    ${world_'$wid'[WorldDBIP]}"'
    eval 'echo "World DB Name:  ${world_'$wid'[WorldDBName]}"'
    eval 'echo "World DB User:  ${world_'$wid'[WorldDBUser]}"'
    eval 'echo "World DB Pass:  ${world_'$wid'[WorldDBPassword]}"'
done

echo
echo "=== World 列表 ==="
echo "All WorldIDs: ${!world_ids[@]}"
echo "Target world: $target_world_id"
echo "Source worlds: ${source_world_ids[*]}"
sorted_world_ids_str=$(join_by_comma "${sorted_world_ids[@]}")
source_world_ids_str=$(join_by_comma "${source_world_ids[@]}")
echo "sorted_world_ids_str => ${sorted_world_ids_str}"
echo "source_world_ids_str => ${source_world_ids_str}"

# WorldDB > autosend_info & redenvelope_info 筆數確認 ( PRE )
TOTAL_1_autosend_pre=0; TOTAL_1_redenvelope_pre=0
echo
echo "1. WorldDB > [合併] autosend_info & redenvelope_info ( PRE )"
for world_id in ${sorted_world_ids[*]}; do
    #dbname="${WORLD_DB_NAME}${world_id}${POSTFIX_BEFORE}"
    dbname=$(get_worlddb_value "${world_id}" "WorldDBName")
    dbname="${dbname}${POSTFIX_BEFORE}"
    dbip=$(get_worlddb_value "${world_id}" "WorldDBIP")
    dbuser=$(get_worlddb_value "${world_id}" "WorldDBUser")
    dbpass=$(get_worlddb_value "${world_id}" "WorldDBPassword")
    COUNT_1_autosend_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U ${dbuser} ${dbname} -Atqc \"SELECT COUNT(*) FROM autosend_info;\"")
    COUNT_1_redenvelope_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U ${dbuser} ${dbname} -Atqc \"SELECT COUNT(*) FROM redenvelope_info;\"")
    TOTAL_1_autosend_pre=$((TOTAL_1_autosend_pre + COUNT_1_autosend_pre))
    TOTAL_1_redenvelope_pre=$((TOTAL_1_redenvelope_pre + COUNT_1_redenvelope_pre))
    echo "${dbname}, ${COUNT_1_autosend_pre}, ${TOTAL_1_autosend_pre}, ${COUNT_1_redenvelope_pre}, ${TOTAL_1_redenvelope_pre}"
done 
echo

# WorldDB > autosend_info & redenvelope_info 筆數確認 ( POST )
echo "1. WorldDB > [合併] autosend_info & redenvelope_info ( POST )"
#dbname="${WORLD_DB_NAME}${target_world_id}${POSTFIX_AFTER}"
dbname=$(get_worlddb_value "${target_world_id}" "WorldDBName")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_worlddb_value "${target_world_id}" "WorldDBIP")
dbuser=$(get_worlddb_value "${target_world_id}" "WorldDBUser")
dbpass=$(get_worlddb_value "${target_world_id}" "WorldDBPassword")
TOTAL_1_autosend_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM autosend_info;\"")
TOTAL_1_redenvelope_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM redenvelope_info;\"")
echo "${dbname}, ${TOTAL_1_autosend_post}, ${TOTAL_1_redenvelope_post}"
echo

# WorldDB > 清空 target db > activity_wheel_broadcast 跟 activity_minigame_wheel_broadcast ( POST )
echo "2. WorldDB > [清空] activity_wheel_broadcast 跟 activity_minigame_wheel_broadcast ( POST )"
#dbname="${WORLD_DB_NAME}${target_world_id}${POSTFIX_AFTER}"
dbname=$(get_worlddb_value "${target_world_id}" "WorldDBName")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_worlddb_value "${target_world_id}" "WorldDBIP")
dbuser=$(get_worlddb_value "${target_world_id}" "WorldDBUser")
dbpass=$(get_worlddb_value "${target_world_id}" "WorldDBPassword")
TOTAL_2_activity_wheel_broadcast=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM activity_wheel_broadcast;\"")
TOTAL_2_activity_minigame_wheel_broadcast=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM activity_minigame_wheel_broadcast;\"")
echo "${dbname}, ${TOTAL_2_activity_wheel_broadcast}, ${TOTAL_2_activity_minigame_wheel_broadcast}"
echo

# GameDB > chat_group (type=0 or type=2) 資料都砍掉 ( PRE )
echo "3. GameDB > [清空] chat_group (type=0 or type=2) 資料 ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT id, type, COUNT(*) FROM chat_group WHERE type IN (0, 2) AND id IN (${sorted_world_ids_str}) GROUP BY id, type ORDER BY type, id;\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT COUNT(*) FROM chat_group WHERE type IN (0, 2) AND id IN (${sorted_world_ids_str});\""
TOTAL_3_chat_group_0_2_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM chat_group WHERE type IN (0, 2) AND id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_3_chat_group_0_2_pre}"
echo

# GameDB > chat_group (type=0 or type=2) 資料都砍掉 ( POST )
echo "3. GameDB > [清空] chat_group (type=0 or type=2) 資料 ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT id, type, COUNT(*) FROM chat_group WHERE type IN (0, 2) AND id IN (${sorted_world_ids_str}) GROUP BY id, type ORDER BY type, id;\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT COUNT(*) FROM chat_group WHERE type IN (0, 2) AND id IN (${sorted_world_ids_str});\""
TOTAL_3_chat_group_0_2_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM chat_group WHERE type IN (0, 2) AND id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_3_chat_group_0_2_post}"
echo

# GameDB > player_characters::world_id ( PRE )
echo "4. GameDB > [更換] player_characters::world_id ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT world_id, COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str}) GROUP BY world_id ORDER BY world_id;\""
TOTAL_4_pcs_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${target_world_id});\"")
TOTAL_4_pcs_source_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${source_world_ids_str});\"")
TOTAL_4_pcs_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_4_pcs_target_wid_pre}, ${TOTAL_4_pcs_source_wids_pre}, ${TOTAL_4_pcs_merge_wids_pre}"
echo

# GameDB > player_characters::world_id ( POST )
echo "4. GameDB > [更換] player_characters::world_id ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT world_id, COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str}) GROUP BY world_id ORDER BY world_id;\""
TOTAL_4_pcs_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${target_world_id});\"")
TOTAL_4_pcs_source_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${source_world_ids_str});\"")
TOTAL_4_pcs_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_4_pcs_target_wid_post}, ${TOTAL_4_pcs_source_wids_post}, ${TOTAL_4_pcs_merge_wids_post}"
echo

# GameDB > guild_info::server_id ( PRE )
echo "5. GameDB > [更換] guild_info::server_id ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT server_id, COUNT(*) FROM guild_info WHERE server_id IN (${sorted_world_ids_str}) GROUP BY server_id ORDER BY server_id;\""
TOTAL_5_guild_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_info WHERE server_id IN (${target_world_id});\"")
TOTAL_5_guild_source_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_info WHERE server_id IN (${source_world_ids_str});\"")
TOTAL_5_guild_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_info WHERE server_id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_5_guild_target_wid_pre}, ${TOTAL_5_guild_source_wids_pre}, ${TOTAL_5_guild_merge_wids_pre}"
echo

# GameDB > guild_info::server_id ( POST )
echo "5. GameDB > [更換] guild_info::server_id ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT server_id, COUNT(*) FROM guild_info WHERE server_id IN (${sorted_world_ids_str}) GROUP BY server_id ORDER BY server_id;\""
TOTAL_5_guild_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_info WHERE server_id IN (${target_world_id});\"")
TOTAL_5_guild_source_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_info WHERE server_id IN (${source_world_ids_str});\"")
TOTAL_5_guild_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_info WHERE server_id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_5_guild_target_wid_post}, ${TOTAL_5_guild_source_wids_post}, ${TOTAL_5_guild_merge_wids_post}"
echo

# GameDB > player_characters::flag & 4 = 4 更名 ( PRE )
echo "6. GameDB > [全部重置] player_characters::flag & 4 = 4 更名 ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str});\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str}) AND (flag & 4 = 4);\""
TOTAL_6_pcs_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${target_world_id});\"")
TOTAL_6_pcs_source_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${source_world_ids_str});\"")
TOTAL_6_pcs_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str});\"")
TOTAL_6_pcs_flag4_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str}) AND (flag & 4 = 4);\"")
echo "${dbname}, ${TOTAL_6_pcs_target_wid_pre}, ${TOTAL_6_pcs_source_wids_pre}, ${TOTAL_6_pcs_merge_wids_pre}, ${TOTAL_6_pcs_flag4_merge_wids_pre}"
echo

# GameDB > player_characters::flag & 4 = 4 更名 ( POST )
echo "6. GameDB > [全部重置] player_characters::flag & 4 = 4 更名 ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str});\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str}) AND (flag & 4 = 4);\""
TOTAL_6_pcs_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${target_world_id});\"")
TOTAL_6_pcs_source_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${source_world_ids_str});\"")
TOTAL_6_pcs_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str});\"")
TOTAL_6_pcs_flag4_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM player_characters WHERE world_id IN (${sorted_world_ids_str}) AND (flag & 4 = 4);\"")
echo "${dbname}, ${TOTAL_6_pcs_target_wid_post}, ${TOTAL_6_pcs_source_wids_post}, ${TOTAL_6_pcs_merge_wids_post}, ${TOTAL_6_pcs_flag4_merge_wids_post}"
echo

# GameDB > guild_race_season::data->allguildrace->section->no.->world_id ( PRE )
echo "7. GameDB > [更換] guild_race_season::data->allguildrace->section->no.->world_id ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (sec ->> 'world')::int AS world_id, COUNT(*) AS cnt FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${sorted_world_ids_str}) GROUP BY world_id ORDER BY world_id;\""
TOTAL_7_guild_race_season_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${target_world_id});\"")
TOTAL_7_guild_race_season_source_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${source_world_ids_str});\"")
TOTAL_7_guild_race_season_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_7_guild_race_season_target_wid_pre}, ${TOTAL_7_guild_race_season_source_wid_pre}, ${TOTAL_7_guild_race_season_merge_wids_pre}"
echo

# GameDB > guild_race_season::data->allguildrace->section->no.->world_id ( POST )
echo "7. GameDB > [更換] guild_race_season::data->allguildrace->section->no.->world_id ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (sec ->> 'world')::int AS world_id, COUNT(*) AS cnt FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${sorted_world_ids_str}) GROUP BY world_id ORDER BY world_id;\""
TOTAL_7_guild_race_season_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${target_world_id});\"")
TOTAL_7_guild_race_season_source_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${source_world_ids_str});\"")
TOTAL_7_guild_race_season_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_season AS t CROSS JOIN LATERAL jsonb_each(t.data -> 'allguildrace') AS grp(grp_key, grp_val) CROSS JOIN LATERAL jsonb_each(grp_val -> 'section') AS sec_key(sec_id, sec) WHERE (sec ->> 'world')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_7_guild_race_season_target_wid_post}, ${TOTAL_7_guild_race_season_source_wid_post}, ${TOTAL_7_guild_race_season_merge_wids_post}"
echo

# GameDB > guild_race_data::fightrecordata->fightrecords->no.->targetworldID ( PRE )
echo "8a. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->targetworldID ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (fr ->> 'targetworldID')::int AS targetworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${sorted_world_ids_str}) GROUP BY targetworldID ORDER BY targetworldID;\""
TOTAL_8a_guild_race_data_fightrecords_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${target_world_id});\"")
TOTAL_8a_guild_race_data_fightrecords_source_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${source_world_ids_str});\"")
TOTAL_8a_guild_race_data_fightrecords_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_8a_guild_race_data_fightrecords_target_wid_pre}, ${TOTAL_8a_guild_race_data_fightrecords_source_wid_pre}, ${TOTAL_8a_guild_race_data_fightrecords_merge_wids_pre}"
echo

# GameDB > guild_race_data::fightrecordata->fightrecords->no.->targetworldID ( POST )
echo "8a. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->targetworldID ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (fr ->> 'targetworldID')::int AS targetworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${sorted_world_ids_str}) GROUP BY targetworldID ORDER BY targetworldID;\""
TOTAL_8a_guild_race_data_fightrecords_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${target_world_id});\"")
TOTAL_8a_guild_race_data_fightrecords_source_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${source_world_ids_str});\"")
TOTAL_8a_guild_race_data_fightrecords_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'targetworldID')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_8a_guild_race_data_fightrecords_target_wid_post}, ${TOTAL_8a_guild_race_data_fightrecords_source_wid_post}, ${TOTAL_8a_guild_race_data_fightrecords_merge_wids_post}"
echo

# GameDB > guild_race_data::fightrecordata->fightrecords->no.->challengerworldID ( PRE )
echo "8b. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->challengerworldID ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (fr ->> 'challengerworldID')::int AS challengerworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${sorted_world_ids_str}) GROUP BY challengerworldID ORDER BY challengerworldID;\""
TOTAL_8b_guild_race_data_fightrecords_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${target_world_id});\"")
TOTAL_8b_guild_race_data_fightrecords_source_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${source_world_ids_str});\"")
TOTAL_8b_guild_race_data_fightrecords_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_8b_guild_race_data_fightrecords_target_wid_pre}, ${TOTAL_8b_guild_race_data_fightrecords_source_wid_pre}, ${TOTAL_8b_guild_race_data_fightrecords_merge_wids_pre}"
echo

# GameDB > guild_race_data::fightrecordata->fightrecords->no.->challengerworldID ( POST )
echo "8b. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->challengerworldID ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (fr ->> 'challengerworldID')::int AS challengerworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${sorted_world_ids_str}) GROUP BY challengerworldID ORDER BY challengerworldID;\""
TOTAL_8b_guild_race_data_fightrecords_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${target_world_id});\"")
TOTAL_8b_guild_race_data_fightrecords_source_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${source_world_ids_str});\"")
TOTAL_8b_guild_race_data_fightrecords_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.fightrecordata -> 'fightrecords') AS fr_pair(rec_no, fr) WHERE (fr ->> 'challengerworldID')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_8b_guild_race_data_fightrecords_target_wid_post}, ${TOTAL_8b_guild_race_data_fightrecords_source_wid_post}, ${TOTAL_8b_guild_race_data_fightrecords_merge_wids_post}"
echo 

# GameDB > guild_race_data::defendrecordata->defendrecords->no.->targetworldID ( PRE )
echo "9a. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->targetworldID ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (dr ->> 'targetworldID')::int AS targetworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${sorted_world_ids_str}) GROUP BY targetworldID ORDER BY targetworldID;\""
TOTAL_9a_guild_race_data_defendrecords_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${target_world_id});\"")
TOTAL_9a_guild_race_data_defendrecords_source_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${source_world_ids_str});\"")
TOTAL_9a_guild_race_data_defendrecords_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_9a_guild_race_data_defendrecords_target_wid_pre}, ${TOTAL_9a_guild_race_data_defendrecords_source_wid_pre}, ${TOTAL_9a_guild_race_data_defendrecords_merge_wids_pre}"
echo

# GameDB > guild_race_data::defendrecordata->defendrecords->no.->targetworldID ( POST )
echo "9a. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->targetworldID ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (dr ->> 'targetworldID')::int AS targetworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${sorted_world_ids_str}) GROUP BY targetworldID ORDER BY targetworldID;\""
TOTAL_9a_guild_race_data_defendrecords_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${target_world_id});\"")
TOTAL_9a_guild_race_data_defendrecords_source_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${source_world_ids_str});\"")
TOTAL_9a_guild_race_data_defendrecords_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'targetworldID')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_9a_guild_race_data_defendrecords_target_wid_post}, ${TOTAL_9a_guild_race_data_defendrecords_source_wid_post}, ${TOTAL_9a_guild_race_data_defendrecords_merge_wids_post}"
echo

# GameDB > guild_race_data::defendrecordata->defendrecords->no.->challengerworldID ( PRE )
echo "9b. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->challengerworldID ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (dr ->> 'challengerworldID')::int AS challengerworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int IN (${sorted_world_ids_str}) GROUP BY challengerworldID ORDER BY challengerworldID;\""
TOTAL_9b_guild_race_data_defendrecords_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int IN (${target_world_id});\"")
TOTAL_9b_guild_race_data_defendrecords_source_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int IN (${source_world_ids_str});\"")
TOTAL_9b_guild_race_data_defendrecords_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_9b_guild_race_data_defendrecords_target_wid_pre}, ${TOTAL_9b_guild_race_data_defendrecords_source_wid_pre}, ${TOTAL_9b_guild_race_data_defendrecords_merge_wids_pre}"
echo

# GameDB > guild_race_data::defendrecordata->defendrecords->no.->challengerworldID ( POST )
echo "9b. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->challengerworldID ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT (dr ->> 'challengerworldID')::int AS challengerworldID, COUNT(*) AS cnt FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int IN (${sorted_world_ids_str}) GROUP BY challengerworldID ORDER BY challengerworldID;\""
TOTAL_9b_guild_race_data_defendrecords_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int
IN (${target_world_id});\"")
TOTAL_9b_guild_race_data_defendrecords_source_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int
IN (${source_world_ids_str});\"")
TOTAL_9b_guild_race_data_defendrecords_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_data AS t CROSS JOIN LATERAL jsonb_each(t.defendrecordata -> 'defendrecords') AS dr_pair(rec_no, dr) WHERE (dr ->> 'challengerworldID')::int
IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_9b_guild_race_data_defendrecords_target_wid_post}, ${TOTAL_9b_guild_race_data_defendrecords_source_wid_post}, ${TOTAL_9b_guild_race_data_defendrecords_merge_wids_post}"
echo  

# GameDB > guild_race_state::world_id ( PRE )
echo "10. GameDB > [更換] guild_race_state::world_id ( PRE )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_BEFORE}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT world_id, COUNT(*) FROM guild_race_state WHERE world_id IN (${sorted_world_ids_str}) GROUP BY world_id ORDER BY world_id;\""
TOTAL_10_guild_race_state_target_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_state WHERE world_id IN (${target_world_id});\"")
TOTAL_10_guild_race_state_source_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_state WHERE world_id IN (${source_world_ids_str});\"")
TOTAL_10_guild_race_state_merge_wids_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_state WHERE world_id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_10_guild_race_state_target_wid_pre}, ${TOTAL_10_guild_race_state_source_wids_pre}, ${TOTAL_10_guild_race_state_merge_wids_pre}"
echo

# GameDB > guild_race_state::world_id ( POST )
echo "10. GameDB > [更換] guild_race_state::world_id ( POST )"
#dbname="${GAME_DB_NAME}${target_set_id}${POSTFIX_AFTER}"
dbname=$(get_gamedb_value "GameDB")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_gamedb_value "GameDBIP")
dbuser=$(get_gamedb_value "GameDBUser")
dbpass=$(get_gamedb_value "GameDBPassword")
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT world_id, COUNT(*) FROM guild_race_state WHERE world_id IN (${sorted_world_ids_str}) GROUP BY world_id ORDER BY world_id;\""
TOTAL_10_guild_race_state_target_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_state WHERE world_id IN (${target_world_id});\"")
TOTAL_10_guild_race_state_source_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_state WHERE world_id IN (${source_world_ids_str});\"")
TOTAL_10_guild_race_state_merge_wids_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM guild_race_state WHERE world_id IN (${sorted_world_ids_str});\"")
echo "${dbname}, ${TOTAL_10_guild_race_state_target_wid_post}, ${TOTAL_10_guild_race_state_source_wids_post}, ${TOTAL_10_guild_race_state_merge_wids_post}"
echo

# AccountDB > related tables ( PRE )
echo "11. AccountDB > [轉換] worlds -> fake_worlds ( PRE )"
#dbname="${ACCOUNT_DB_NAME}${POSTFIX_BEFORE}"
dbname=$(get_accountdb_value "AccountDBName")
dbname="${dbname}${POSTFIX_BEFORE}"
dbip=$(get_accountdb_value "AccountDBIP")
dbuser=$(get_accountdb_value "AccountDBUser")
dbpass=$(get_accountdb_value "AccountDBPW")
START_WORLD_ID=$((${target_set_id} * 100))
END_WORLD_ID=$(((${target_set_id} + 1) * 100))
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT * FROM worlds WHERE id IN (${sorted_world_ids_str}) ORDER BY id;\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT * FROM fake_worlds WHERE id IN (${sorted_world_ids_str}) ORDER BY id;\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT * FROM fake_worlds WHERE real_world_id IN (${source_world_ids_str}) ORDER BY id;\""
COUNT_11_worlds_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM worlds WHERE id IN (${sorted_world_ids_str});\"")
COUNT_11_fake_worlds_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM fake_worlds WHERE id IN (${sorted_world_ids_str});\"")
COUNT_11_fake_worlds_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM fake_worlds WHERE id IN (${sorted_world_ids_str});\"")
COUNT_11_fake_worlds_source_wids_on_real_wid_pre=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM fake_worlds WHERE real_world_id IN (${source_world_ids_str});\"")
echo "${dbname}, ${COUNT_11_worlds_pre}, ${COUNT_11_fake_worlds_pre}, ${COUNT_11_fake_worlds_source_wids_on_real_wid_pre}"
echo

# AccountDB > related tables ( POST )
echo "11. AccountDB > [轉換] worlds -> fake_worlds ( POST )"
#dbname="${ACCOUNT_DB_NAME}${POSTFIX_POST}"
dbname=$(get_accountdb_value "AccountDBName")
dbname="${dbname}${POSTFIX_AFTER}"
dbip=$(get_accountdb_value "AccountDBIP")
dbuser=$(get_accountdb_value "AccountDBUser")
dbpass=$(get_accountdb_value "AccountDBPW")
START_WORLD_ID=$((${target_set_id} * 100))
END_WORLD_ID=$(((${target_set_id} + 1) * 100))
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT * FROM worlds WHERE id IN (${sorted_world_ids_str}) ORDER BY id;\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT * FROM fake_worlds WHERE id IN (${sorted_world_ids_str}) ORDER BY id;\""
ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -c \"SELECT * FROM fake_worlds WHERE real_world_id IN (${source_world_ids_str}) ORDER BY id;\""
COUNT_11_worlds_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM worlds WHERE id IN (${sorted_world_ids_str});\"")
COUNT_11_fake_worlds_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM fake_worlds WHERE id IN (${sorted_world_ids_str});\"")
COUNT_11_fake_worlds_source_wids_on_real_wid_post=$(ssh -o StrictHostKeyChecking=no ${dbip} "PGPASSWORD=\"${dbpass}\" psql -U postgres ${dbname} -Atqc \"SELECT COUNT(*) FROM fake_worlds WHERE real_world_id IN (${source_world_ids_str});\"")
echo "${dbname}, ${COUNT_11_worlds_post}, ${COUNT_11_fake_worlds_post}, ${COUNT_11_fake_worlds_source_wids_on_real_wid_post}"
echo

echo "#========== Final Check ==========#"
OUTPUT_FILE="/tmp/merge_result_monitor.log"
CHECK_RESULT=0

[ -f "${OUTPUT_FILE}" ] && rm -rf ${OUTPUT_FILE}

if [ "${TOTAL_1_autosend_pre}" -eq "${TOTAL_1_autosend_post}" ] && [ "${TOTAL_1_redenvelope_pre}" -eq "${TOTAL_1_redenvelope_post}" ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "1. WorldDB > [合併] autosend_info & redenvelope_info ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "1. WorldDB > [合併] autosend_info & redenvelope_info ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_2_activity_wheel_broadcast}" -eq 0 ] && [ "${TOTAL_2_activity_minigame_wheel_broadcast}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "2. WorldDB > [清空] activity_wheel_broadcast 跟 activity_minigame_wheel_broadcast ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "2. WorldDB > [清空] activity_wheel_broadcast 跟 activity_minigame_wheel_broadcast ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_3_chat_group_0_2_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "3. GameDB > [清空] chat_group (type=0 or type=2) 資料 ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "3. GameDB > [清空] chat_group (type=0 or type=2) 資料 ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_4_pcs_merge_wids_pre}" -eq "${TOTAL_4_pcs_target_wid_post}" ] && [ "${TOTAL_4_pcs_source_wids_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "4. GameDB > [更換] player_characters::world_id ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "4. GameDB > [更換] player_characters::world_id ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_5_guild_merge_wids_pre}" -eq "${TOTAL_5_guild_target_wid_post}" ] && [ "${TOTAL_5_guild_source_wids_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "5. GameDB > [更換] guild_info::server_id ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "5. GameDB > [更換] guild_info::server_id ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_6_pcs_flag4_merge_wids_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "6. GameDB > [全部重置] player_characters::flag & 4 = 4 更名 ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "6. GameDB > [全部重置] player_characters::flag & 4 = 4 更名 ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_7_guild_race_season_merge_wids_pre}" -eq "${TOTAL_7_guild_race_season_target_wid_post}" ] && [ "${TOTAL_7_guild_race_season_source_wid_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "7. GameDB > [更換] guild_race_season::data->allguildrace->section->no.->world_id ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "7. GameDB > [更換] guild_race_season::data->allguildrace->section->no.->world_id ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_8a_guild_race_data_fightrecords_merge_wids_pre}" -eq "${TOTAL_8a_guild_race_data_fightrecords_target_wid_post}" ] && [ "${TOTAL_8a_guild_race_data_fightrecords_source_wid_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "8a. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->targetworldID ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "8a. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->targetworldID ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_8b_guild_race_data_fightrecords_merge_wids_pre}" -eq "${TOTAL_8b_guild_race_data_fightrecords_target_wid_post}" ] && [ "${TOTAL_8b_guild_race_data_fightrecords_source_wid_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "8b. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->challengerworldID ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "8b. GameDB > [更換] guild_race_data::fightrecordata->fightrecords->no.->challengerworldID ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_9a_guild_race_data_defendrecords_merge_wids_pre}" -eq "${TOTAL_9a_guild_race_data_defendrecords_target_wid_post}" ] && [ "${TOTAL_9a_guild_race_data_defendrecords_source_wid_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "9a. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->targetworldID ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "9a. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->targetworldID ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_9b_guild_race_data_defendrecords_merge_wids_pre}" -eq "${TOTAL_9b_guild_race_data_defendrecords_target_wid_post}" ] && [ "${TOTAL_9b_guild_race_data_defendrecords_source_wid_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "9b. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->challengerworldID ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "9b. GameDB > [更換] guild_race_data::defendrecordata->defendrecords->no.->challengerworldID ( X )" | tee -a ${OUTPUT_FILE}
fi

if [ "${TOTAL_10_guild_race_state_merge_wids_pre}" -eq 0 ] && [ "${TOTAL_10_guild_race_state_source_wids_pre}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "10. GameDB > [無須更換] guild_race_state::world_id ( O )" | tee -a ${OUTPUT_FILE}
else
    if [ "${TOTAL_10_guild_race_state_merge_wids_post}" -eq 1 ] && [ "${TOTAL_10_guild_race_state_source_wids_post}" -eq 0 ]; then
        CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
        echo "10. GameDB > [更換] guild_race_state::world_id ( O )" | tee -a ${OUTPUT_FILE}
    else
        echo "10. GameDB > [更換] guild_race_state::world_id ( X )" | tee -a ${OUTPUT_FILE}
    fi
fi

source_world_ids_count=$(array_length "${source_world_ids[@]}")
if [ "${COUNT_11_worlds_post}" -eq 1 ] && [ "${COUNT_11_fake_worlds_post}" -eq "${source_world_ids_count}" ] && [ "${COUNT_11_fake_worlds_source_wids_on_real_wid_post}" -eq 0 ]; then
    CHECK_RESULT=$(( ${CHECK_RESULT} + 1 ))
    echo "11. AccountDB > [轉換] worlds -> fake_worlds ( O )" | tee -a ${OUTPUT_FILE}
else
    echo "11. AccountDB > [轉換] worlds -> fake_worlds ( X )" | tee -a ${OUTPUT_FILE}
fi
echo "#========== Final Check ==========#"
echo

if [ "${CHECK_RESULT}" -eq 13 ]; then
    echo "[${CHECK_RESULT} / 13] Merge Successfully! ( O )"
    #send_messages_test INFO merge_result "[${ENVIRONMENT}合併] 成功! $target_world_id <= ${source_world_ids[*]}"
    $SCRIPT1 "[${ENVIRONMENT}合併] 成功!" "$target_world_id <- ${source_world_ids[*]}"
    exit 0
else
    echo "[${CHECK_RESULT} / 13] Merge Failure! ( X )"
    $SCRIPT2 ERROR merge_result "[${ENVIRONMENT}合併] 失敗! $target_world_id <- ${source_world_ids[*]}"
    exit 2
fi
