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

backup
file_modification
add_current_datetime
add_note
add_orig_status
copy_csv
set_current_bonus_to_log
set_modified_bonus_to_log
set_modified_bonus_to_data
set_checked_bonus_to_log
check_checed_bonus_with_modified_bonus
update_final_status_to_log
get_log_data


all => backup + file_modification + add_current_datetime + add_note + add_orig_status + copy_csv + set_current_bonus_to_log + set_modified_bonus_to_log + set_modified_bonus_to_data + set_checked_bonus_to_log + check_checed_bonus_with_modified_bonus + update_final_status_to_log + get_log_data
prepare => backup + file_modification + add_current_datetime + add_note + add_orig_status + copy_csv
execute => set_modified_bonus_to_data
verify => check_checed_bonus_with_modified_bonus

P2: <DBNAME> 
P3: <FILENAME with full path> 
P4: <NOTE>"

ACTION=$1
DBNAME=$2
FILENAME_FULL=$3
FILENAME_BASE=$(basename $3)
NOTE=$4

echo "Initialize: ${FILENAME_FULL} ${FILENAME_BASE} ${NOTE}"

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 add_current_datetime() {
	FILENAME_FULL=$1
	DATETIME="NOW()"
	RECORD_LINES=$(cat ${FILENAME_FULL} | awk -F"," '{print $2","$3}' | sort | uniq -c | wc -l)
	echo "Add Current DateTime: ${DATETIME}"
	sed -i "s/\$/,${DATETIME}/g" ${FILENAME_FULL}
	tail -n ${RECORD_LINES} ${FILENAME_FULL}
}

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

function add_orig_status() {
	FILENAME_FULL=$1
	ORIG_STATUS=0
	RECORD_LINES=$(cat ${FILENAME_FULL} | awk -F"," '{print $2","$3}' | sort | uniq -c | wc -l)
	echo "Add Original Status: ${ORIG_STATUS}"
	sed -i "s/\$/,${ORIG_STATUS}/g" ${FILENAME_FULL}
	tail -n ${RECORD_LINES} ${FILENAME_FULL}
}

function copy_csv() {
	DBNAME=$1
	FILENAME_FULL=$2
	TABLENAME="z_comp_aren_bonus_log"
	echo "Copy CSV: ${DBNAME} ${FILENAME_FULL} ( ${TABLENAME} )"
	psql -U postgres ${DBNAME} -c "COPY (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
