#!/bin/sh

# copyright 2004 vagrant@freegeek.org, distributed under the terms of the
# GNU General Public License version 2 or any later version.

if [ -r /etc/lessdisks/mkinitrd/initrd-netboot.conf ]; then
  . /etc/lessdisks/mkinitrd/initrd-netboot.conf
fi

if [ -r "$cmdline_opts" ]; then
  . $cmdline_opts
fi

# /dev/nfs should be the rootdevice, otherwise exit...
if [ "/dev/nfs" != "$rootdev" ]; then
  echo "skipping NFS Root script..."
  exit 0
fi

# TODO support udhcpc style versions of tftp-server-name
server="$siaddr"
if [ -z "$server" ] && [ -n "$tftp" ]; then
  echo "WARNING: assuming TFTP server $tftp is also NFS server..."
  server="$tftp"
elif [ -z "$server" ] && [ -n "$serverid" ]; then
  echo "WARNING: assuming DHCP server $serverid is also NFS server..."
  server="$serverid"
fi

if [ -z "$server" ]; then
  echo "no NFS server defined, exiting.."
  exit 1
fi

nfsroot="$nfspath"
if [ -z "$fs_type" ]; then
  fs_type=nfs
fi

if [ -z "$nfs_opts" ]; then
  fs_options=ro,async,nolock
else
  fs_options="$nfs_opts"
fi

echo "attempting to mount NFS filesystem..."

modprobe nfs

# stupid hack to remove trailing whitespace
for s in $server; do
  server=$s
done

command="mount -nt $fs_type -o $fs_options $server:$nfsroot /mnt"
echo "$command"
$command
status="$?"
if [ "0" != "$status" ]; then
  # call a shell up for debugging, since we couldn't mount NFS root...
  /bin/sh
fi

chroot /mnt mount -nt devfs devfs dev
if [ -z "$LESSDISKS_DEBUG" ]; then
  umount /tmp
fi
