#!/sbin/openrc-run

name="tgtd"
pidfile="/run/$RC_SVCNAME.pid"
command="/usr/sbin/tgtd"
command_args_background="--pid-file ${pidfile}"

depend() {
	need dev localmount
}

start_post() {
	# We start tgtd, but leave in "offline" mode until
	# targets are configured.
	sleep 1
	tgtadm --op update --mode sys --name State -v offline
}

stop() {
	# Set tgtd to "offline" mode.
	tgtadm --op update --mode sys --name State -v offline

	# Remove system node. This will fail if
	# not all targets have been removed first.
	tgtadm --op delete --mode system
	retval=$?
	if [ $retval -ne 0 ]; then
		eerror "Failed to shutdown tgtd"
		eend $retval
		exit $retval
	fi
}

status_post() {
	einfo "Run 'tgtadm --mode sys --op show' for tgtd status"
	einfo "Run 'tgtadm --mode target --op show' for target status"
}
