#!/system/bin/sh
#script by Pikachu01 and improved by Slaid480 for Fly-On Mod !
#Optimize SQlite databases of apps

#============ Copyright (C) 2013 Salah Abouabdallah(Slaid480)===========#
 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
 
#  You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
#=======================================================================#

FLY=/data/Fly-On/07sqlite_optimize.log

busybox rm -f $FLY
busybox touch $FLY

echo "# Fly-On Mod LOGGING ENGINE" | tee -a $FLY
echo "" | tee -a $FLY
echo "$( date +"%m-%d-%Y %H:%M:%S" ) iptimizing and defragging your database files,please wait..." | tee -a $FLY;

#Interval between Optimize SQlite runs, in seconds, 108000=30 hours
RUN_EVERY=108000


# echo "";
echo "*********************************************";
echo "Optimizing and defragging your database files (*.db)";
echo "Ignore the 'database disk image is malformed' error";
echo "Ignore the 'no such collation sequence' error";
echo "*********************************************";
echo "";

for i in \
`busybox find /data -iname "*.db"`;
do \
  /system/xbin/sqlite3 $i 'VACUUM;';
  /system/xbin/sqlite3 $i 'REINDEX;';
done;

if [ -d "/dbdata" ]; then
  for i in \
  `busybox find /dbdata -iname "*.db"`;
  do \
    /system/xbin/sqlite3 $i 'VACUUM;';
    /system/xbin/sqlite3 $i 'REINDEX;';
  done;
fi;


if [ -d "/datadata" ]; then
  for i in \
  `busybox find /datadata -iname "*.db"`;
  do \
    /system/xbin/sqlite3 $i 'VACUUM;';
    /system/xbin/sqlite3 $i 'REINDEX;';
  done;
fi;


for i in \
`busybox find /sdcard -iname "*.db"`;
do \
  /system/xbin/sqlite3 $i 'VACUUM;';
  /system/xbin/sqlite3 $i 'REINDEX;';
done;

echo "" | tee -a $FLY
echo "$( date +"%m-%d-%Y %H:%M:%S" ) database files optimization done!" | tee -a $FLY;