#!/bin/sh
#
# mount 9p.2000u resource sharing
#
# runlevels: geexbox, debug, configure

if test -f /etc/9p -a -f /etc/network; then
  echo "### Mounting 9P shares ###"
  . /etc/network
  (
    while [ ! -f /var/ifup ]; do
      sleep 1
    done 

    # Mount 9P.2000u
    while true; do
      IFS="\n"
      for MOUNTS in $(grep -v "^#" /etc/9p | grep -v "^$"); do

          SRV="$(echo $MOUNTS | sed 's/\(.*\):.*:.*:.* .*/\1/')"
        PPORT="$(echo $MOUNTS | sed 's/.*:\(.*\):.*:.* .*/\1/')"
        NNAME="$(echo $MOUNTS | sed 's/.*:.*:\(.*\):.* .*/\1/')"
        ANAME="$(echo $MOUNTS | sed 's/.*:.*:.*:\(.*\) .*/\1/')"
          DIR="$(echo $MOUNTS | sed 's/.*:.*:.*:.* \(.*\)/\1/')"

      if [ ! -e "/mnt/9p/$DIR" ]; then
          mkdir -p "/mnt/9p/$DIR"
          mount "$SRV" -t 9p -o ro,port=$PPORT,uname=$NNAME,aname=$ANAME "/mnt/9p/$DIR" >/dev/null 2>&1 || rmdir -p "/mnt/9p/$DIR"
        fi
      done
      [ -z "$NET_RESCAN_DELAY" -o $NET_RESCAN_DELAY -le 0 ] && break
      sleep $NET_RESCAN_DELAY
    done
  )&
fi

exit 0
