#!/bin/bash
source $HOME/.gamerc
[ "$1" == "" ] && exec echo "Usage: $0 <TAG> <Location> ( log / receivable / receipt )"

TAG=$1
[ ! -z $TAG ] && CONDITIONAL_STATEMENT_1=" WHERE tag = '$TAG'"; CONDITIONAL_STATEMENT_2=" WHERE mail_name = '$TAG'"
LOCATION=$2

if [ -z "$LOCATION" ]; then
	echo "### xl_event_send_reward_by_system ( log ) ###"
	echo "### TAG => $TAG ###"
	CHECK_CNT=$(psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT COUNT(*) FROM xl_event_send_reward_by_system $CONDITIONAL_STATEMENT_1) TO STDOUT;")
	if [ "$CHECK_CNT" -eq 0 ]; then
		echo -e "$TAG\t0"
	else
		psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT tag, status, COUNT(*) FROM xl_event_send_reward_by_system $CONDITIONAL_STATEMENT_1 GROUP BY tag, status) TO STDOUT;"
	fi
	echo
	echo "### item_receivable ###"
	echo "### TAG => $TAG ###"
	CHECK_CNT=$(psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT COUNT(*) FROM item_receivable $CONDITIONAL_STATEMENT_2) TO STDOUT;")
	if [ "$CHECK_CNT" -eq 0 ]; then
		echo -e "$TAG\t0"
	else
		psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT mail_name, COUNT(*) FROM item_receivable $CONDITIONAL_STATEMENT_2 GROUP BY mail_name) TO STDOUT;"
	fi
	echo
	echo "### item_receipt ###"
	echo "### TAG => $TAG ###"
	#psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT '$TAG' AS mail_name, COUNT(*) FROM item_receipt t1 WHERE (t1.item_id, t1.item_quantity) IN (SELECT item_id, item_quantity FROM xl_event_send_reward_by_system WHERE tag = '$TAG' LIMIT 1)) TO STDOUT;"
	CHECK_CNT=$(psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT COUNT(*) FROM item_receipt $CONDITIONAL_STATEMENT_2) TO STDOUT;")
	if [ "$CHECK_CNT" -eq 0 ]; then
		echo -e "$TAG\t0"
	else
		psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT mail_name, COUNT(*) FROM item_receipt $CONDITIONAL_STATEMENT_2 GROUP BY mail_name) TO STDOUT;"
	fi
	echo
elif [ "$LOCATION" == "log" ]; then
	echo "### xl_event_send_reward_by_system ( log ) ###"
	echo "### TAG => $TAG ###"
	CHECK_CNT=$(psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT COUNT(*) FROM xl_event_send_reward_by_system $CONDITIONAL_STATEMENT_1) TO STDOUT;")
	if [ "$CHECK_CNT" -eq 0 ]; then
		echo -e "$TAG\t0"
	else
		psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT tag, status, COUNT(*) FROM xl_event_send_reward_by_system $CONDITIONAL_STATEMENT_1 GROUP BY tag, status) TO STDOUT;"
	fi
	echo
elif [ "$LOCATION" == "receivable" ]; then
	echo "### item_receivable ###"
	echo "### TAG => $TAG ###"
	CHECK_CNT=$(psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT COUNT(*) FROM item_receivable $CONDITIONAL_STATEMENT_2) TO STDOUT;")
	if [ "$CHECK_CNT" -eq 0 ]; then
		echo -e "$TAG\t0"
	else
		psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT mail_name, COUNT(*) FROM item_receivable $CONDITIONAL_STATEMENT_2 GROUP BY mail_name) TO STDOUT;"
	fi
	echo
elif [ "$LOCATION" == "receipt" ]; then
	echo "### item_receipt ###"
	echo "### TAG => $TAG ###"
	#psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT '$TAG' AS mail_name, COUNT(*) FROM item_receipt t1 WHERE (t1.item_id, t1.item_quantity) IN (SELECT item_id, item_quantity FROM xl_event_send_reward_by_system WHERE tag = '$TAG' LIMIT 1)) TO STDOUT;"
	CHECK_CNT=$(psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT COUNT(*) FROM item_receipt $CONDITIONAL_STATEMENT_2) TO STDOUT;")
	if [ "$CHECK_CNT" -eq 0 ]; then
		echo -e "$TAG\t0"
	else
		psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT mail_name, COUNT(*) FROM item_receipt $CONDITIONAL_STATEMENT_2 GROUP BY mail_name) TO STDOUT;"
	fi
	echo
else
	exec echo "Usage: $0 <TAG> <Location> ( log / receivable / receipt )" 
fi
