#!/usr/bin/env bash

# mount nfs share on linux
# sudo nala install nfs-common
# mkdir ~/data/local
# sudo mount -o nolock,noatime,nodiratime,proto=tcp,timeo=600,retrans=2,noac alex-p51s-5:/home/alex/data/local ./data/local

uv run --python 3.14 --with "machineconfig>=7.32" python -m machineconfig.scripts.python.mount_nfs
# Check if remote server is reachable and share folder exists
if ! ping -c 1 "$remote_server" &> /dev/null; then
  echo "💥 Error: Remote server $remote_server is not reachable."
  exit 1
fi

#if ! showmount -e "$remote_server" | grep -q "$share_path"; then
#  echo "Error: NFS share folder $share_path is not available on $remote_server."
#  exit 1
#fi

# Check if the share is already mounted
if mount | grep -q ":$share_path on "; then
  echo "⚠️ The NFS share $share_info is already mounted."
  exit 0
fi


if [ ! -d "$local_mount_point" ]; then
  echo "➡️ Creating local mount point directory $local_mount_point"
  sudo mkdir -p "$local_mount_point"
  sudo chown "$(id -u):$(id -g)" "$local_mount_point"
fi

sudo service rpcbind start
sudo service nfs-common start
# without the two above, on wsl, you get error: https://superuser.com/questions/657071/mount-nfs-rpc-statd-is-not-running-but-is-required-for-remote-locking

echo "💡 Mounting NFS share $share_info at $local_mount_point using command:"
echo "sudo mount -t nfs $share_info $local_mount_point"
sudo mount -t nfs "$share_info" "$local_mount_point"

# Add a line to fstab file to ensure automount
fstab_line="$remote_server:$share_path $local_mount_point nfs defaults 0 0"
if grep -qF "$fstab_line" /etc/fstab; then
  echo "🤷‍♂️ The following line already exists in /etc/fstab:"
  echo "$fstab_line"
else
  echo "➡️ Adding the following line to /etc/fstab:"
  echo "$fstab_line"
  echo "$fstab_line" | sudo tee -a /etc/fstab > /dev/null
fi
