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

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

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

start () {
	#if [ "x$(cat /module_name)" = "xRBR50" ]; then
		## support wifi_schedule 
		prepare_crontab
	#fi
	/usr/sbin/dsyslog 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 
		/www/cgi-bin/firewall_function.sh blk_sched restart
	#fi

	# Start NTP when `time_zone` is reset.
	# check if booting or not.if not booting, run ntpclient 
	if [ ! -f /tmp/boot_status ]; then
		killall ntpclient
		sleep 1
		if [ "$($CONFIG get endis_ntp)" = "1" ]; then
			/usr/sbin/ntpclient
		fi
		# sleep 1 second wait for ntpclient to get time
		sleep 1
	fi

	crond -c $CRONTABS -T "$($CONFIG get time_zone)"
}

stop() {
	killall -9 crond
}

restart() {
	stop
	start
}

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

