#!/bin/sh /etc/rc.common

USE_PROCD=1

# default_prerm stops every init script owned by the package being upgraded.
# Exit before rc.common's procd_kill: this service owns the active installer,
# whose journal remains authoritative until completion or explicit recovery.
case "$action" in
	start)
		# default_postinst also starts owned services. Do not submit a fresh
		# procd definition while the previous worker is installing this package.
		if [ -x /var/run/smart-srun/update-worker ]; then
			/var/run/smart-srun/update-worker _update-worker-active && exit 0
		fi
		;;
	stop|restart|shutdown)
		[ -s /etc/smart-srun/recovery/update.json ] && exit 0
		;;
	enable|disable|boot)
		exit 0
		;;
esac

# Invoked only for an explicitly queued task. No boot auto-install, respawn or
# shared main-service instance: an interrupted install needs explicit recovery.
start_service() {
	[ -s /etc/smart-srun/recovery/update.json ] || return 1
	[ -x /var/run/smart-srun/update-worker ] || return 1
	procd_open_instance instance1
	procd_set_param command /var/run/smart-srun/update-worker _update-worker
	procd_set_param term_timeout 86400
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}
