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

source $HOME/.gamerc

XLUSER_ID=$1
ADD_PVALUES=$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 ( PValues ) ###"
	psql -U postgres ${MEMBER_DB_NAME} -c "SELECT idnum, mid, pvalues, bonus FROM tb_user WHERE mid = '${XLUSER_ID}';"
	#SELECT add_point('4716472',999999,'20241023_4716472_999999_TEST','TW');
	current_datetime=$(date '+%Y%m%dT%H%M%S')
	psql -U postgres ${MEMBER_DB_NAME} -c "SELECT add_point('${XLUSER_ID}',${ADD_PVALUES},'${XLUSER_ID}_${current_datetime}_${ADD_PVALUES}_TEST','TW');"
	echo "### After modification ( PValues ) ###"
	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
