#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org
START=70

CRONTABS=/tmp/etc/crontabs
CRON_SPOOL=/var/spool/cron

prepare_crontab()
{
	case "$(uci get opmode.operate.mode)" in
	apmode)
		/sbin/apsched ;;
	brmode)
		echo -n > $CRONTABS/root
		echo -n "root" > $CRONTABS/cron.update
		;;
	*)
		/sbin/cmdsched
		;;
	esac
}

start_stage () {
#	/usr/sbin/dsyslog restart
	/etc/init.d/syslog restart

	mkdir -p $CRON_SPOOL
	[ -d $CRONTABS ] && rm -fr $CRONTABS
	mkdir -p $CRONTABS
	[ -L ${CRON_SPOOL}/crontabs ] || ln -s $CRONTABS ${CRON_SPOOL}/crontabs

	# Generate `Block Sites` && `Block Services` && `E-mail Notification`
	# for scheduling.
	# This `firewall_function.sh` script will call `/sbin/cmdsched`, then
	# re-start firewall setting.
	#if [ "x$(cat /module_name)" = "xRBR50" ]; then
		## support wifi_schedule 
	/etc/init.d/schedule start
	#fi

	# Start NTP when `time_zone` is reset.
	# check if booting or not.if not booting, run ntpclient 
	#if [ "$1" != "boot" ]; then # fix bug OXD-288, ntpclient restart twice, send continuous packet
	#	killall ntpclient
	#	sleep 1
	#	if [ "$(uci get system.ntp.enable)" = "1" ]; then
	#		/etc/init.d/ntpclient start
	#	fi
	#	# sleep 1 second wait for ntpclient to get time
	#	sleep 1
	#fi

#	crond -c $CRONTABS -T "$(uci get system.systemtimezone.timezone)"
#   We have set TZ to system env in /etc/TZ, so -T param is uncenessary.
	crond -c $CRONTABS
}
start () {
    start_stage start
}

stop() {
	killall -9 crond
}

restart() {
	stop
	start
}

boot () {
	mkdir -p $CRONTABS
	mkdir -p $CRON_SPOOL
	ln -s $CRONTABS ${CRON_SPOOL}/crontabs
	start_stage boot
}

