#!/bin/bash

#upmem a Simple Command-line to raise RAM memory.
if [[ -z ${@:1} ]]; then
echo "Raises your RAM of specific running process.

Before run script, application must be opened in the background.

usage : upmem [opt]

opt.
app   : raise memory of applications.
sys   : raise memory of system process.
stapp : memory usage monitor (Apps).
stsys : memory usage monitor (System).

This will work temporary. means, if you close the app, your RAM will back to normal and you need to raise it again."
exit 0
fi

if [ $1 == app ]; then
:
elif [ $1 == sys ]; then
:
elif [ $1 == stapp ]; then
:
elif [ $1 == stsys ]; then
:
else
echo "Invalid option, please try again."
exit 1
fi


#Monitor memory usage Apps.
if [ $1 == stapp ]; then
#Getting list of disk usage (running apps), only disk usage. without id process name. and save to text file.
	ps aux | sort -u | grep 'MEM\|Applications\|Bundle' | sed '/grep\|PlugIns\|Service/d' | awk '{print $2,$3,$4}' > /var/tmp/x.txt
#Getting list only name of id process (running applications). and save to text file.
	ps aux | sort -u | grep 'Applications\|Bundle' | sed '/grep\|PlugIns\|Service/d' | sed '1 i\PROCESS' | awk -F '/' '{print $NF}' | sed 's/\s.*$//' > /var/tmp/y.txt
#Declare path of both text files.
	alg=/var/tmp/x.txt
	algs=/var/tmp/y.txt
#Merge both text files. then do alignments and sort to make nice display tabular.
	paste $algs $alg | expand -t $(( $(wc -L <$algs) + 2 )) | awk 'NR == 1; NR > 1 {print $0 | "sort -f"}'
#Delete all text files.
	rm $alg
	rm $algs
	exit 0
#Monitor memory usage Sys.
elif [ $1 == stsys ]; then
	ps aux | sort -u | grep 'MEM\|usr\|System\|sbin' | sed '/grep\|com/d' | awk '{print $2,$3,$4}' > /var/tmp/x.txt
	ps aux | sort -u | grep 'usr\|System\|sbin' | sed '/grep\|com/d' | sed '1 i\PROCESS' | awk -F '/' '{print $NF}' | sed 's/\s.*$//' > /var/tmp/y.txt
	alg=/var/tmp/x.txt
	algs=/var/tmp/y.txt
	paste $algs $alg | expand -t $(( $(wc -L <$algs) + 2 )) | awk 'NR == 1; NR > 1 {print $0 | "sort -f"}'
	rm $alg
	rm $algs
	exit 0
fi

#Declare
LGR='\033[1;37m'
NC='\033[0m'
siz=$(sysctl -n hw.memsize)
size=$(echo -n "$siz" | wc -c)
sz=$(sysctl -n hw.memsize | head -c 4)
szz=$(sysctl -n hw.memsize | head -c 5)
max=$(echo max RAM $sz\MB)
maxx=$(echo max RAM $szz\MB)

inv() {
x=$(expr $wr + 1)
if [ $sel == $x ]; then
	exit 0
elif [ $size == 10 ]; then
 if [[ $sel -gt $sz ]]; then
 echo "Invalid, $max"
 sel='';
 fi
elif [ $size == 11 ]; then
 if [[ $sel -gt $szz ]]; then
 echo "Invalid, $maxx"
 sel='';
 fi
elif [[ $sel =~ ^[0-9]+$ ]]; then
:
else
echo "Invalid, try again."
	sel='';
fi
}
#Listing id process name of running applications
if [ $1 == app ]; 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 Apps : "

		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
#Set memory-size
			while [[ $sel == '' ]]; do
				if [ $size == 10 ]; then
					read -p "Set memory-size to raise ($max): " sel
				else
					read -p "Set memory-size to raise ($maxx): " sel
				fi

#Error handling
wr=$(ps -A | grep 'Bundle\|Applications' | sed '/grep\|PlugIns\|Service/d' | awk -F "/" 'NF{ print $NF }' | sort -f | wc -l)
				inv
			done
#Execute jetsamctl
				jetsamctl -l $sel $opt 2>/dev/null
#Error handling
				code=$?
				if [ ! $code -eq 0 ]; then
					echo "error: id process expired."
					exit $code
				fi
				echo -e Done,\ $sel\MB\ raised\ for\ ${LGR}\'$opt\'${NC}
				break;
			else
				echo "Invalid, try again."
			fi
		done
else
#Listing id process name of running system process.
	IFS=','
	options=($(ps -A | grep 'usr\|System\|sbin' | sed '/grep\|com\|Radio\|upmem\|Gestalt\|Mounter\|service\|Service\|Proxy\|geFi/d' | awk -F '/' '{print $NF}' | sed 's/\s.*$//' | sort -f | tr '\n' ','))
	PS3="Please select num of id process : "

		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
#Set memory-size
			while [[ $sel == '' ]]; do
				if [ $size == 10 ]; then
					read -p "Set memory-size to raise ($max): " sel
				else
					read -p "Set memory-size to raise ($maxx): " sel
				fi
#Error handling
				wr=$(ps -A | sort -u | grep 'usr\|System\|sbin' | sed '/grep\|com\|Radio\|upmem\|Gestalt\|Mounter\|service\|Service\|Proxy\|geFi/d' | awk -F '/' '{print $NF}' | sed 's/\s.*$//' | wc -l)
				inv
done
		
#Execute jetsamctl
				jetsamctl -l $sel $opt 2>/dev/null
				code=$?
				if [ ! $code -eq 0 ]; then
					echo "error: id process expired."
					exit $code
				fi
				echo -e Done,\ $sel\MB\ raised\ for\ ${LGR}\'$opt\'${NC}
				break;
			else
			echo "Invalid, try again."
			fi
		done
fi
exit 0

#Moose <gathering011@gmail.com>