#!/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.
strict    : increase full performance, prevent opening apps and prevent all apps spawn in the background except the game to boost more performance.
exstrict  : to exit strict mode.


note: Game must be opened in the background.

you have to use option 'exstrict' to exit from strict mode.
"

exit 0
fi

LGR='\033[1;37m'
NC='\033[0m'
path=/private/var/containers/Bundle/Application

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

if [ $1 == high ]; then

echo Listing Games...
cd $path
ls > /var/jb/tmp/ls.txt
touch /var/jb/tmp/list.txt
list=$(cat /var/jb/tmp/ls.txt)

for l in $list; do

game=$(plutil $path/$l/iTunesMetadata.plist 2>/dev/null | grep -w "Games" | awk '{print $3}' | cut -c -5)
trs=$(ls $path/$l | grep _Troll)

if [[ $game == Games ]]; then
ls $path/$l | grep app | awk -F'.' '{print $1}' >> /var/jb/tmp/list.txt

elif [[ $trs == _TrollStore ]]; then

find $path/$l -type f -iname "*Game*" | cut -d'/' -f8 | cut -d. -f1 >> /var/jb/tmp/list.txt

fi

done


IFS=','
	options=($(cat /var/jb/tmp/list.txt | awk '!x[$0]++' | 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
rm /var/jb/tmp/list.txt 2>/dev/null
rm /var/jb/tmp/ls.txt 2>/dev/null
				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)

echo Listing Games...
cd $path
ls > /var/jb/tmp/ls.txt
touch /var/jb/tmp/list.txt
list=$(cat /var/jb/tmp/ls.txt)

for l in $list; do

game=$(plutil $path/$l/iTunesMetadata.plist 2>/dev/null | grep -w "Games" | awk '{print $3}' | cut -c -5)
trs=$(ls $path/$l | grep _Troll)

if [[ $game == Games ]]; then
ls $path/$l | grep app | awk -F'.' '{print $1}' >> /var/jb/tmp/list.txt

elif [[ $trs == _TrollStore ]]; then

find $path/$l -type f -iname "*Game*" | cut -d'/' -f8 | cut -d. -f1 >> /var/jb/tmp/list.txt

fi

done


IFS=','
	options=($(cat /var/jb/tmp/list.txt | awk '!x[$0]++' | 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
rm /var/jb/tmp/list.txt 2>/dev/null
rm /var/jb/tmp/ls.txt 2>/dev/null
				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

elif [ $1 == strict ]; then
echo Listing Games...
cd $path
ls > /var/jb/tmp/ls.txt
touch /var/jb/tmp/list.txt
list=$(cat /var/jb/tmp/ls.txt)

for l in $list; do

game=$(plutil $path/$l/iTunesMetadata.plist 2>/dev/null | grep -w "Games" | awk '{print $3}' | cut -c -5)
trs=$(ls $path/$l | grep _Troll)

if [[ $game == Games ]]; then
ls $path/$l | grep app | awk -F'.' '{print $1}' >> /var/jb/tmp/list.txt

elif [[ $trs == _TrollStore ]]; then

find $path/$l -type f -iname "*Game*" | cut -d'/' -f8 | cut -d. -f1 >> /var/jb/tmp/list.txt

fi

done

IFS=','
	options=($(cat /var/jb/tmp/list.txt | awk '!x[$0]++' | 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
rm /var/jb/usr/bin/cls 2>/dev/null
rm /var/jb/tmp/list.txt 2>/dev/null
rm /var/jb/tmp/ls.txt 2>/dev/null
				exit 0
			elif (( REPLY > 0 && REPLY <= ${#options[@]} )) 2>/dev/null; then

cat /var/jb/var/mobile/Library/Gboost/strict.txt | sed "s/MTerminal/MTerminal\\\|$opt/g" > /var/jb/usr/bin/cls && chmod 755 /var/jb/usr/bin/cls
cls -k 2>/dev/null &
rm /var/jb/usr/bin/cls 2>/dev/null
rm /var/jb/tmp/list.txt 2>/dev/null
rm /var/jb/tmp/ls.txt 2>/dev/null

#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 strict mode, enjoy the game!
					exit 0
else
echo -e Done, ${LGR}\'$opt\'${NC} boosted to strict mode, enjoy the game!
					exit 0
				fi
				break;
			else
				echo "Invalid, try again."
			fi
		done

elif [ $1 == exstrict ]; then

pid=$(ps -e | grep cls | sed -n '1{p;q}' | awk '{print $1}')
kill $pid 2>/dev/null
code=$?
if [ ! $code -eq 0 ]; then
kill $pid 2>/dev/null
else
:
fi
else
echo "Invalid, try again"
exit 82
fi

#Moose <gathering011@gmail.com>