cat <<"EOF">>/etc/login.conf

cachefs:\
        :openfiles=2048:\
        :tc=daemon:
EOF
groupadd _cachefs
useradd -d /var/empty -L cachefs -g _cachefs _cachefs

cat <<"EOF">/etc/rc.d/cachefs
#!/bin/sh

daemon="/usr/local/sbin/cachefs"
daemon_flags="-src file:///mnt/nfs -dst file:///mnt/cache -data /var/cachefs/md.json -listen :9090 -webdav :9091 -cache :9092 -quota 10"
daemon_user="_cachefs"

. /etc/rc.d/rc.subr

pexp=".*${daemon}.*"
rc_bg=YES
rc_reload=NO

rc_start() {
  ${rcexec} "${daemon} ${daemon_flags} < /dev/null 2>&1 | \
      logger -ip daemon.info -t cachefs"
}

rc_cmd $1
EOF
chmod 555 /etc/rc.d/cachefs

mkdir -p /mnt/cache /var/cachefs

chown _cachefs:_cachefs /mnt/cache /var/cachefs

rcctl enable cachefs
rcctl start cachefs
