#!/bin/bash
source $HOME/.gamerc
[ "$1" == "" ] && exec echo "Usage: $0 <GAMEDB_NAME>"

DBNAME=$1

ITEM_ID_SQL_PARAMS=""
for item_id in ${ITEM_ID_LISTS}; do
    if [[ $item_id == *"-"* ]]; then
        id_start=$(echo $item_id | awk -F"-" '{print $1}')
        id_end=$(echo $item_id | awk -F"-" '{print $2}')
        for item_id_c in $(seq ${id_start} ${id_end}); do
            ITEM_ID_SQL_PARAMS="${ITEM_ID_SQL_PARAMS},${item_id_c}"
        done
    else
        ITEM_ID_SQL_PARAMS="${ITEM_ID_SQL_PARAMS},${item_id}"
    fi
    ITEM_ID_SQL_PARAMS=$(echo ${ITEM_ID_SQL_PARAMS}|sed "s/^,//")
done

#ITEMID_RELATED_TABLES="account_elfcase account_mailitem account_storage account_weaponcase auction collection collection_in_game elf_container elfbank_container elfhouse_container equipment family_storage inventory mailitem player_room_container storage"
TABLE_LISTS=${ITEMID_RELATED_TABLES}

echo ${DBNAME} | colorize red green
for table in ${TABLE_LISTS}; do
    echo ${table} | colorize yellow black
    if [[ ${table} == *"inventory"* || ${table} == *"equipment"* || ${table} == *"storage"* || ${table} == *"mailitem"* ]]; then
        psql -U postgres ${DBNAME} -c "SELECT player_id, item_id, COUNT(durability), '${table}'::TEXT AS table_name FROM ${table} WHERE (item_id=54110 and (combo_id = 7046 OR combo_id = 7047)) OR (item_id=55111 and (combo_id = 7046 OR combo_id = 7047)) GROUP BY item_id ORDER BY item_id;"

    elif [[ "${table}" == "auction" ]]; then
        psql -U postgres ${DBNAME} -c "SELECT item_id, COUNT(*), '${table}'::TEXT AS table_name FROM ${table} WHERE (item_id=54110 and (combo_id = 7046 OR combo_id = 7047)) OR (item_id=55111 and (combo_id = 7046 OR combo_id = 7047)) GROUP BY item_id ORDER BY item_id;"

    elif [[ "${table}" == "collection" || "${table}" == "account_storage" ]]; then
        psql -U postgres ${DBNAME} -c "SELECT item_id, COUNT(*), '${table}'::TEXT AS table_name FROM ${table} WHERE (item_id=54110 and (combo_id = 7046 OR combo_id = 7047)) OR (item_id=55111 and (combo_id = 7046 OR combo_id = 7047)) GROUP BY item_id ORDER BY item_id;"

    elif [[ "${table}" == "family_storage" ]]; then
        psql -U postgres ${DBNAME} -c "SELECT item_id, COUNT(*), '${table}'::TEXT AS table_name FROM ${table} WHERE (item_id=54110 and (combo_id = 7046 OR combo_id = 7047)) OR (item_id=55111 and (combo_id = 7046 OR combo_id = 7047)) GROUP BY item_id ORDER BY item_id;"

    elif [[ ${table} == *"elf"* ]]; then
        psql -U postgres ${DBNAME} -c "SELECT item1, COUNT(*), '${table}'::TEXT AS table_name FROM ${table} WHERE (item1=54110 and (combo1 = 7046 OR combo1 = 7047)) OR (item1=55111 and (combo1 = 7046 OR combo1 = 7047)) GROUP BY item1 ORDER BY item1;"
        psql -U postgres ${DBNAME} -c "SELECT item2, COUNT(*), '${table}'::TEXT AS table_name FROM ${table} WHERE (item2=54110 and (combo2 = 7046 OR combo2 = 7047)) OR (item2=55111 and (combo2 = 7046 OR combo2 = 7047)) GROUP BY item2 ORDER BY item2;"
        psql -U postgres ${DBNAME} -c "SELECT item3, COUNT(*), '${table}'::TEXT AS table_name FROM ${table} WHERE (item3=54110 and (combo3 = 7046 OR combo3 = 7047)) OR (item3=55111 and (combo3 = 7046 OR combo3 = 7047)) GROUP BY item3 ORDER BY item3;"

    fi
done
