#!/bin/sh

# Karsten M. Self
# Tue Feb  4 14:57:17 PST 2003

# Create commands to create links to access currently accessible Windows
# shares.  Typically:  create a set of directories corresponding to
# shares, eg, under /smb.  Run this script from within each directory.

export PATH=/usr/bin:/bin

# SMB user & password should be first & second  arguments.

if [ $# -lt 2 ]; then
    echo "Supply legacy MS Windows domain user and pass as arguments"
    exit
fi

user=$1
pass=$2

SERVERS=$( 
    smbclient -U ${user}%${pass} -L 192.168.1.3 |
	sed -e '1,/^[ 	]*Server/d' \
	    -e '/^[ 	]*-----/d' \
	    -e '/^[ 	]*$/,$d'
    )

echo SERVERS: $SERVERS

for SERVER in $SERVERS
do
    echo "# >>> $SERVER <<<"

    SHARES=$(
      smbclient -U ${user}%${pass} -L $SERVER |
        sed -ne '/Sharename/,/^[ 	]*$/{
	  /   *Disk  / {
	    s/^	\([A-z][^ ]\{1,\} \{0,1\}[^ ]\{0,\}\)   *Disk *.*$/\1/p
	  }
	}' 
    )

# Uncomment if you just want to list shares
##     echo "# Shares:" $SHARES
## 
##     for share in "$SHARES"
##     do
##         echo "    $share"
##     done
##     echo -e '\n\n'
##     
## 
##     continue

    for share in $SHARES
    do
        echo "mkdir /smb/"$SERVER"/"$share""
    done

    echo

    for share in $SHARES
    do
        echo -e "mount //$SERVER/$share    \c"
	echo -e "/smb/$SERVER/$share    \c"
	echo -e "smbfs \c"
	echo -e "noauto,credentials=/root/samba_creds,\c"
	echo -e "workgroupd=DOMAIN,gid=smbuser,fmask=660,dmask=770 \c"
	echo -e " 0   0"
    done

    echo -e '\n\n'
done
