#! /bin/sh
#----------------------------------------------------------------------------
# samba-dismount-smbfs - dismount smb filesystem                   3.0.0
#
# Copyright (c) 2005 Thomas Bork, tom(at)fli4l(dot)de
#
# Creation   : 20.07.2002  tb
# Last Update: $Id: samba-dismount-smbfs 10522 2005-12-06 09:07:46Z knuffel $
#
# 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=''
colechobin="/usr/local/bin/colecho"

do_title ()
   {
    clrhome
    $colechobin "Dismount SMB filesystem" gn
    echo
    }

do_title

if [ -z "`grep smbfs /etc/mtab`" ]
then
    $colechobin "There are no SMB filesystems mounted!" br x br
    exit 1
else
    echo "Currently mountpoints of SMB filesystems:"
    current=`grep smbfs /etc/mtab | cut -d" " -f2`
    $colechobin "$current" br x br
    echo
fi

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

do_title

if [ "$mountpoint" = "" ]
then
    $colechobin "No mountpoint specified!" br x br
    exit 1
else
    if [ "`echo $mountpoint | tr 'A-X' 'a-x'`" = "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
              $colechobin "SMB filesystem in $i umounted" gn
          else
              $colechobin "cannot umount SMB filesystem in $i!" br x br
          fi
        done
    else
        if [ "`echo $mountpoint | cut -c1`" != "/" ]
        then
            $colechobin "You must specify an absolut path with a leading '/'!" br x br
            exit 1
        else
            cd /
            for i in $mountpoint
            do
              echo "umounting SMB filesystem in $i ..."
              smbumount $i
 
              if [ "$?" = "0" ]
              then
                  $colechobin "SMB filesystem in $i umounted" gn
              else
                  $colechobin "cannot umount SMB filesystem in $i!" br x br
              fi
            done
        fi
    fi
fi

echo
