#!/bin/bash
[ "$4" == "" ] && exec echo "Usage: $0 
P1: <ACTION>

backup
file_modification
check_item_id
add_test_acc
add_mail_tag
check_mail_tag_1
check_mail_tag_2
send_reward
del_reward

all => backup + file_modification + check_item_id + add_test_acc + add_mail_tag + check_mail_tag_1 + send_reward + verify
check => backup + file_modification + check_item_id
prepare => backup + file_modification + check_item_id + add_test_acc + add_mail_tag + check_mail_tag_1
execute => check_mail_tag_1 + send_reward + verify
verify => check_mail_tag_1 + check_mail_tag_2

P2: <DBNAME> 
P3: <FILENAME with full path> 
P4: <MAIL_TAG> 
P5: <TEST_XLUSERID>"

ACTION=$1
DBNAME=$2
FILENAME_FULL=$3
FILENAME_BASE=$(basename $3)
MAIL_TAG=$4
TEST_XLUSERID=$5

if [ -z "${TEST_XLUSERID}" ]; then
	TEST_XLUSERID="893363"
fi

echo "Initialize: ${FILENAME_FULL} ${FILENAME_BASE} ${MAIL_TAG} ${TEST_XLUSERID}"

function backup() {
	FILENAME_FULL=$1
	if [ ! -f "${FILENAME_FULL}" ]; then
		exec echo "${FILENAME_FULL} is not exist!"
	fi

	echo "Backup: ${FILENAME_FULL}"
	BACKUP_FILENAME="${FILENAME_FULL}.bak"
	if [ ! -f "${BACKUP_FILENAME}" ]; then
		cp -arf ${FILENAME_FULL} ${BACKUP_FILENAME}
	else
		rm -rf ${FILENAME_FULL}
		cp -arf ${BACKUP_FILENAME} ${FILENAME_FULL}
	fi

	ls -hltra ${FILENAME_FULL} ${BACKUP_FILENAME}
}

function file_modification() {
	FILENAME_FULL=$1
	if [ -f "${FILENAME_FULL}" ]; then
		chmod 644 ${FILENAME_FULL}; dos2unix ${FILENAME_FULL}
		echo "" >> ${FILENAME_FULL}
		sed -i '/^$/d' ${FILENAME_FULL}
		echo "File Modification: ${FILENAME_FULL}"
	else
		exec echo "${FILENAME_FULL} is not exist!"
	fi
}

function check_item_id() {
	FILENAME_FULL=$1
	TOTAL_LINES=$(cat ${FILENAME_FULL} | wc -l)
	echo "Check Item ID: Statistics ORIG (Count: ${TOTAL_LINES})"
	cat ${FILENAME_FULL} | awk -F"," '{print $2","$3}' | sort | uniq -c
}

function add_test_acc() {
	FILENAME_FULL=$1
	TEST_XLUSERID=$2
	REWARD_RECORD_LINES=$(cat ${FILENAME_FULL} | awk -F"," '{print $2","$3}' | sort | uniq -c | wc -l)
	for reward_record in $(cat ${FILENAME_FULL} | awk -F"," '{print $2","$3}' | sort | uniq); do 
		sed -i -e "\$a${TEST_XLUSERID},${reward_record}" ${FILENAME_FULL};
	done; 
	TOTAL_LINES=$(cat ${FILENAME_FULL} | wc -l)
	echo "Add Test Account: ${TEST_XLUSERID} Statistics MODIFICATION (Count: ${TOTAL_LINES})"
	tail -n ${REWARD_RECORD_LINES} ${FILENAME_FULL}
}

function add_mail_tag() {
	FILENAME_FULL=$1
	MAIL_TAG=$2
	REWARD_RECORD_LINES=$(cat ${FILENAME_FULL} | awk -F"," '{print $2","$3}' | sort | uniq -c | wc -l)
	echo "Add Mail Tag: ${MAIL_TAG}"
	sed -i "s/\$/,${MAIL_TAG}/g" ${FILENAME_FULL}
	tail -n ${REWARD_RECORD_LINES} ${FILENAME_FULL}
}

function check_mail_tag_1() {
	DBNAME=$1
	MAIL_TAG=$2
	echo "Check Mail Tag: ${DBNAME} ${MAIL_TAG} ( Item Receivable )"
	psql -U postgres ${DBNAME} -c "SELECT mail_name, COUNT(*) FROM item_receivable WHERE mail_name = '${MAIL_TAG}' GROUP BY mail_name;"
}

function check_mail_tag_2() {
	DBNAME=$1
	MAIL_TAG=$2
	echo "Check Mail Tag: ${DBNAME} ${MAIL_TAG} ( Item Receipt )"
	psql -U postgres ${DBNAME} -c "SELECT mail_name, COUNT(*) FROM item_receipt WHERE mail_name = '${MAIL_TAG}' GROUP BY mail_name;"
}

function send_reward() {
	DBNAME=$1
	FILENAME_FULL=$2
	MAIL_TAG=$3
	echo "Send Reward: ${DBNAME} ${MAIL_TAG} ( Item Receivable )"
	psql -U postgres ${DBNAME} -c "COPY item_receivable (account_name, item_id, item_quantity, mail_name) FROM '${FILENAME_FULL}' CSV;"
}

function del_reward() {
	DBNAME=$1
	MAIL_TAG=$2
	echo "Delete Reward: ${DBNAME} ${MAIL_TAG} ( Item Receivable )"
	psql -U postgres ${DBNAME} -c "DELETE FROM item_receivable WHERE mail_name = '${MAIL_TAG}';"
}

function all() {
	DBNAME=$1
	FILENAME_FULL=$2
	MAIL_TAG=$3
	TEST_XLUSERID=$4

	backup ${FILENAME_FULL}
	file_modification ${FILENAME_FULL}
	check_item_id ${FILENAME_FULL}
	add_test_acc ${FILENAME_FULL} ${TEST_XLUSERID}
	add_mail_tag ${FILENAME_FULL} ${MAIL_TAG}
	check_mail_tag_1 ${DBNAME} ${MAIL_TAG}
	send_reward ${DBNAME} ${FILENAME_FULL} ${MAIL_TAG}
	verify ${DBNAME} ${MAIL_TAG}
}

function check() {
	FILENAME_FULL=$1

	backup ${FILENAME_FULL}
	file_modification ${FILENAME_FULL}
	check_item_id ${FILENAME_FULL}
}

function prepare() {
	DBNAME=$1
	FILENAME_FULL=$2
	MAIL_TAG=$3
	TEST_XLUSERID=$4

	backup ${FILENAME_FULL}
	file_modification ${FILENAME_FULL}
	check_item_id ${FILENAME_FULL}
	add_test_acc ${FILENAME_FULL} ${TEST_XLUSERID}
	add_mail_tag ${FILENAME_FULL} ${MAIL_TAG}
	check_mail_tag_1 ${DBNAME} ${MAIL_TAG}
}

function execute() {
	DBNAME=$1
	FILENAME_FULL=$2
	MAIL_TAG=$3

	check_mail_tag_1 ${DBNAME} ${MAIL_TAG}
	send_reward ${DBNAME} ${FILENAME_FULL} ${MAIL_TAG}
	verify ${DBNAME} ${MAIL_TAG}
}

function verify() {
	DBNAME=$1
	MAIL_TAG=$2

	check_mail_tag_1 ${DBNAME} ${MAIL_TAG}
	check_mail_tag_2 ${DBNAME} ${MAIL_TAG}
}

if [ "${ACTION}" == "backup" ]; then
	backup ${FILENAME_FULL}	
elif [ "${ACTION}" == "file_modification" ]; then
	file_modification ${FILENAME_FULL}
elif [ "${ACTION}" == "check_item_id" ]; then
	check_item_id ${FILENAME_FULL}
elif [ "${ACTION}" == "add_test_acc" ]; then
	add_test_acc ${FILENAME_FULL} ${TEST_XLUSERID}
elif [ "${ACTION}" == "add_mail_tag" ]; then
	add_mail_tag ${FILENAME_FULL} ${MAIL_TAG}
elif [ "${ACTION}" == "check_mail_tag_1" ]; then
	check_mail_tag_1 ${DBNAME} ${MAIL_TAG}
elif [ "${ACTION}" == "check_mail_tag_2" ]; then
	check_mail_tag_2 ${DBNAME} ${MAIL_TAG}
elif [ "${ACTION}" == "send_reward" ]; then
	send_reward ${DBNAME} ${FILENAME_FULL} ${MAIL_TAG}
elif [ "${ACTION}" == "del_reward" ]; then
	del_reward ${DBNAME} ${MAIL_TAG}
elif [ "${ACTION}" == "all" ]; then
	all ${DBNAME} ${FILENAME_FULL} ${MAIL_TAG} ${TEST_XLUSERID}
elif [ "${ACTION}" == "check" ]; then
	check ${FILENAME_FULL}
elif [ "${ACTION}" == "prepare" ]; then
	prepare ${DBNAME} ${FILENAME_FULL} ${MAIL_TAG} ${TEST_XLUSERID}
elif [ "${ACTION}" == "execute" ]; then
	execute ${DBNAME} ${FILENAME_FULL} ${MAIL_TAG}
elif [ "${ACTION}" == "verify" ]; then
	verify ${DBNAME} ${MAIL_TAG}
fi
