#!/sbin/openrc-run
# SPDX-License-Identifier: MIT

name="Start podman containers"
description="Start podman containers configured in /etc/atmark/containers"

depend() {
	need sysfs cgroups localmount
	# we need to use 'need' for stop to wait until containers
	# stopped to stop iptables, but we don't want to start services
	# if not configured...
	if [ -e /etc/iptables/rules-save ]; then
		 need iptables
	fi
	if [ -e /etc/iptables/rules6-save ]; then
		need ip6tables
	fi
}

start() {
	local msg="Starting configured podman containers"

	ebegin "$msg"

	# print some characters to tty1 (fb console) to check easily
	# if the display is working.  this should be done before
	# starting any containers that will use the display.
	if [ -z "$disable_tty1_message" ]; then
		echo "$msg" > /dev/tty1
	fi

	podman_start -a
	eend $? "Could not start all containers"
}

stop() {
	ebegin "Stopping all podman containers"

	# stop all containers. sigkill will be sent after 5s.
	podman stop -t 5 -a >/dev/null

	# wait up to 5s for stop to have really killed containers
	podman ps -a --format '{{.ID}}' \
		| timeout 5s xargs -r podman wait >/dev/null 2>&1

	podman pod rm -a

	# don't remove networks as users could have created these manually
	# in /etc/cni/net.d...

	eend 0
}
