#!/bin/bash
#================================================================================================================================
#
#          FILE: player_sql
#
#         USAGE: player_sql --find --account <Account Name 1> ... <Account Name N> [Remote Server]
#
#                player_sql --find --character <Character Name 1> ... <Character Name N> [Remote Server]
#
#                player_sql --query --account <Account Name 1> ... <Account Name N> [Remote Server]
#
#                player_sql --query --character <Character Name 1> ... <Character Name N> [Remote Server]
#
#                player_sql --update [Field Name 1] <Field Value 1> ... [Field Name N] <Field Value N> --account <Account Name 1> ... <Account Name N> [Remote Server]
#
#                player_sql --update [Field Name 1] <Field Value 1> ... [Field Name N] <Field Value N> --character <Character Name 1> ... <Character Name N> [Remote Server]
#
#                player_sql --show_table [Remote Server]
#
#                player_sql --show_version
#
#                player_sql --help
#
#   DESCRIPTION: Display the Player's Information and Modify the Privilege for the Player
#
#       OPTIONS: --find --account <Account Name 1> ... <Account Name N> [Remote Server]
#
#                --find --character <Character Name 1> ... <Character Name N> [Remote Server]
#
#                --query --account <Account Name 1> ... <Account Name N> [Remote Server]
#
#                --query --character <Character Name 1> ... <Character Name N> [Remote Server]
#
#                --update [Field Name 1] <Field Value 1> ... [Field Name N] <Field Value N> --account <Account Name 1> ... <Account Name N> [Remote Server]
#
#                --update [Field Name 1] <Field Value 1> ... [Field Name N] <Field Value N> --character <Character Name 1> ... <Character Name N> [Remote Server]
#
#                --show_table [Remote Server]
#
#                --show_version
#
#                --help
#
#  REQUIREMENTS:
#
#         NOTES:
#
#          BUGS:  ---
#        AUTHOR: vincent (Vicent Tu), xvincent.tu@xlegend.com.tw
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Wed Dec 14th 13:40:00 EDT 2011
#      REVISION: 1.5.0
#
#          TODO:
#
#================================================================================================================================

#set -o nounset                              # Treat unset variables as an error
set -m                                       # Enable job control

source ~/.gamerc
version='1.5.0'

left="("
squote="'"
space=" "
comma=","
right=")"

MEMBER_DB=$MEMBER_DB_NAME
GAME_DB=$GAME_DB_NAME

show_table() {
        TABLE='player_characters'
        HOST=$2
        if [ "$HOST" == "" -o "$HOST" == "gamedb?" ]; then
                GAME_DB+=1
                exec echo "\\d+ $TABLE" | psql -d $GAME_DB
        elif [[ $HOST == gamedb[1-9] ]]; then
                GAME_DB_NUM=$(echo $HOST | sed -e 's/gamedb//')
                GAME_DB+=$GAME_DB_NUM
                export PGPASSWORD="$LIVE_SERVER_DB_PASSWORD"
                exec echo "\\d+ $TABLE" | psql -h "$HOST" $GAME_DB
        fi
}

show_usage() {
        echo ""
        echo "Usage:"
        echo "  --find --account <Account Name 1> ... <Account Name N> [Remote Server]"
        echo "  --find --character <Character Name 1> ... <Character Name N> [Remote Server]"
        echo "  --query --account <Account Name 1> ... <Account Name N> [Remote Server]"
        echo "  --query --character <Character Name 1> ... <Character Name N> [Remote Server]"
        echo "  --update [Field Name 1] <Field Value 1> ... [Field Name N] <Field Value N> --account <Account Name 1> ... <Account Name N> [Remote Server]"
        echo "  --update [Field Name 1] <Field Value 1> ... [Field Name N] <Field Value N> --character <Character Name 1> ... <Character Name N> [Remote Server]"
        echo "  --show_table [Remote Server]"
        echo "  --version"
        exec echo "     --help"
}

show_help() {
        echo "player_sql is a script which can display and update the player information"
        echo ""
        echo "Usage:"
        echo "  $script [OPTIONS]... "
        echo ""
        echo "General options:"
        echo "  -f -a, --find --account                 find the account exist in the tb_user table or not"
        echo "  -f -c, --find --character               find the character exist in the player characters table or not"
        echo "  -q -a, --query --account                query the player information for account in the player characters table"
        echo "  -q -c, --query --character              query the player information for character in the player characters table"
        echo "  -u ... -a, --update ... --account       update the player information for account in the player characters table"
        echo "  -u ... -c, --update ... --character     update the player information for character in the player characters table"
        echo "  -s, --show_table                        show the field information in the player characters table"
        echo "  -h, --help                              show this help, then exit"
        echo "  -v, --version                           output version information, then exit"
        echo ""
        echo "EXAMPLES"
        echo "  player_sql -f -a xlegend1 xlegend2"
        echo "  Find the accounts \"xlegend1\" and \"xlegend2\" exist in the \"Test Server\" or not"
        echo ""
        echo "  player_sql --find --character gd fn gf js gamedbX"
        echo "  Find the characters \"gd\", \"fn\", \"gf\" and \"js\" exist in the \"Live Server (gamedbX)\" or not"
        echo ""
        echo "  player_sql -q -a xlegend1 xlegend2"
        echo "  Query the player information for accounts \"xlegend1\" and \"xlegend2\" in the \"Test Server\""
        echo ""
        echo "  player_sql --query --character gd fn gf js gamedbX"
        echo "  Query the player information for characters \"gd\", \"fn\", \"gf\" and \"js\" in the \"Live Server (gamedbX)\""
        echo ""
        echo "  player_sql --update privilege 5 level 65 --account xlegend1 xlegend2"
        echo "  Update the player information for accounts \"xlegend1\" and \"xlegend2\" in the \"Test Server\""
        echo "  And show the player information for accounts you changed in the \"Test Server\""
        echo ""
        echo "  player_sql -u privilege 5 level 65 -c gd fn gf js gamedbX"
        echo "  Update the player information for characters \"gd\", \"fn\", \"gf\" and \"js\" in the \"Live Server (gamedbX)\""
        echo "  And show the player information for characters you changed in the \"Live Server (gamedbX)\""
        echo ""
        echo "  player_sql -s gamedbX"
        echo "  Show the field information in player characters table in the \"Live Server (gamedbX)\""
        echo ""
        echo "  player_sql --help"
        echo "  Show this help, then exit"
        echo ""
        echo "  player_sql -v"
        exec echo "     Output version information, then exit"
}

show_version() {
        echo "player_sql        $version"
        exec echo "a script which can display and update the player information"
}

# Convert parameters to an array
declare -i i=0

OPERAND="IN"
for p in $@
do
        ((i++))
        param_value[$i]=$p
        echo "${param_value[$i]}" | egrep "%|_"
        if [ "$?" -eq 0 ]; then
                OPERAND="LIKE"
        fi
done

case ${param_value[1]} in
        "")
            show_usage
            ;;
        -f|--find|-q|--query)
            first=3
            last=3
            ;;
        -u|--update)
            ;;
        -s|--show_table)
            show_table
            exit
            ;;
        -h|--help)
            show_help
            ;;
        -v|--version)
            show_version
            ;;
        *)
            show_usage
esac

if [ "${param_value[1]}" == "-u" -o "${param_value[1]}" == "--update" ]; then

        param_num=1
        pos=0

        for param in ${param_value[@]}
        do
                if [ "$param" == "-a" -o "$param" == "--account" ]; then
                        tag=$param_num
                        pos=$param_num
                        param_type=$param
                        first=$((++param_num))
                        last=$first
                        break
                elif [ "$param" == "-c" -o "$param" == "--character" ]; then
                        tag=$param_num
                        pos=$param_num
                        param_type=$param
                        first=$((++param_num))
                        last=$first
                        break
                fi
                let param_num++
        done

        field_num=$(((first-2)/2))
        field_start=2
        field_end=3

        for ((i=1;i<=$field_num;i++))
        do
                field+=$(echo ${param_value[$field_start]} = ${param_value[$field_end]})
                if [ "$i" -ne "$field_num" ]; then
                        field+=', '
                fi
                field_start=$(expr $field_start + 2)
                field_end=$(expr $field_end + 2)
        done
fi

for ((i=$first;i<=$#;i++))
do
        if [ "$i" -eq $last -a "$i" -ne "$#" ]; then
                final+=$left
                final+=$squote
                final+=${param_value[$i]}
                final+=$squote
                final+=$comma
                final+=$space
        elif [ "$i" -eq $last -a "$i" -eq "$#" ]; then
                final+=$left
                final+=$squote
                final+=${param_value[$i]}
                final+=$squote
                final+=$right
        elif [ "$i" -eq "$#" ]; then
                final+=$squote
                final+=${param_value[$i]}
                final+=$squote
                final+=$right
        else
                final+=$squote
                final+=${param_value[$i]}
                final+=$squote
                final+=$comma
                final+=$space
        fi
done

if [[ ${param_value[$#]} == gamedb[1-9] ]]; then
        final=${final/, \'gamedb[1-9]\'/}
        echo ${final[@]}
        GAME_DB_NUM=$(echo ${param_value[$#]} | sed -e 's/gamedb//')
        GAME_DB+=$GAME_DB_NUM
        export PGPASSWORD="$LIVE_SERVER_DB_PASSWORD"
        declare -i num=0
        if [ "${param_value[1]}" == "-q" -o "${param_value[1]}" == "--query" ]; then

                if [ "${param_value[2]}" == "-a" -o "${param_value[2]}" == "--account" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -h accountdb -d $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_account[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}-1; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_account[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_account[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Accounts: $wrong_output"
                                fi
                        fi

                        check_account=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -h accountdb $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | wc -l)
                        if [ "$check_account" -eq 0 ]; then
                                echo "Error: Accounts not found in the database of the Live Server!"
                        else
                                check_character=$(echo "select given_name from player_characters where account_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB | grep "0 rows")
                                if [ "$check_character" == "(0 rows)" ]; then
                                        echo "Error: Accounts exist, but there is no any Characters found in the database of the Live Server! (${param_value[$#]})"
                                else
                                        echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where account_name $OPERAND ${final[@]} order by account_name;" | psql -h ${param_value[$#]} $GAME_DB
                                fi

                        fi

                elif [ "${param_value[2]}" == "-c" -o "${param_value[2]}" == "--character" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_character[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}-1; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_character[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_character[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Characters: $wrong_output"
                                fi
                        fi

                        check_character=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | wc -l)
                        if [ "$check_character" -eq 0 ]; then
                                echo "Error: Characters not found in the database of the Live Server! (${param_value[$#]})"
                        else
                                echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB
                        fi

                fi

        elif [ "${param_value[1]}" == "-u" -o "${param_value[1]}" == "--update" ]; then

                if [ "$param_type" == "-a" -o "$param_type" == "--account" ]; then
                        echo "update player_characters set $field WHERE account_name $OPERAND ${final[@]};" | psql -h ${param_value[$#]} $GAME_DB
                        echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where account_name $OPERAND ${final[@]} order by account_name;" | psql -h ${param_value[$#]} $GAME_DB
                elif [ "$param_type" == "-c" -o "$param_type" == "--character" ]; then
                        echo "update player_characters set $field WHERE given_name $OPERAND ${final[@]};" | psql -h ${param_value[$#]} $GAME_DB
                        echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB
                fi

        elif [ "${param_value[1]}" == "-f" -o "${param_value[1]}" == "--find" ]; then

                if [ "${param_value[2]}" == "-a" -o "${param_value[2]}" == "--account" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -h accountdb -d $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_account[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}-1; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_account[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_account[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Accounts: $wrong_output"
                                fi
                        fi

                        check_account=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -h accountdb $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | wc -l)
                        if [ "$check_account" -eq 0 ]; then
                                echo "Error: Accounts not found in the database of the Live Server!"
                        else
                                account_content=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -h accountdb $MEMBER_DB | egrep -v "mid|-|row" | sed -e s'/^ //' | sed '$d' | sed -e s"/^/\'/" | sed -e s"/$/\'/" | tr -s "\n" "," | sed -e 's/,$//' | sed -e 's/,/, /g' | sed -e 's/^/(/' | sed -e 's/$/)/')
                                echo "$account_content found in the database of the Live Server."
                        fi

                elif [ "${param_value[2]}" == "-c" -o "${param_value[2]}" == "--character" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_character[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}-1; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_character[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_character[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Characters: $wrong_output"
                                fi
                        fi

                        check_character=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | wc -l)

                        if [ "$check_character" -eq 0 ]; then
                                echo "Error: Characters not found in the database of the Live Server! (${param_value[$#]})"
                        else
                                character_content=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h ${param_value[$#]} $GAME_DB | egrep -v "given_name|-|row" | sed -e s'/^ //' | sed '$d' | sed -e s"/^/\'/" | sed -e s"/$/\'/" | tr -s "\n" "," | sed -e 's/,$//' | sed -e 's/,/, /g' | sed -e 's/^/(/' | sed -e 's/$/)/')
                                echo "$character_content found in the database of the Live Server (${param_value[$#]})."
                        fi
                fi

        fi

elif [ ${param_value[$#]} == "gamedb?" ]; then
        final=${final/, \'gamedb?\'/}
        echo ${final[@]}
        GAME_DB_LIST=$(cat /etc/hosts | grep -i gamedb[1-9] | tr '[A-Z]' '[a-z]' | awk -F" " '{print $2}')
        export PGPASSWORD="$LIVE_SERVER_DB_PASSWORD"
        if [ "${param_value[1]}" == "-f" -o "${param_value[1]}" == "--find" ]; then

                if [ "${param_value[2]}" == "-a" -o "${param_value[2]}" == "--account" ]; then
                                check_account=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -h accountdb -d $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | wc -l)
                        if [ "$check_account" -eq 0 ]; then
                                echo "Error: Accounts not found in the database of the Live Server!"
                        else
                                account_content=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -h accountdb -d $MEMBER_DB | egrep -v "mid|-|row" | sed -e s'/^ //' | sed '$d' | sed -e s"/^/\'/" | sed -e s"/$/\'/" | tr -s "\n" "," | sed -e 's/,$//' | sed -e 's/,/, /g' | sed -e 's/^/(/' | sed -e 's/$/)/')
                                echo "$account_content found in the database of the Live Server."
                        fi

                elif [ "${param_value[2]}" == "-c" -o "${param_value[2]}" == "--character" ]; then
                        for gamedb_num in $(echo $GAME_DB_LIST)
                        do
                                GAME_DB=$GAME_DB_NAME
                                GAME_DB_NUM=$(echo $gamedb_num | sed -e 's/gamedb//')
                                GAME_DB+=$GAME_DB_NUM
                                check_character=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h $gamedb_num -d $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | wc -l)
                                if [ "$check_character" -eq 0 ]; then
                                        echo "Error: Characters not found in the database of the Live Server! ($gamedb_num)"
                                else
                                        character_content=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -h $gamedb_num -d $GAME_DB | egrep -v "given_name|-|row" | sed -e s'/^ //' | sed '$d' | sed -e s"/^/\'/" | sed -e s"/$/\'/" | tr -s "\n" "," | sed -e 's/,$//' | sed -e 's/,/, /g' | sed -e 's/^/(/' | sed -e 's/$/)/')
                                        echo "$character_content found in the database of the Live Server ($gamedb_num)."
                                fi
                        done

                fi
        fi

else
        echo ${final[@]}
        GAME_DB+=1
        export PGPASSWORD="$TEST_SERVER_DB_PASSWORD"
        declare -i num=0
        if [ "${param_value[1]}" == "-q" -o "${param_value[1]}" == "--query" ]; then

                if [ "${param_value[2]}" == "-a" -o "${param_value[2]}" == "--account" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -d $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_account[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_account[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_account[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Accounts: $wrong_output"
                                fi
                        fi

                        check_account=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -d $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | wc -l)

                        if [ "$check_account" -eq 0 ]; then
                                echo "Error: Accounts not found in the database of the Test Server!"
                        else
                                check_character=$(echo "select given_name from player_characters where account_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB | grep "0 rows")
                                if [ "$check_character" == "(0 rows)" ]; then
                                        echo "Error: Accounts exist, but there is no any Characters found in the database of the Test Server!"
                                else
                                        echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where account_name $OPERAND ${final[@]} order by account_name;" | psql -d $GAME_DB
                                fi

                        fi

                elif [ "${param_value[2]}" == "-c" -o "${param_value[2]}" == "--character" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_character[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_character[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_character[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Characters: $wrong_output"
                                fi
                        fi

                        check_character=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | wc -l)

                        if [ "$check_character" -eq 0 ]; then
                                echo "Error: Characters not found in the database of the Test Server!"
                        else
                                echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB
                        fi

                fi

        elif [ "${param_value[1]}" == "-u" -o "${param_value[1]}" == "--update" ]; then

                if [ "$param_type" == "-a" -o "$param_type" == "--account" ]; then
                        echo "update player_characters set $field WHERE account_name $OPERAND ${final[@]};" | psql -d $GAME_DB
                        echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where account_name $OPERAND ${final[@]} order by account_name;" | psql -d $GAME_DB
                elif [ "$param_type" == "-c" -o "$param_type" == "--character" ]; then
                        echo "update player_characters set $field WHERE given_name $OPERAND ${final[@]};" | psql -d $GAME_DB
                        echo "select account_id, account_name, given_name, family_id, node_id, x, y, z, flags, level, gold, privilege from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB
                fi

        elif [ "${param_value[1]}" == "-f" -o "${param_value[1]}" == "--find" ]; then

                if [ "${param_value[2]}" == "-a" -o "${param_value[2]}" == "--account" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -d $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_account[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_account[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_account[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Accounts: $wrong_output"
                                fi
                        fi

                        check_account=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -d $MEMBER_DB | egrep -v "mid|-|row" | sed '/^$/d' | wc -l)

                        if [ "$check_account" -eq 0 ]; then
                                echo "Error: Accounts not found in the database of the Test Server!"
                        else
                                account_content=$(echo "select mid from tb_user where mid $OPERAND ${final[@]} order by mid;" | psql -d $MEMBER_DB | egrep -v "mid|-|row" | sed -e s'/^ //' | sed '$d' | sed -e s"/^/\'/" | sed -e s"/$/\'/" | tr -s "\n" "," | sed -e 's/,$//' | sed -e 's/,/, /g' | sed -e 's/^/(/' | sed -e 's/$/)/')
                                echo "$account_content found in the database of the Test Server."
                        fi

                elif [ "${param_value[2]}" == "-c" -o "${param_value[2]}" == "--character" ]; then

                        if [ "$OPERAND" == "IN" ]; then
                                check=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | sed -e 's/^ //')

                                for q in $(echo $check)
                                do
                                        ((num++))
                                        check_character[$num]=$q
                                done

                                for((m=$first; m<=${#param_value[@]}; m++))
                                do
                                        skip=
                                        for((n=1; n<=${#check_character[@]}; n++))
                                        do
                                                [[ ${param_value[$m]} == ${check_character[$n]} ]] && { skip=1; break; }
                                        done
                                        [[ ! -n $skip ]] && wrong+="${param_value[$m]} "
                                done

                                if [[ -n $wrong ]]; then
                                        wrong_output=$(echo $wrong | sed -e 's/ $//' | sed -e "s/^/'/" | sed -e "s/$/'/" | sed -e 's/^/(/' | sed -e 's/$/)/' | sed -e "s/ /', '/g")
                                        echo "Wrong Characters: $wrong_output"
                                fi
                        fi

                        check_character=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB | egrep -v "given_name|-|row" | sed '/^$/d' | wc -l)

                        if [ "$check_character" -eq 0 ]; then
                                echo "Error: Characters not found in the database of the Test Server!"
                        else
                                character_content=$(echo "select given_name from player_characters where given_name $OPERAND ${final[@]} order by given_name;" | psql -d $GAME_DB | egrep -v "given_name|-|row" | sed -e s'/^ //' | sed '$d' | sed -e s"/^/\'/" | sed -e s"/$/\'/" | tr -s "\n" "," | sed -e 's/,$//' | sed -e 's/,/, /g' | sed -e 's/^/(/' | sed -e 's/$/)/')
                                echo "$character_content found in the database of the Test Server."
                        fi

                fi
        fi

fi
