[Main]
Type = classic
Version = 0.1.0
Description = "zfs-fuse daemon"
User = ( root )
Depends = ( mount-rw )
Options = ( log )

[Start]
Execute = (
	# zfs-fuse in foreground (--no-daemon), matching debian/sv/zfs-fuse.
	# Pause unless ENABLE_ZFS=yes in /etc/default/zfs-fuse (runit exits 161).
	
	envfile -I /etc/default/zfs-fuse
	importas -uD "yes" ENABLE_ZFS ENABLE_ZFS
	importas -sCuD "" DAEMON_OPTS DAEMON_OPTS
	
	ifelse { test "${ENABLE_ZFS}" = yes } {
		# Migrate old cache location if needed (same as OpenRC/runit).
		foreground {
			ifelse { test -f /etc/zfs/zpool.cache } {
				ifelse { test ! -e /var/lib/zfs/zpool.cache } {
					foreground { mkdir -p /var/lib/zfs }
					mv /etc/zfs/zpool.cache /var/lib/zfs/zpool.cache
				}
				exit 0
			}
			exit 0
		}
		# Mount/share once the daemon accepts commands (runit background waiter).
		background {
			forx i { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 }
			ifelse { zpool list } {
				foreground { zfs mount -a }
				foreground { zfs share -a }
				exit 0
			}
			ifelse { zfs list } {
				foreground { zfs mount -a }
				foreground { zfs share -a }
				exit 0
			}
			foreground { sleep 1 }
		}
		unexport LANG
		s6-softlimit -v 0 -c 512000 -l 0 -s 0
		exec /usr/sbin/zfs-fuse ${Args} ${DAEMON_OPTS}
	}
	s6-pause
)

[Logger]
RunAs = s6log
Backup = 3
MaxSize = 2000000
Timestamp = tai
Destination = /var/log/zfs-fuse-s6

[Environment]
Args = !--no-daemon
