#!/system/bin/sh
#
# 06SetVM: script to aumtomatize the swap memory settings
#
# Author:
#	Michelasso @ androidiani.com
#	Miche1asso @ xda
#
# Version 1.1

log -p i -t SetVM "Starting SetVM"

############################################################
# Settings section
#############################################################
#
# Enable the zRAM in here:
# -----------------------
# Change value from OFF to ON to enable it
# size should be 25% of total RAM or less. Value is in MB

ENABLE_ZRAM=OFF
zram_size="70"

# Linux swap settings:
# -------------------
# Change value from OFF to ON to enable it
# The SDcard must be partitioned with the 3rd partition
# created as Linux swap

ENABLE_SWAP=OFF

# End of settings section
############################################################

#***********************************************************************************
# DO NOT CHANGE ANYTHING BELOW THIS LINE IF YOU DO NOT KNOW WHAT YOU ARE DOING!!!!
#***********************************************************************************
#
#############################################################
# zRAM module
#
if [ -z $ENABLE_ZRAM ] || [ ! "$ENABLE_ZRAM" = "ON" ]; then
#
# Swappiness wouldn't really matter. Just set it to zero
#
	echo 0  > /proc/sys/vm/swappiness
#
	log -p i -t SetVM "zRAM is not enabled"
else
#
# Set Swappiness. Use value >0 if zRAM is enabled. Not too high
# Higher the number, higher the swap activity (theoretically...)
#
	echo 40 > /proc/sys/vm/swappiness
#
# Load kernel's zRAM modules
	insmod /system/lib/modules/lzo_compress.ko
	insmod /system/lib/modules/lzo_decompress.ko
	insmod /system/lib/modules/zram.ko
#
# enable zRAM. disksize is in bytes
#
	if [ ! -e /sys/block/zram0/disksize ]; then
		log -p e -t SetVM "Something went wrong. zRAM modules not loaded by the kernel!!"
	else
		echo $(($zram_size*1024*1024)) > /sys/block/zram0/disksize
		mkswap /dev/block/zram0
		swapon -p 100 /dev/block/zram0
		log -p i -t SetVM "zRAM is enabled. Current status of swap areas:"
		log -p i -t SetVM "$(cat /proc/swaps)"
	fi
fi
#
# End of zRAM section
#############################################################


#############################################################
# Enable/disable Linux Swap
# For testing ONLY. The phone may end up lagging heavily
#
if [ -z $ENABLE_SWAP ] || [ ! "$ENABLE_SWAP" = "ON" ]; then
#
# Swap is disabled. Nothing changes
#
# Disable the swap partition just in case. Fail through if not enabled
#
	swapoff /dev/block/mmcblk0p3
	log -p i -t SetVM "Linux swap has been disabled"
elif [ ! -b /dev/block/mmcblk0p3 ]; then
#
# There is no swap partition. 
	log -p e -t SetVM "No Linux swap partion present on SD card"
else
#
        echo 40 > /proc/sys/vm/swappiness
#
# Enable Linux swap
#
	swapon -p 10 /dev/block/mmcblk0p3
	log -p i -t SetVM "Linux swap is enabled. Current status of swap areas:"
	log -p i -t SetVM "$(cat /proc/swaps)"
fi
#
# End of Linux swap section
#############################################################
#
# Finito
#
log -p i -t SetVM "SetVM ended"

