#!/bin/bash
[ "$2" == "" ] && exec echo "Usage: $0 <XLUSER_ID> <ADD_BONUS>"

source $HOME/.gamerc

XLUSER_ID=$1
ADD_BONUS=$2

get_os() {
        local os=$(cat /etc/os-release | egrep "^ID=" | sed 's/ID=//;s/"//g')
        echo ${os}
}

get_environment() {
        local os=$(get_os)
        if [ "${os}" == "centos" ]; then
                local internal_ip=$(/sbin/ifconfig eth0 | grep 'inet' | grep -v 'inet6' | awk -F' ' '{print $2}' | awk '{print $1}')
        elif [ "${os}" == "debian" ]; then
                local internal_ip=$(/sbin/ifconfig ens4 | grep 'inet' | grep -v 'inet6' | awk -F' ' '{print $2}' | awk '{print $1}')
        fi
        local checked=$(sed -n '/PLEASE DO NOT CHANGE THE FILE/,/DO NOT CHANGE THE SERVER NAMES ABOVE/p' /etc/hosts | egrep "${internal_ip} ")
        if [ -z "${checked}" ]; then
                local environment="TEST"
        else
                local environment="LIVE"
        fi
        echo ${environment}
}

environment=$(get_environment)

if [ ${environment} == "TEST" ]; then
	echo "### Before modification ( Bonus ) ###"
	psql -U postgres ${MEMBER_DB_NAME} -c "SELECT idnum, mid, pvalues, bonus FROM tb_user WHERE mid = '${XLUSER_ID}';"
	current_datetime=$(date '+%Y%m%dT%H%M%S')
	psql -U postgres ${MEMBER_DB_NAME} -c "UPDATE tb_user SET bonus = bonus + ${ADD_BONUS} WHERE mid = '${XLUSER_ID}';"
	echo "### After modification ( Bonus ) ###"
	psql -U postgres ${MEMBER_DB_NAME} -c "SELECT idnum, mid, pvalues, bonus FROM tb_user WHERE mid = '${XLUSER_ID}';"
elif [ ${environment} == "LIVE" ]; then
	exec echo "You can not add values on LIVE!"
else
	exec echo "Unknown Environment."
fi
