#!/bin/bash
sleep 3;
export LC_ALL=C
ExecuteOpti="False"

# Get the first argument (command name)
Exec_String=$(ps -p $1 -o cmd=)
echo $Exec_String

# Loop through .ninf files in the directory
for file in /Users/$USER/.config/NebiSoft/NebiDE.optimizer/*.filter.ninf; do
    echo $file
    
    # Check if the file exists
    if [ -e "$file" ]; then
        # Source the .ninf file to load variables
        source "$file"
        
        # Check if the first argument matches the Exec_Pattern
        if [[ "$Exec_String" =~ $Exec_Pattern ]]; then
            echo "Pattern '$Exec_Pattern' matches the current Client's Exec string: $Exec_String"
            ExecuteOpti="True"
            config=$(echo $file | sed 's/.*\///g;s/\..*//g')
            break  # Exit the loop when a match is found
        fi
    fi
done

if [ "$Only_On_Fullscreen" == "True" ]; then
	ExecuteOpti="False"

	if [ "$2" == "true" ]; then
		ExecuteOpti="True"
	fi
fi

if [ "$config" == "" ]; then
	ExecuteOpti="False"
fi

if [ "$ExecuteOpti" == "True" ]; then
	notify-send "Game mode by NebiDE optimizer is on." "Configuration '$config' loaded."
	renice $Game_Nice_Level -p $1
	renice $NebiDE_Nice_Level -p $(pidof awesome | head -1)
	
	if [ "$Kill_Compositor" == "True" ]; then
		killall picom
	fi
	
	if [ "$Kill_Wallpaper_Engine" == "True" ]; then
		killall nebide-we
	fi
	
	if [ "$Custom_Sh_Script" != "" ]; then
		bash "$Custom_Sh_Script"
	fi
	
	if [ "$NebiDE_CPU_Limit" != 100 ]; then
		cpulimit -p $(pidof awesome | head -1) -l $NebiDE_CPU_Limit
	fi
fi

