#!/bin/bash
source $HOME/.gamerc

if [ "${ENABLE_FIX_WS_CPU}" == "1" ]; then
	WS_FIX_CPU_ID=$(cat /proc/cmdline | tr ' ' '\n' | grep isolcpus | awk -F"=" '{print $2}')

	if [ ! -z "${WS_FIX_CPU_ID}" ]; then
		while ! ps -ef | egrep -v 'grep|rsync' | grep './WorldServer' | awk -F" " '{print $2}' > /dev/null; do
			sleep 1 # Check every second
		done

		WS_PROCESS_ID=$(ps -ef | egrep -v 'grep|rsync' | grep './WorldServer' | awk -F" " '{print $2}')

		if ! command -v taskset &> /dev/null; then
			# Check OS Debian to do do the security settings
			OS_DISTRO=$(cat /etc/os-release | grep ^ID= | sed 's/ID=//;s/"//g')
			if [ "${OS_DISTRO}" == "debian" -o "${OS_DISTRO}" == "ubuntu" ]; then
				sudo apt-get update
				sudo apt-get install -y util-linux
			elif [ "${OS_DISTRO}" == "centos" -o "${OS_DISTRO}" == "fedora" ]; then
				sudo yum -y install util-linux
			fi
		fi
		
		taskset -cp ${WS_FIX_CPU_ID} ${WS_PROCESS_ID}
	else
		echo 'Not set isolcpus on grub and reboot'
	fi
else
	echo 'Not Enable parameter ${ENABLE_FIX_WS_CPU}.'
fi
