#!/bin/bash # Samba Browser Script # Written by David Elentok. 09/01/04 17:02 # # Arguments: # -r = reload (regenerates the share tree) # -mc = midnight commander mode (doesn't open an mc terminal after mounting) tempfile="$(tempfile)" treefile="$HOME/var/smbtree" lastfile="$HOME/var/last_smb_mount" mc_mode=0 if [ "$1" == "-r" -o ! -e "$treefile" ]; then TREE="$(smbtree)" echo "$TREE" > $treefile shift else TREE="$(cat $treefile)" fi if [ "$1" == "-mc" ]; then mc_mode=1 fi data=$(echo "$TREE" | grep ' ' | sed "s# ##g" | sed "s/[ ]/;/" | grep -v "^\\\\.[^\\]*;" | grep -v '\\IPC\$' | grep -v '\\ADMIN\$') length=$(echo "$data" | grep -c $) MENU="" for x in $(seq $length) do line=$(echo "$data" | sed -n ${x}p) share=$(echo $line | cut -d\; -f1) desc=$(echo $line | cut -d\; -f2) if [ -n "$desc" ]; then desc=" $desc"; fi share_check=$(echo $share | sed 's#\\#/#g') if [ -n "$(mount | grep $share_check)" ]; then stats="(*)" else stats="( )" fi MENU="$MENU '$share' '$stats$desc'" # echo "$MENU" done # read -n1 echo $MENU eval "dialog --menu 'Samba Browser' 30 80 $length $MENU" 2> $tempfile choice=$(cat $tempfile) rm $tempfile if [ -z "$choice" ]; then exit 0 fi clear share="$(echo $choice | gawk -F'\' '{print $NF}')" name="$(echo $choice | gawk -F'\' '{print $(NF-1)}')" dir="$HOME/mnt/$name/$share" # ALREADY MOUNTED -------------------------------------------------------------- if [ -n "$(mount | grep $dir)" ]; then # NOT MC-MODE: unmount ------------------------------------------------------- if [ $mc_mode -eq 0 ]; then echo "Unmounting [$dir]... " smbumount $dir exit $? # MC-MODE: just change directory --------------------------------------------- else echo $dir > "$lastfile" exit 0 fi fi # NOT MOUNTED ------------------------------------------------------------------ # Create Directories if [ ! -d "$HOME/mnt/$name" ]; then mkdir "$HOME/mnt/$name"; fi if [ ! -d "$dir" ]; then mkdir "$dir"; fi # Mount echo "Mounting [$choice] at [$dir]" smbmount $choice $dir # IF Mounted ------------------------------------------------------------------- if [ $? -eq 0 ]; then if [ $mc_mode -eq 0 ]; then exec mc $dir else echo $dir > "$lastfile" exit 0 fi # IF NOT ----------------------------------------------------------------------- else rm "$lastfile" exit 1 fi