#!/bin/bash

if [[ -z ${@:1} || $1 == --help ]]; then
	echo "Gboost. Command-line tools to boost game performance.

usage : gboost [opt]

opt.
medium    : increase performance slightly.
high      : increase full performance.


note: Game must be opened in the background.
"

exit 0
fi

LGR='\033[1;37m'
NC='\033[0m'

mem=$(sysctl -n hw.memsize | head -c 4)
div=2

if [ $1 == high ]; then

IFS=','
	options=($(ps -A | grep 'Bundle\|Applications' | sed '/grep\|PlugIns\|Service/d' | awk -F "/" 'NF{ print $NF }' | sort -f | tr '\n' ','))
	PS3="Please select num of your Game : "

		select opt in "${options[@]}" "QUIT"; do
			if (( REPLY == 1 + ${#options[@]} )) 2>/dev/null ; then
				exit 0
			elif (( REPLY > 0 && REPLY <= ${#options[@]} )) 2>/dev/null; then

#Execute jetsamctl
				jetsamctl -l $mem $opt 2>/dev/null
#Error handling
				code=$?
				if [ ! $code -eq 0 ]; then
					echo -e Done, ${LGR}\'$opt\'${NC} boosted to high performance, enjoy the game!
					exit 0
else
echo -e Done, ${LGR}\'$opt\'${NC} boosted to high performance, enjoy the game!
					exit 0
				fi
				break;
			else
				echo "Invalid, try again."
			fi
		done

elif [ $1 == medium ]; then
med=$(expr $mem / $div)

IFS=','
	options=($(ps -A | grep 'Bundle\|Applications' | sed '/grep\|PlugIns\|Service/d' | awk -F "/" 'NF{ print $NF }' | sort -f | tr '\n' ','))
	PS3="Please select num of your Game : "

		select opt in "${options[@]}" "QUIT"; do
			if (( REPLY == 1 + ${#options[@]} )) 2>/dev/null ; then
				exit 0
			elif (( REPLY > 0 && REPLY <= ${#options[@]} )) 2>/dev/null; then

#Execute jetsamctl
				jetsamctl -l $med $opt 2>/dev/null
#Error handling
				code=$?
				if [ ! $code -eq 0 ]; then
					echo -e Done, ${LGR}\'$opt\'${NC} boosted to medium performance, enjoy the game!
exit 0
else
echo -e Done, ${LGR}\'$opt\'${NC} boosted to medium performance, enjoy the game!
					exit 0
				fi
				break;
			else
				echo "Invalid, try again."
			fi
		done
else
echo "Invalid, try again"
exit 82
fi

#Moose <gathering011@gmail.com>