#!/system/bin/sh
: '
 ========= Copyright (C) 2014 The PAC-man Team (The AIO Rom) =========

 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/>.

 =====================================================================
'

. /data/local/init.d.cfg

LOG="/data/PACLogs/wifisleep.log"

if $wifisleep; then
    if [ -e $LOG ]; then
        rm $LOG;
    fi;
    time=10000;
    sql=/system/xbin/sqlite3;
    old=$($sql /data/data/com.android.providers.settings/databases/settings.db "select value from secure where name='wifi_idle_ms'");

    echo "$( date +"%m-%d-%Y %H:%M:%S" ) DeepSleep tweak started" | tee -a $LOG;
    echo "Old wifi_idle_ms value was $old" | tee -a $LOG;

    if [ $old="" ]; then
        $sql /data/data/com.android.providers.settings/databases/settings.db "insert into secure (name, value) values ('wifi_idle_ms', $time )";
    fi;

    $sql /data/data/com.android.providers.settings/databases/settings.db "update secure set value=$time where name='wifi_idle_ms'";
    new=$($sql /data/data/com.android.providers.settings/databases/settings.db "select value from secure where name='wifi_idle_ms'");

    echo "Current wifi_idle_ms value is $new" | tee -a $LOG;
    echo "$( date +"%m-%d-%Y %H:%M:%S" ) DeepSleep tweak ended" | tee -a $LOG;
    echo "* Wifi Sleep Tweaks Starting At $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $paclog;
fi;
