#!/bin/bash
#===============================================================================
#
#          FILE: addaccount
#
#         USAGE: addaccount <Account Name> <Password>
#
#   DESCRIPTION: Add an account to the account database
#
#       OPTIONS: <Account Name> <Password> [Remote Server]
#
#  REQUIREMENTS:
#
#         NOTES:
#
#          BUGS:  ---
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#		 vincent (Vincent Tu), xvincent.tu@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Mon May 23 02:35:52 EDT 2011
#	UPDATED: Tue Apr 17 11:45:00 EDT 2012
#      REVISION: 2.0
#
#          TODO:
#
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
set -m                                       # Enable job control 
source ~/.gamerc
ACTION="update"

[ "$1" == "" ] && exec echo "Usage: $0 <Account Name> <Password> [Remote Server]"

if [ "$3" == "" ]; then
	echo "insert into tb_user ( mid, password, pwd, memberid ) values ('$1', '$2', md5('$2'), '$1');" \
   	| psql $MEMBER_DB_NAME 2>&1 | grep ERROR \
		&& ulogger_echo "Add account \"$1\" on \"TEST\" failure" | colorize red black \
		|| ulogger_echo "Add account \"$1\" on \"TEST\" successfully" | colorize green black
else
	echo "insert into tb_user ( mid, password, pwd, memberid ) values ('$1', '$2', md5('$2'), '$1');" \
   	| psql -h "$3" $MEMBER_DB_NAME 2>&1 | grep ERROR \
		&& ulogger_echo "Add account \"$1\" on \"$3\" failure" | colorize red black \
		|| ulogger_echo "Add account \"$1\" on \"$3\" successfully" | colorize green black
fi
