#!/bin/bash
#===============================================================================
#
#          FILE: giveout_item
#
#         USAGE: giveout_item <Account name|Account list file> <Item ID> [Item ID] ...
#
#   DESCRIPTION: Insert item data for giveout to Account Database table item_receivable
#
#       OPTIONS: <Account name|Account list file> <Item ID> [Item ID] ...
#
#  REQUIREMENTS:
#
#         NOTES:
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Mon Jan 16 17:42:32 KST 2012
#      REVISION: 1.0
#
#          TODO:
#
#===============================================================================

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

[ "$2" == "" ] && exec echo "Usage: $0 <Account name|Account list file> <Item ID> [Item ID] ..."

source ~/.gamerc

DATE="$(date +%Y%m%dT%H%M%S)"

ACCOUNT="$1"
[ -f $ACCOUNT ] && IS_LIST_FILE=1

shift

[ "$IS_LIST_FILE" == "1" ] && MAILNAME="$(cut -c 0-32 <<< $ACCOUNT)" || MAILNAME="giveout_item-$DATE"

echo "Generating the delete list ... " | colorize green
for account in $([ "$IS_LIST_FILE" == "1" ] && cat $ACCOUNT | grep -v "^$" | tr -d '\r' || echo $ACCOUNT);do 
   for itemid in $*;do 
	echo "delete from item_receivable where item_id = $itemid and item_quantity = 1 and account_name = '$account' and mail_name = '$MAILNAME' and id = any(array(select id from item_receivable where item_id = $itemid and item_quantity = 1 and account_name = '$account' and mail_name = '$MAILNAME' order by id desc limit 1));"
   done
done > giveout_item-$DATE-$ACCOUNT-delete_item.sql

echo "Total: $(wc -l < giveout_item-$DATE-$ACCOUNT-delete_item.sql)" | colorize green

for account in $([ "$IS_LIST_FILE" == "1" ] && cat $ACCOUNT | grep -v "^$" | tr -d '\r' || echo $ACCOUNT);do 
   for itemid in $*;do 
	echo "insert into item_receivable (item_id, item_quantity, account_name, mail_name) values ($itemid, 1, '$account', '$MAILNAME');"
   done
done > giveout_item-$DATE-$ACCOUNT-insert_item.sql

# Show how many lines
wc -l giveout_item-$DATE-$ACCOUNT-* | grep giveout_item-$DATE-$ACCOUNT-
echo

# Insert item shell script
cat > giveout_item-$DATE-$ACCOUNT-insert_item.sh << __EOF__
#!/bin/bash

source ~/.gamerc
cat giveout_item-$DATE-$ACCOUNT-insert_item.sql | psql -e $ACCOUNT_DB_NAME 2>&1 | highlight - "INSERT [0-9]+ [^1]" "INSERT [0-9]+ 1"| tee giveout_item-$DATE-$ACCOUNT-insert_item.log
__EOF__
chmod 755 giveout_item-$DATE-$ACCOUNT-insert_item.sh
echo "giveout_item-$DATE-$ACCOUNT-insert_item.sh ready." | colorize green

# Delete item shell script
cat > giveout_item-$DATE-$ACCOUNT-delete_item.sh << __EOF__
#!/bin/bash

source ~/.gamerc
cat giveout_item-$DATE-$ACCOUNT-delete_item.sql | psql -e $ACCOUNT_DB_NAME 2>&1 | highlight - "DELETE [^1]" "DELETE 1" | tee giveout_item-$DATE-$ACCOUNT-delete_item.log
__EOF__
chmod 755 giveout_item-$DATE-$ACCOUNT-delete_item.sh
echo "giveout_item-$DATE-$ACCOUNT-delete_item.sh ready." | colorize green

