#!/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

symlink_system_bin() {
    # crond has "/bin/sh" hardcoded
    if busybox [ ! -h /bin ]; then
        mount -o remount,rw rootfs /;
        busybox ln -s /system/bin /bin;
        mount -o remount,ro rootfs /;
    fi;
}

export_timezone() {
    # set timezone (if you're not between -0500 and -0800 you get PST)
    # todo - support other timezones
    timezone=`date +%z`;
    if busybox [ $timezone = "-0800" ]; then
        TZ=PST8PDT;
    elif busybox [ $timezone = "-0700" ]; then
        TZ=MST7MDT;
    elif busybox [ $timezone = "-0600" ]; then
        TZ=CST6CDT;
    elif busybox [ $timezone = "-0500" ]; then
        TZ=EST5EDT;
    else
        TZ=PST8PDT;
    fi;
    export TZ;
}

set_crontab() {
    # use /data/cron, call the crontab file "root"
    if busybox [ -e /data/cron/root ]; then
        mkdir -p /data/cron;
        cat >> /data/cron/root << EOF
01 * * * * busybox run-parts /system/etc/cron/cron.hourly
02 4 * * * busybox run-parts /system/etc/cron/cron.daily
22 4 * * 0 busybox run-parts /system/etc/cron/cron.weekly
EOF
    fi;
    busybox crond -c /data/cron;
}

if $enable_cron -a is_busybox_applet_available crond; then
    symlink_system_bin;
    export_timezone;
    set_crontab;
    echo "* Cron Starting At $( date +"%m-%d-%Y %H:%M:%S" )" | tee -a $paclog;
fi;
