#!/system/bin/sh
#
# Script to optimize various filesystem parameters
#
# Author:
#       Michelasso @ androidiani.com
#       Miche1asso @ xda
#
# Version 1.02



log -p i -t FSoptimize "Starting FSoptimize"

LBB="logwrapper busybox"

# / and /system must be read-write
mount -o remount,rw auto /
mount -o remount,rw auto /system

#remounting the file systems with noatime and nodiratime flags to save battery and CPU cycles

for k in $($LBB mount | grep relatime | cut -d " " -f3)
do
	sync
	log -p i -t FSoptimize "Remounting filesystem $k with noatime,nodiratime"
	$LBB mount -o remount,noatime,nodiratime $k

done


for k in $($LBB mount | grep "type ext" | cut -d " " -f3)
do
	sync
	log -p i -t FSoptimize "Remounting filesystem $k with discard,barrier=0,commit=60,noauto_da_alloc,delalloc"
	$LBB mount -o remount,discard,barrier=0,commit=60,noauto_da_alloc,delalloc $k
done


# if mounting is not successful then manual mounting will do the thing for you
$LBB mount -o remount,noatime,nodiratime auto /mnt/sdcard
$LBB mount -o remount,noatime,nodiratime auto /mnt/secure/asec
$LBB mount -o remount,noatime,nodiratime auto /mnt/sdcard/.android_secure


# Tweak the read ahead buffer to 2048 KB
for i in $(find /sys/devices/virtual/bdi -name read_ahead_kb); do
	echo 2048 > $i
done

# remove junk
rm /data/local/*.apk
rm /data/local/tmp/*.apk
rm /data/log/*.log
rm /data/log/dumpstate_*
rm /cache/*.*
rm /cache/recovery/*.*
rm -r /cache/.busybox
rm /data/system/dropbox/*.txt
rm /data/backup/pending/*.tmp
rm /data/tombstones/*.*
#
# application logs
#                                                                                                                                                                                    
# Whatsapp is simply outrageous. 10MB of logging in one day. It also seems that it reduces drastically the performances.                                                             
# Sending its log where it belongs: the Void!                                                                                                                                        
rm /data/data/com.whatsapp/files/Logs/*                                                                                                                                           
ln -s /dev/null /data/data/com.whatsapp/files/Logs/whatsapp.log
# also remove SuperSU logs
rm /data/data/eu.chainfire.supersu/logs/*

# Fix permission
chmod 755 /data/system/usagestats

# make it sure that / and /system are read-only
mount -o remount,ro auto /
mount -o remount,ro auto /system

log -p i -t FSoptimize "FSoptimize ended"
