#!/bin/sh /etc/rc.common
#copyright by sirpdboy

START=95
STOP=10

run_autotimeset()
{
	config_get_bool enable $1 enable
	if [ "x$enable" = "x1" ] ; then
		config_get stype $1 stype
		config_get week $1 week
		config_get minute $1 minute
		config_get hour $1 hour
		[ "$minute" -eq 0 ] && minute="00"
		[ "$week" -eq 7 ] && week="*"

		if [ $stype = 1 ] ; then
			local cmd="$minute $hour * * $week /usr/libexec/timeset/handler reboot" 
			echo "$cmd" >> /etc/crontabs/root
			echo "Auto REBOOT has started.."
		fi
		if [ $stype = 2 ] ; then
			local cmd="$minute $hour * * $week /usr/libexec/timeset/handler poweroff" 
			echo "$cmd" >> /etc/crontabs/root
			echo "Auto poweroff has started.."	
		fi
		if [ $stype = 3 ] ; then
			local cmd="$minute $hour * * $week /usr/libexec/timeset/handler network" 
			echo "$cmd" >> /etc/crontabs/root
			echo "Auto network restart has started."
		fi
    fi
}

start()
{
	del_cru
	[ -n "`tail -c1 /etc/crontabs/root`" ] && echo >> /etc/crontabs/root
	config_load autotimeset
	config_foreach run_autotimeset login
	/etc/init.d/cron reload
}

stop()
{
    del_cru
	/etc/init.d/cron reload
	echo "Auto set has stop."
}

restart()
{
	start
}

del_cru()
{
	sed -i '/ \/usr\/libexec\/timeset\/handler /d' /etc/crontabs/root >/dev/null 2>&1
}

