#!/bin/bash

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

delay='1'   # second
timeout='2' # minutes
end_time="$(date -d "now + ${timeout} minutes" +%s)"

nfsd_ready() {
  # the nfsd kernel threads must have been started (nfsdctl autostart / rpc.nfsd)
  [ -r /proc/fs/nfsd/threads ] || return 1
  local threads
  threads="$(cat /proc/fs/nfsd/threads 2>/dev/null)"
  [ -n "$threads" ] && [ "$threads" -gt 0 ] 2>/dev/null || return 2
  return 0
}

until nfsd_ready >/dev/null 2>&1; do
  if [ "$(date +%s)" -gt "$end_time" ]; then
    echo "Service nfsd timed out."
    exit 1
  fi
  sleep "$delay"
done

exit 0
