#!/bin/sh
#----------------------------------------------------------------------------
# samba-dismount-smbfs - dismount smb filesystem
#
# Copyright (c) 2001-2003 Thomas Bork, tom(at)fli4l(dot)de
#
# Creation:     20.07.2002  tb
# Last Update:  $Id: samba-dismount-smbfs,v 1.2 2003/09/27 13:58:53 knuffel Exp $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

# set -x

mountpoint=''

clrhome
colecho "Dismount SMB Filesystem" gn
echo
	    
if [ -z "`grep smbfs /etc/mtab`" ]
then
    echo
    colecho "There are no SMB Filesystems mounted!" br x br
    echo
    exit 1
else
    echo "Currently Mountpoints of SMBFS Filesystems:"
    echo
    current=`grep smbfs /etc/mtab | cut -d" " -f2`
    colecho "$current" br x br
    echo
fi

if [ "$mountpoint" = "" ]
then
    echo -e "Which Mountpoint do you want to dismount?"
    echo
    echo -e "Choice 1"
    echo -e "--------"
    echo -e "All mounted SMB Filesystems on all Mountpoints - type 'all'."
    echo
    echo -e "Choice 2"
    echo -e "--------"
    echo -e "One ore more Mountpoints, type in exact Names"
    echo -e "separated with a blank, for instance '/mountpoint1 /mountpoint2':"
    echo
    read mountpoint
fi

clrhome
colecho "Dismount SMB Filesystem" gn
echo

if [ "$mountpoint" = "" ]
then
    echo
    colecho "No Mountpoint specified!" br x br
    echo
    exit 1
else
    if [ "$mountpoint" = "all" -o "$mountpoint" = "All" -o "$mountpoint" = "ALL" ]
    then
        cd /
        for i in `grep smbfs /etc/mtab | cut -d" " -f2`
        do
                  echo "umounting smb filesystem in $i ..."
                  smbumount $i
                  if [ "$?" = "0" ]
                  then
                      colecho "smb filesystem in $i umounted" gn
                  else
                      colecho "cannot umount smbfs filesystem in $i!" br x br
                  fi
        done
    else
        if [ "`echo $mountpoint | cut -c1`" != "/" ]
        then
            echo
            colecho "You must specify an absolut Path with a leading '/'!" br x br
            echo
            exit 1
	else
            cd /
            for i in $mountpoint
	    do
                  echo
                  echo "umounting smbfs filesystem in $i ..."
                  smbumount $i
                  if [ "$?" = "0" ]
                  then
                      colecho "smbfs filesystem in $i umounted" gn
                  else
                      colecho "cannot umount smbfs filesystem in $i!" br x br
		  fi
            done
	fi
    fi
fi
echo
