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

usage() {
    exec echo "Usage: $0 <DB_TYPE: ACCOUNTDB / GAMEDB / WORLDDB / SOCIETYDB>"
}

# 參數檢查
[[ $# -lt 1 ]] && usage
DB_TYPE="$1"

case "${DB_TYPE}" in
    ACCOUNTDB)
        TEST_VER=$(ssh TEST "psql -U postgres ${ACCOUNT_DB_NAME} -Atqc \"SELECT schema_version FROM configuration;\"")
        LIVE_VER=$(ssh ACCOUNTDB "psql -U postgres ${ACCOUNT_DB_NAME} -Atqc \"SELECT schema_version FROM configuration;\"")
        [ "${TEST_VER}" == "${LIVE_VER}" ] && RESULT="(O)" || RESULT="(X)"
echo "$(cat <<EOF
====================
// ACCOUNTDB
TEST: ${ACCOUNT_DB_NAME}
RESULT: ${RESULT}

TEST: ${TEST_VER}
LIVE: ${LIVE_VER}
====================
EOF
)"
echo
        ;;
    GAMEDB|gamedb)
        TEST_VER=$(ssh TEST "psql -U postgres ${GAME_DB_NAME}10 -Atqc \"SELECT schema_version FROM configuration;\"")
        SET_ID_LISTS=$(ssh CTRL "psql -U postgres WebTool -Atqc \"SELECT DISTINCT set_id FROM db_setup WHERE is_commercial = 1 ORDER BY set_id;\"")
        OUTPUT_GAMEDB_SCHEMA_VERSION="/tmp/n1_gamedb_schema_version"
        [ -f "${OUTPUT_GAMEDB_SCHEMA_VERSION}" ] && rm -rf ${OUTPUT_GAMEDB_SCHEMA_VERSION}
        for set_id in ${SET_ID_LISTS}; do
            LIVE_VER=$(ssh GAMEDB${set_id} "psql -U postgres ${GAME_DB_NAME}${set_id} -Atqc \"SELECT schema_version FROM configuration;\"")
            #echo "${GAME_DB_NAME}${set_id}: ${LIVE_VER}" >> ${OUTPUT_GAMEDB_SCHEMA_VERSION}
            if [ -z "${RESULT}" ]; then
                if [ "${TEST_VER}" == "${LIVE_VER}" ]; then 
                    RESULT="(O)"
                else
                    RESULT="(X)"
                    echo "${GAME_DB_NAME}${set_id}: ${LIVE_VER}" >> ${OUTPUT_GAMEDB_SCHEMA_VERSION}
                fi
            else
                if [ "${TEST_VER}" != "${LIVE_VER}" ] && [ "${RESULT}" == "(O)" ]; then
                    RESULT="(X)"
                    echo "${GAME_DB_NAME}${set_id}: ${LIVE_VER}" >> ${OUTPUT_GAMEDB_SCHEMA_VERSION}
                fi
            fi
        done
if [ "${RESULT}" == "(X)" ]; then
echo "$(cat <<EOF
====================
// GAMEDB
TEST: ${GAME_DB_NAME}10
RESULT: ${RESULT}

TEST: ${TEST_VER}
EOF
)"
cat ${OUTPUT_GAMEDB_SCHEMA_VERSION}
echo "$(cat <<EOF
====================
EOF
)"
else
echo "$(cat <<EOF
====================
// GAMEDB
TEST: ${GAME_DB_NAME}10
RESULT: ${RESULT}

TEST: ${TEST_VER}
LIVE: ${LIVE_VER}
====================
EOF
)"
fi
echo
        ;;
    WORLDDB|worlddb)
        TEST_VER=$(ssh TEST "psql -U postgres ${WORLD_DB_NAME}1001 -Atqc \"SELECT schema_version FROM configuration;\"")
        WORLD_ID_LISTS=$(ssh CTRL "psql -U postgres WebTool -Atqc \"SELECT world_id FROM db_setup WHERE is_commercial = 1 AND world_id NOT IN (SELECT id FROM fake_worlds) ORDER BY world_id;\"")
        OUTPUT_WORLDDB_SCHEMA_VERSION="/tmp/n1_worlddb_schema_version"
        [ -f "${OUTPUT_WORLDDB_SCHEMA_VERSION}" ] && rm -rf ${OUTPUT_WORLDDB_SCHEMA_VERSION}
        for world_id in ${WORLD_ID_LISTS}; do
            LIVE_VER=$(ssh WORLDDB${world_id} "psql -U postgres ${WORLD_DB_NAME}${world_id} -Atqc \"SELECT schema_version FROM configuration;\"")
            echo "${WORLD_DB_NAME}${world_id}: ${LIVE_VER}" >> ${OUTPUT_WORLDDB_SCHEMA_VERSION}
            if [ -z "${RESULT}" ]; then
                [ "${TEST_VER}" == "${LIVE_VER}" ] && RESULT="(O)" || RESULT="(X)"
            else
                if [ "${TEST_VER}" != "${LIVE_VER}" ] && [ "${RESULT}" == "(O)" ]; then
                    RESULT="(X)"
                fi
            fi
        done
if [ "${RESULT}" == "(X)" ]; then
echo "$(cat <<EOF
====================
// WORLDDB
TEST: ${WORLD_DB_NAME}1001
RESULT: ${RESULT}

TEST: ${TEST_VER}
EOF
)"
cat ${OUTPUT_WORLDDB_SCHEMA_VERSION}
echo "$(cat <<EOF
====================
EOF
)"
else
echo "$(cat <<EOF
====================
// WORLDDB
TEST: ${WORLD_DB_NAME}1001
RESULT: ${RESULT}

TEST: ${TEST_VER}
LIVE: ${LIVE_VER}
====================
EOF
)"
fi
echo
        ;;
    SOCIETYDB|societydb)
        echo "ERROR: Useless now for ${DB_TYPE}";
        ;;
    *)
        echo "ERROR: Unknown DB_TYPE for ${DB_TYPE}"; usage
        ;;
esac
