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

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

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

case "${DB_TYPE}" in
    GAMEDB|gamedb)
        for GAMEDB in $(find $HOME/server* -maxdepth 1 -type f -name "setup.ini" -exec grep "^GameDB=" {} + | cut -d= -f2); do
            if [ "$HOST_NAME" == "TEST" -o "$HOST_NAME" == "SUB" -o "$HOST_NAME" == "SUBMIT" -o "$HOST_NAME" == "DATA" -o "$HOST_NAME" == "DLC" -o "$HOST_NAME" == "TIME1" -o "$HOST_NAME" == "MEDIA" ] ; then
                SET_ID=$(echo ${GAMEDB}|sed "s/${GAME_DB_NAME}//")
                if [ "${SET_ID}" != "1" ]; then
                    cd $HOME/common-TEST/db/; echo ${GAMEDB} | colorize yellow black; perl db_game_alter :${GAMEDB}
                fi
            else
                SET_ID=$(echo ${GAMEDB}|sed "s/${GAME_DB_NAME}//")
                if [ "${SET_ID}" != "1" ]; then
                    cd $HOME/common/db/; echo ${GAMEDB} | colorize yellow black; perl db_game_alter :${GAMEDB}
                fi
            fi
        done
        ;;
    WORLDDB|worlddb)
        for WORLDDB in $(find $HOME/server*/WorldServer* -maxdepth 1 -type f -name "setup.ini" -exec grep "^WorldDBName=" {} + | cut -d= -f2); do
            if [ "$HOST_NAME" == "TEST" -o "$HOST_NAME" == "SUB" -o "$HOST_NAME" == "SUBMIT" -o "$HOST_NAME" == "DATA" -o "$HOST_NAME" == "DLC" -o "$HOST_NAME" == "TIME1" -o "$HOST_NAME" == "MEDIA" ] ; then
                cd $HOME/common-TEST/db/; echo ${WORLDDB} | colorize yellow black; perl db_world_alter :${WORLDDB}
            else
                cd $HOME/common/db/; echo ${WORLDDB} | colorize yellow black; perl db_world_alter :${WORLDDB}
            fi
        done
        ;;
    *)
        echo "ERROR: Unknown DB_TYPE"; usage
        ;;
esac
