# Network shares + ramdisk. Mount points, fstab entries and the "reachable" gate
# services live here (no secrets). Only the credential FILES the CIFS/davfs mounts
# reference (/etc/nase.meow.credentials, /etc/davfs2/secrets) are secret — enter
# them via 04-secrets/run.sh. The mounts just fail until those files exist.

# mount points
for d in /mnt/nase.meow /mnt/files.70b1.de /mnt/ramdisk; do
  [ -d "$d" ] || { mkdir -p "$d" && info "created $d"; }
done

# fstab entries owned by the provisioning user (numeric uid/gid, rebuild-safe).
u="${SUDO_USER:-$(id -un 1000 2>/dev/null || echo root)}"
uid="$(id -u "$u")"; gid="$(id -g "$u")"

add_fstab() {  # add_fstab <mountpoint> <full fstab line>
  grep -qE "[[:space:]]$1[[:space:]]" /etc/fstab && return 0
  printf '%s\n' "$2" >> /etc/fstab && info "fstab += $1"
}
add_fstab /mnt/ramdisk \
  "tmpfs  /mnt/ramdisk  tmpfs  rw,uid=$uid,gid=$gid,nodev,nosuid,size=16G,comment=x-gvfs-show  0 0"
add_fstab /mnt/nase.meow \
  "//nase.meow/data  /mnt/nase.meow  cifs  rw,exec,uid=$uid,gid=$gid,credentials=/etc/nase.meow.credentials,_netdev,x-systemd.requires=nase.meow-ready.service,comment=x-gvfs-show  0 0"
add_fstab /mnt/files.70b1.de \
  "https://files.70b1.de  /mnt/files.70b1.de  davfs  rw,uid=$uid,gid=$gid,_netdev,x-systemd.requires=files.70b1.de-ready.service,comment=x-gvfs-show  0 0"

# "share reachable" gate services (wait until host is up before mounting)
deploy etc/systemd/system/nase.meow-ready.service
deploy etc/systemd/system/files.70b1.de-ready.service
systemctl daemon-reload
enable_unit nase.meow-ready.service
enable_unit files.70b1.de-ready.service

info "NOTE: enter share credentials via 04-secrets/run.sh (mounts fail until then)"
