[Main]
Type = oneshot
Version = 0.1.0
Description = "anacron oneshot"
User = ( root )
Depends = ( mount-rw )
Options = ( !log )

[Start]
Execute = (
	# Mirror debian/init.d start (OpenRC): run anacron once to catch up.
	
	ifelse { test -x /usr/sbin/anacron } {
		envfile -I /etc/default/anacron
		importas -uD "no" ANACRON_RUN_ON_BATTERY_POWER ANACRON_RUN_ON_BATTERY_POWER
		importas -sCuD "-s" ANACRON_ARGS ANACRON_ARGS
	
		ifelse { test "${ANACRON_RUN_ON_BATTERY_POWER}" = yes } {
			exec /usr/sbin/anacron ${ANACRON_ARGS}
		}
	
		ifelse { test -x /usr/bin/on_ac_power } {
			# on_ac_power: 0 = AC, 1 = battery
			ifelse { /usr/bin/on_ac_power } {
				exec /usr/sbin/anacron ${ANACRON_ARGS}
			} {
				exit 0
			}
		}
	
		exec /usr/sbin/anacron ${ANACRON_ARGS}
	}
	
	exit 0
)

[Stop]
Execute = (
	# Soft-stop if anacron is still running (OpenRC stop semantics).
	
	foreground {
		if { pidof -x /usr/sbin/anacron }
		foreground {
			backtick -n pids { pidof -x /usr/sbin/anacron }
			importas -sCu pids pids
			kill -USR1 ${pids}
		}
		foreground { sleep 1 }
		backtick -n pids { pidof -x /usr/sbin/anacron }
		importas -sCuD "" pids pids
		ifelse { test -n "${pids}" } { kill -TERM ${pids} }
		exit 0
	}
	exit 0
)
