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

[ "$1" == "" ] && exec echo "Usage: $0 <Environment (TEST => 0, LIVE => 1)>"

env=$1
output_filename="/tmp/growthfund_vs_powerupitem_item_id_monitor.log"

COUNT1=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id1 AS item_id FROM growthfund WHERE reward_item_id1 <> 0 AND reward_item_id1 NOT IN (SELECT item_id FROM powerupitem) ORDER BY reward_item_id1) SELECT COUNT(item_id) FROM t1) TO STDOUT;")

COUNT2=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id2 AS item_id FROM growthfund WHERE reward_item_id2 <> 0 AND reward_item_id2 NOT IN (SELECT item_id FROM powerupitem) ORDER BY reward_item_id2) SELECT COUNT(item_id) FROM t1) TO STDOUT;")

COUNT3=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id3 AS item_id FROM growthfund WHERE reward_item_id3 <> 0 AND reward_item_id3 NOT IN (SELECT item_id FROM powerupitem) ORDER BY reward_item_id3) SELECT COUNT(item_id) FROM t1) TO STDOUT;")

COUNT4=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id4 AS item_id FROM growthfund WHERE reward_item_id4 <> 0 AND reward_item_id4 NOT IN (SELECT item_id FROM powerupitem) ORDER BY reward_item_id4) SELECT COUNT(item_id) FROM t1) TO STDOUT;")

COUNT5=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id5 AS item_id FROM growthfund WHERE reward_item_id5 <> 0 AND reward_item_id5 NOT IN (SELECT item_id FROM powerupitem) ORDER BY reward_item_id5) SELECT COUNT(item_id) FROM t1) TO STDOUT;")

if [ "${COUNT1}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id1 AS item_id FROM growthfund WHERE reward_item_id1 <> 0 AND reward_item_id1 NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY reward_item_id1) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR growthfund_vs_powerupitem_item_id "${ENV_TAG} 成長基金表的獎勵道具ID 1 設定版本內未包含的道具ID品項"
fi

if [ "${COUNT2}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id2 AS item_id FROM growthfund WHERE reward_item_id2 <> 0 AND reward_item_id2 NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY reward_item_id2) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR growthfund_vs_powerupitem_item_id "${ENV_TAG} 成長基金表的獎勵道具ID 2 設定版本內未包含的道具ID品項"
fi

if [ "${COUNT3}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id3 AS item_id FROM growthfund WHERE reward_item_id3 <> 0 AND reward_item_id3 NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY reward_item_id3) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR growthfund_vs_powerupitem_item_id "${ENV_TAG} 成長基金表的獎勵道具ID 3 設定版本內未包含的道具ID品項"
fi

if [ "${COUNT4}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id4 AS item_id FROM growthfund WHERE reward_item_id4 <> 0 AND reward_item_id4 NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY reward_item_id4) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR growthfund_vs_powerupitem_item_id "${ENV_TAG} 成長基金表的獎勵道具ID 4 設定版本內未包含的道具ID品項"
fi

if [ "${COUNT5}" -ne 0 ]; then
    OUTPUT=$(psql -U postgres ${ACCOUNT_DB_NAME} -c "COPY (WITH t1 AS (SELECT DISTINCT reward_item_id5 AS item_id FROM growthfund WHERE reward_item_id5 <> 0 AND reward_item_id5 NOT IN (SELECT item_id FROM powerupitem)) SELECT item_id FROM t1 ORDER BY reward_item_id5) TO '${output_filename}' CSV HEADER")

    if [ "${env}" == "0" ]; then
        ENV_TAG="(測試)"
    elif [ "${env}" == "1" ]; then
        ENV_TAG="(正式)"
    fi
    send_messages ERROR growthfund_vs_powerupitem_item_id "${ENV_TAG} 成長基金表的獎勵道具ID 5 設定版本內未包含的道具ID品項"
fi

