#!/system/xbin/sh
# imoseyon mods

# file system speedups
busybox mount -o remount,noatime,barrier=0,nobh /system
busybox mount -o remount,noatime /data
busybox mount -o remount,noatime,barrier=0,nobh /cache

# drop garbage in caches
busybox rm -f /cache/*.apk
busybox rm -f /cache/*.tmp
busybox rm -f /data/dalvik-cache/*.apk
busybox rm -f /data/dalvik-cache/*.tmp

#Defrags database files

VAC_LOG_FILE=/data/S50-vacuum-databases.log

# do nothing if less than 5 days
find $VAC_LOG_FILE -type f -mtime +5 -exec rm '{}' \;

if ! test -f $VAC_LOG_FILE; then

	touch $VAC_LOG_FILE

	busybox mount -o rw,remount /

	if [ -e $VAC_LOG_FILE ]; then
		rm $VAC_LOG_FILE;
	fi;
    	
	echo "Starting Sqlite Vacuum $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $VAC_LOG_FILE;

	for i in \
	`find /data -iname "*.db"`
	do \
		sqlite3 $i 'VACUUM;'; 
		echo Vacuum $i  | tee -a $VAC_LOG_FILE;
	done;

	for i in \
	`find /dbdata -iname "*.db"`
	do \
		sqlite3 $i 'VACUUM;'; 
		echo Vacuum $i  | tee -a $VAC_LOG_FILE;
	done;

	for i in \
	`find /sdcard -iname "*.db"`
	do \
		sqlite3 $i 'VACUUM;'; 
		echo Vacuum $i  | tee -a $VAC_LOG_FILE;
	done

	echo "Sqlite Vacuum finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $VAC_LOG_FILE;

fi

# zipalign


ZIP_LOG_FILE=/data/log/zipalign.log;
ZIPALIGNED_APK=/data/local/zipalignedapk;

if [ -e $ZIP_LOG_FILE ]; then
	rm $ZIP_LOG_FILE;
fi;

touch $ZIP_LOG_FILE

if [ ! -f $ZIPALIGNED_APK ]; then
	touch $ZIPALIGNED_APK;
fi;

echo "Automatic ZipAlign started at $( date +"%m-%d-%Y %H:%M:%S" ) " | tee -a $ZiP_LOG_FILE;

for DIR in /system/app /data/app; do
	cd $DIR;
	for APK in *.apk; do
		if [ $APK -ot $ZIPALIGNED_APK ] && [ $(grep "$DIR/$APK" $ZIPALIGNED_APK|wc -l) -gt 0 ]; then
			echo "Already checked: $DIR/$APK" | tee -a $ZIP_LOG_FILE;
			if [ -e /data/$APK ]; then
				rm /data/$APK;
				echo "Temporary APK removed: /data/$APK" | tee -a $ZIP_LOG_FILE;
			fi;
		else
			ZIPCHECK=`/system/bin/armzipalign -c -v 4 $APK | grep FAILED | wc -l`;
			if [ $ZIPCHECK == "1" ]; then
				echo "Now aligning: $DIR/$APK" | tee -a $ZIP_LOG_FILE;
				/system/xbin/zipalign -v -f 4 $APK /data/$APK;
				sync
				busybox mount -o rw,remount /system;
				cp -f -p /data/$APK $APK;
				rm /data/$APK;
				if [ -e /data/$APK ]; then
					sleep 2;
					rm /data/$APK;
					if [ -e /data/$APK ]; then
						echo "Failed to remove APK. Run the tweak again : /data/$APK" | tee -a $ZIP_LOG_FILE;
					else
						echo "Temporary APK removed: /data/$APK" | tee -a $ZIP_LOG_FILE;
					fi;
				fi;
				grep "$DIR/$APK" $ZIPALIGNED_APK > /dev/null || echo $DIR/$APK >> $ZIPALIGNED_APK;
			else
				echo "Already aligned: $DIR/$APK" | tee -a $ZIP_LOG_FILE;
				if [ -e /data/$APK ]; then
					rm /data/$APK;
					echo "Temporary APK removed: /data/$APK" | tee -a $ZIP_LOG_FILE;
				fi;
				grep "$DIR/$APK" $ZIPALIGNED_APK > /dev/null || echo $DIR/$APK >> $ZIPALIGNED_APK;
			fi;
		fi;
	done;
done;
sync
busybox mount -o ro,remount /system;
busybox mount -o ro,remount /;
touch $ZIPALIGNED_APK;
echo "Automatic ZipAlign finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $ZIP_LOG_FILE;
