#!/usr/bin/env /lib/runit/invoke-run
#Copyright: 2026 Aitor <aitor@genuen.org>
#License: CC0-1.0
#Comment: ported for runit-services testing (zfs-fuse)

exec 2>&1
ENABLE_ZFS=yes
DAEMON_OPTS=""
[ -f /etc/default/zfs-fuse ] && . /etc/default/zfs-fuse
[ "x$ENABLE_ZFS" = "xyes" ] || exit 161
[ -x /usr/sbin/zfs-fuse ] || exit 162

# migrate old cache location if needed
oldcache=/etc/zfs/zpool.cache
newcache=/var/lib/zfs/zpool.cache
if [ -f "$oldcache" ] && [ ! -e "$newcache" ]; then
  mkdir -p "$(dirname "$newcache")"
  mv "$oldcache" "$newcache"
fi

ulimit -v unlimited
ulimit -c 512000
ulimit -l unlimited
ulimit -s unlimited
unset LANG

(
  # Mount/share once the daemon is accepting commands
  for i in $(seq 1 30); do
    if zpool list >/dev/null 2>&1 || zfs list >/dev/null 2>&1; then
      zfs mount -a || true
      zfs share -a || true
      exit 0
    fi
    sleep 0.5
  done
) &

exec /usr/sbin/zfs-fuse --no-daemon $DAEMON_OPTS
