#!/system/bin/sh

# SQLite database vaccum

# Frequent inserts, updates, and deletes can cause the database file to become fragmented - where data for a single table or index is scattered around the database file.
# Running VACUUM ensures that each table and index is largely stored contiguously within the database file.
# In some cases, VACUUM may also reduce the number of partially filled pages in the database, reducing the size of the database file further.
# sqlite3 binary in /system/xbin is required!

# Changelog
#  v1.0 - (??/??/????) - original version
#  v1.1 - (06/01/2013) - run only every X seconds, default = 1 week (mcbyte_it)

# Log file location
LOG_FILE=/data/sqlite.log

#Interval between SQLite3 runs, in seconds, 604800=1 week
RUN_EVERY=604800

# Get the last modify date of the Log file, if the file does not exist, set value to 0
if [ -e $LOG_FILE ]; then
    LASTRUN=`stat -t $LOG_FILE | awk '{print $14}'`
else
    LASTRUN=0
fi;

# Get current date in epoch format
CURRDATE=`date +%s`

# Check the interval
INTERVAL=$(expr $CURRDATE - $LASTRUN)

# If interval is more than the set one, then run the main script
if [ $INTERVAL -gt $RUN_EVERY ];
then
    if [ -e $LOG_FILE ]; then
        rm $LOG_FILE;
    fi;
        
    echo "SQLite database VACUUM and REINDEX started at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;

    for i in `busybox find /d* -iname "*.db"`; do
        /system/xbin/sqlite3 $i 'VACUUM;';
        resVac=$?
        if [ $resVac == 0 ]; then
            resVac="SUCCESS";
        else
            resVac="ERRCODE-$resVac";
        fi;
        
        /system/xbin/sqlite3 $i 'REINDEX;';
        resIndex=$?
        if [ $resIndex == 0 ]; then
            resIndex="SUCCESS";
        else
            resIndex="ERRCODE-$resIndex";
        fi;
        echo "Database $i:  VACUUM=$resVac  REINDEX=$resIndex" | tee -a $LOG_FILE;
    done
      
    echo "SQLite database VACUUM and REINDEX finished at $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $LOG_FILE;
fi;