#!/bin/bash
source $HOME/.gamerc
[ "$1" == "" ] && exec echo "Usage: $0 <Filename> ( account_name, item_id, item_quantity, tag )"

FILENAME=$1
[ ! -f $FILENAME ] && exec echo "File ( $FILENAME ) is not exist, please check again!"

datetime_now () {
	NOW=$(date +"%Y%m%dT%H%M%S")
	echo $NOW
}

check_item_id () {
	echo "### Check Item ID Begin ###"
	datetime_now
	for item_id in $(cat $FILENAME | awk -F"," '{print $2}' | sort | uniq); 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
			SERVER_INI_PATH="$HOME/common-TEST/Data/db"
		else
			SERVER_INI_PATH="$HOME/common/Data/db"
		fi

		CHECK_ITEM_ID=$(cat $SERVER_INI_PATH/S_Item.ini $SERVER_INI_PATH/S_ItemMall.ini | egrep "^${item_id}\|" | wc -l)
		if [ "$CHECK_ITEM_ID" -eq 0 ]; then
			nl $FILENAME | awk -F"," "{if(\$2 == \"$item_id\") {print \$0}}" | sed 's/^[ ]*//g;s/^[\t]*//'
			exec echo "Item ID ( ${item_id} ) is not exist in Data/db/S_Item.ini or Data/db/S_ItemMall.ini, please check again!"
		fi
	done
	echo "### Check Item ID End ###"
	echo
}

check_account_name () {
	echo "### Check Account Name Begin ###"
	datetime_now
	ACCOUNT_LIST_TABLE="xl_event_account_name_list"
	ACCOUNT_LIST_FILE="/tmp/${ACCOUNT_LIST_TABLE}.csv"
	psql -U postgres $ACCOUNT_DB_NAME -c "DROP TABLE IF EXISTS $ACCOUNT_LIST_TABLE;"
	cat $FILENAME | awk -F"," '{print $1}' | sort | uniq > $ACCOUNT_LIST_FILE
	psql -U postgres $ACCOUNT_DB_NAME -c "CREATE TABLE $ACCOUNT_LIST_TABLE (account_name TEXT primary key);"
	psql -U postgres $ACCOUNT_DB_NAME -c "COPY $ACCOUNT_LIST_TABLE FROM '$ACCOUNT_LIST_FILE' CSV;"
	CHECK_ACCOUNT_NAME=$(psql -U postgres $ACCOUNT_DB_NAME -c "COPY (SELECT account_name FROM $ACCOUNT_LIST_TABLE WHERE account_name NOT IN (SELECT username FROM accounts ORDER BY username)) TO STDOUT;")
	if [ ! -z "$CHECK_ACCOUNT_NAME"	]; then
		for account_name in $CHECK_ACCOUNT_NAME; do
			nl $FILENAME | awk "/$account_name,/ {print}" | sed 's/^[ ]*//g;s/^[\t]*//'
		done
		exec echo "Account Name is not exist in accounts table, please check again!"
	fi
	echo "### Check Account Name End ###"
	echo
}

check_number_of_fields () {
	echo "### Check Number of Fields Bigin ###"
	datetime_now
	CHECK_NUMBER_FIELDS=$(cat $FILENAME | awk -F"," '{if(NF != "4") {print $0}}' | wc -l)
	if [ "$CHECK_NUMBER_FIELDS" -gt 0 ]; then
		nl $FILENAME | awk -F"," '{if(NF != "4") {print $0}}' | sed 's/^[ ]*//g;s/^[\t]*//'
		exec echo "File Contents have wrong number fields != 4, please check again!"
	fi
	echo "### Check Number of Fields End ###"
	echo
}

check_item_quantity () {
	echo "### Check Item Quantity Begin ###"
	datetime_now
	MAX_STACK_NUM="9999"
	for item_quantity in $(cat $FILENAME | awk -F"," '{print $3}' | sort | uniq); do
		#if [[ "$item_quantity" =~ ^[0-9]+$ ]]; then
		if [ "$(echo "$item_quantity" | bc 2>/dev/null)" == "$item_quantity" ]; then
			if [ "$item_quantity" -gt $MAX_STACK_NUM ]; then
				nl $FILENAME | awk "/,$item_quantity,/ {print}" | sed 's/^[ ]*//g;s/^[\t]*//'
				exec echo "Item Quantity ( ${item_quantity} ) is bigger than max stack number $MAX_STACK_NUM, please check again!"
			fi
		else
			nl $FILENAME | awk "/,$item_quantity,/ {print}" | sed 's/^[ ]*//g;s/^[\t]*//'
			exec echo "Item Quantity ( ${item_quantity} ) is invalid, please check again!"
		fi
	done
	echo "### Check Item Quantity End ###"
	echo
}

# Pre Check
check_item_id
check_item_quantity
check_account_name
check_number_of_fields

# Check contents correctly, then insert to prepare reward queue table.
echo "### COPY REWARD TO Item Receivable Begin ###"
datetime_now
psql -U postgres $ACCOUNT_DB_NAME -c "COPY xl_event_send_reward_by_system (account_name, item_id, item_quantity, tag) FROM '$FILENAME' CSV;"
echo "### COPY REWARD TO Item Receivable End ###"
echo
datetime_now
