#!/bin/sh /etc/rc.common

#START=80

start() {
    local timing_type=`uci get auto_backup.schedule.timing_type`
    local hour=`uci get auto_backup.schedule.hour`
    local minute=`uci get auto_backup.schedule.minute`

    local enable=`uci get auto_backup.schedule.enable`
    crontab -l > /tmp/crontab_tmp
    sed -i '/auto_backup_execute/d' /tmp/crontab_tmp
    if [ $enable == "off" ]; then
        crontab /tmp/crontab_tmp
        return 0
    fi

    case $timing_type in
    "daily")
        echo "${minute} ${hour} * * * /bin/sh /usr/sbin/auto_backup_execute" >> /tmp/crontab_tmp
        ;;
    "weekly")
        day_of_week=`uci get auto_backup.schedule.day_of_week`
        day_of_week=`lua -e "print(require 'luci.controller.admin.auto_backup'.DAY_TBL.$day_of_week)"`
        echo "${minute} ${hour} * * ${day_of_week} /bin/sh /usr/sbin/auto_backup_execute" >> /tmp/crontab_tmp
        ;;
    "monthly")
        day_of_month=`uci get auto_backup.schedule.day_of_month`
        echo "${minute} ${hour} ${day_of_month} * * /bin/sh /usr/sbin/auto_backup_execute" >> /tmp/crontab_tmp
        ;;
    "yearly")
        day_of_month=`uci get auto_backup.schedule.day_of_month`
        month_of_year=`uci get auto_backup.schedule.month_of_year`
        month_of_year=`lua -e "print(require 'luci.controller.admin.auto_backup'.MON_TBL.$month_of_year)"`
        echo "${minute} ${hour} ${day_of_month} ${month_of_year} * /bin/sh /usr/sbin/auto_backup_execute" >> /tmp/crontab_tmp
        ;;
    *)
        ;;
    esac
    crontab /tmp/crontab_tmp
    return 0
}

stop() {
    return 0
}

reload() {
    return 0
}

