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

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

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

		MS_PROCESS_ID=$(ps -ef | egrep -v 'grep|rsync' | grep './MissionServer' | 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 ${MS_FIX_CPU_ID} ${MS_PROCESS_ID}
	else
		echo 'Not set isolcpus on grub and reboot'
	fi
else
	echo 'Not Enable parameter ${ENABLE_FIX_MS_CPU}.'
fi
