#!/usr/bin/openrc-run

. /opt/etc/functions.openrc

depend()
{
	use modules mtab
	provide ${SVCNAME}
}

start()
{
	local _pool_altroot _pool _altroot
	ebegin "Starting \"${SVCNAME}\""
	# Delay until all required block devices are present.
	eindent
	update_me
	einfo "${ME}: calling udevadm settle -t 30 ..."
	udevadm settle -t 30
	update_me
	einfo "${ME}: udevadm returned."
	# Import all pools listed in config file
	for _pool_altroot in ${ZFS_POOLS}; do
		update_me
		# 2016-07-29: pool format now is pool[:altroot]
		case ${_pool_altroot} in
			*:*)
				_pool=$(echo ${_pool_altroot} | sed -e 's/\:.*//')
				_altroot="-R $(echo ${_pool_altroot} | sed -e 's/.*\://')"
				;;
			*)
				_pool=${_pool_altroot}
				_altroot=""
				;;
		esac
		if (zpool list -H | grep ^${_pool} > /dev/null 2>&1); then
			ewarn "${ME}: pool ${_pool} is already imported"
		else
			zpool import -d /dev/disk/by-partlabel -f ${_altroot} ${_pool} \
				&& einfo "${ME}: imported pool ${_pool}" \
				|| ewarn "${ME}: unable to import pool ${_pool}"
		fi
	done
	# Mount all filesystems on the pool(s)
	zfs mount -a > /dev/null 2>&1
	eoutdent
	eend 0
	return 0
}

stop()
{
	return 0
}
