#!/bin/sh
#
# mount samba shares
#
# runlevels: geexbox, debug

if test -x /usr/bin/smbmount -a -f /etc/network; then
  echo "### Mounting Samba shares ###"
  (
    . /etc/network
    while [ ! -f /var/ifup ]; do
      sleep 1
    done
    OPT="-N"
    test -n "$SMB_USER" && OPT="-U$SMB_USER%$SMB_PWD"
    saveifs=$IFS
    while true; do
      smbtree "$OPT" | while read mounts; do
      (
        IFS=/
        set $mounts
        name=$1
        ip=$2
        shift 2
        while [ $# -gt 0 ]; do
          dir="/mnt/shares/$name/$1"
          if [ ! -e "$dir" ]; then
            mkdir -p "$dir"
            mount "//$ip/$1" "$dir" -t cifs -o "ro,iocharset=utf8,user=$SMB_USER,pass=$SMB_PWD" || smbmount "//$name/$1" "$dir" -o "ro,iocharset=utf8,ip=$ip,username=$SMB_USER,passwd=$SMB_PWD" || rmdir -p "$dir"
          fi
          shift
        done
        IFS=$saveifs
      )&
      done
      sleep 180
    done
  )>/dev/null 2>&1 &
fi

exit 0
