#!/bin/sh /etc/rc.common
UCI_CONFIG="schedule_reboot"
TIMEOBJ_CONFIG="time_object"
config_load $UCI_CONFIG

apply() {
	status=`uci get $UCI_CONFIG.profile.status`	
	if [ "$status" = "enable" ]; then
		sed "/Auto Reboot/d" -i /etc/crontabs/root >/dev/null 2>&1
		#parsing required data for cron
		timeobj=`uci get $UCI_CONFIG.profile.timeobj`
		weekdays_check=`uci get time_object.$timeobj.frequency`
		[ "$weekdays_check" = "weekdays" ] && {
			starttime=`uci get $TIMEOBJ_CONFIG.$timeobj.starttime`
			weekdays=`uci get $TIMEOBJ_CONFIG.$timeobj.weekdays`
			hour=`echo $starttime|awk -F":" '{print $1}'`
			min=`echo $starttime|awk -F":" '{print $2}'`
			cron_week=`echo $weekdays|sed 's/Mon/1/g'|sed 's/Tue/2/g'|sed 's/Wed/3/g'|sed 's/Thu/4/g'|sed 's/Fri/5/g'|sed 's/Sat/6/g'|sed 's/Sun/0/g'|awk '{OFS=",";$1=$1;print $0}'`
			#sed "4s/.*/$min $hour * * $cron_week logger \"[schedule] Auto Reboot\";reboot/" -i /etc/crontabs/root >/dev/null 2>&1
			echo "$min $hour * * $cron_week logger \"[schedule] Auto Reboot\";reboot" >>/etc/crontabs/root
		}
	elif [ "$status" = "disable" ]; then
		sed "/Auto Reboot/d" -i /etc/crontabs/root >/dev/null 2>&1
	fi
	uci commit $UCI_CONFIG
	/etc/init.d/cron restart
}