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

array_diff() {
  local -n a1=$1
  local -n a2=$2
  local result=()

  for val in "${a1[@]}"; do
    skip=false
    for compare in "${a2[@]}"; do
      if [[ "$val" == "$compare" ]]; then
        skip=true
        break
      fi
    done
    if ! $skip; then
      result+=("$val")
    fi
  done

  echo "${result[@]}"
}

if [ "${ENABLE_FIX_CPU_FOR_GAMESERVERS}" == "1" ]; then
	FIX_CPU_ID_STR=$(cat /proc/cmdline | tr ' ' '\n' | grep isolcpus | awk -F"=" '{print $2}')
	IFS="," read -ra FIX_CPU_ID_LISTS <<< "${FIX_CPU_ID_STR}"
	FIX_CPU_ID_TOTAL_NUM=${#FIX_CPU_ID_LISTS[@]}
	
	echo "FIX_CPU_ID_TOTAL_NUM => $FIX_CPU_ID_TOTAL_NUM"
	if [ "${FIX_CPU_ID_TOTAL_NUM}" -gt 0 ]; then
		string_1=$(ps -eo pid,pcpu,comm --sort=-pcpu | grep -v SKServer | grep Server | awk '{print $3}' | head -n ${FIX_CPU_ID_TOTAL_NUM} | tr '\n' ' ')
		string_2=$($HOME/bin/get_all_server_cpu_affinity_into_arr)                                          
		read -ra array_1 <<< "$string_1"
		read -ra array_2 <<< "$string_2"                                                          
		
		diff_1=($(array_diff array_1 array_2))
		diff_2=($(array_diff array_2 array_1))
		
		if [ ${#array_2[@]} -eq 0 ]; then
			for idx in ${!array_1[@]}; do
				server_new=${array_1[$idx]}
				pid_new=$(ps -eo pid,pcpu,comm --sort=-pcpu | grep ${server_new} | awk '{print $1}')
		
				taskset -cp ${cpu_id} ${pid_new}
				if [ "$HOST_NAME" == "TEST" -o "$HOST_NAME" == "SUB" -o "$HOST_NAME" == "SUBMIT" -o "$HOST_NAME" == "DATA" -o "$HOST_NAME" == "DLC" -o "$HOST_NAME" == "TIME1" -o "$HOST_NAME" == "MEDIA" ] ; then
					ENVIRONMENT="TEST"
				else
					ENVIRONMENT="LIVE"
				fi
				ssh CTRL "psql -U postgres WebTool -c \"INSERT INTO monitor_server_cpu_affinity (environment, host_name, process_name, process_id, cpu_id) VALUES ('$ENVIRONMENT', '$HOST_NAME', '$server_new', ${pid_new}, ${FIX_CPU_ID_LISTS[$idx]});\""
			done
		else
			if [ ${#diff_2[@]} -gt 0 ]; then
				for idx in ${!diff_2[@]}; do
					server_old=${diff_2[$idx]}
					pid_old=$(ps -eo pid,pcpu,comm --sort=-pcpu | grep ${server_old} | awk '{print $1}')
					cpu_id=$($HOME/bin/get_specific_server_cpu_affinity ${server_old})
					
					server_new=${diff_1[$idx]}
					pid_new=$(ps -eo pid,pcpu,comm --sort=-pcpu | grep ${server_new} | awk '{print $1}')
					
					$HOME/bin/set_server_to_non_isolated_cpus ${server_old}
					taskset -cp ${cpu_id} ${pid_new}
					if [ "$HOST_NAME" == "TEST" -o "$HOST_NAME" == "SUB" -o "$HOST_NAME" == "SUBMIT" -o "$HOST_NAME" == "DATA" -o "$HOST_NAME" == "DLC" -o "$HOST_NAME" == "TIME1" -o "$HOST_NAME" == "MEDIA" ] ; then
						ENVIRONMENT="TEST"
					else
						ENVIRONMENT="LIVE"
					fi
					ssh CTRL "psql -U postgres WebTool -c \"INSERT INTO monitor_server_cpu_affinity (environment, host_name, process_name, process_id, cpu_id) VALUES ('$ENVIRONMENT', '$HOST_NAME', '$server_new', ${pid_new}, ${FIX_CPU_ID_LISTS[$idx]});\""
				done 
			fi
		fi	
	else
		echo 'Not set isolcpus on grub and reboot'
	fi
else
	echo 'Not Enable parameter ${ENABLE_FIX_CPU_FOR_GAMESERVERS}.'
fi
