#!/sbin/openrc-run
#
# OpenRC init script for the cron daemon.
#

description="Regular background program processing daemon"

# Do not reset PATH: openrc-run prepends /usr/lib/rc/bin (ebegin, mountinfo, …).
command=/usr/sbin/cron
pidfile=/var/run/crond.pid
command_background="yes"

[ -r /etc/default/cron ] && . /etc/default/cron

command_args="-f ${EXTRA_OPTS:-}"

# Read the system's locale and set cron's locale. This is only used for
# setting the charset of mails generated by cron. To provide locale
# information to tasks running under cron, see /etc/pam.d/cron.
parse_environment()
{
	for ENV_FILE in /etc/environment /etc/default/locale; do
		[ -r "$ENV_FILE" ] || continue
		[ -s "$ENV_FILE" ] || continue

		for var in LANG LANGUAGE LC_ALL LC_CTYPE; do
			value=$(grep "^${var}=" "$ENV_FILE" | tail -n1 | cut -d= -f2)
			[ -n "$value" ] && eval export $var=$value

			if [ -n "$value" ] && [ "$ENV_FILE" = /etc/environment ]; then
				ewarn "/etc/environment has been deprecated for locale information; use /etc/default/locale for $var=$value instead"
			fi
		done
	done

	if [ -z "$TZ" ] && [ -e /etc/localtime ]; then
		TZ=$(readlink /etc/localtime | sed "s|/usr/share/zoneinfo/||")
		export TZ
	fi
}

depend() {
	need net localmount
	use dns logger
	after clock bootmisc
}

start_pre() {
	[ -f "$command" ] || return 0
	if [ "$READ_ENV" = "yes" ]; then
		parse_environment
	fi
}

stop() {
	ebegin "Stopping periodic command scheduler"
	start-stop-daemon --stop --quiet --pidfile "$pidfile" --exec "$command" --retry=TERM/30/KILL/5
	RETVAL=$?
	[ $RETVAL -eq 0 ] && [ -e "$pidfile" ] && rm -f "$pidfile"
	eend $RETVAL
}

reload() {
	# cron reloads automatically
	:
}
