#!/bin/sh
#----------------------------------------------------------------------------
# samba-mount-smbfs  - mount smb filesystem
#
# Copyright (c) 2001-2003 Thomas Bork, tom(at)fli4l(dot)de
#
# Creation:     04.11.2001  tb
# Last Update:  $Id: samba-mount-smbfs,v 1.2 2003/09/27 14:00:38 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

ask ()
{
    while [ 1 ]
    do
        echo -e "$1 (y/n)? \c"
        read a

        case "$a"
        in
            y | yes)    a=y; break;;
            n | no)     a=n; break;;
            *)
              colecho "Please answer y(es) or n(o)!" br x br
              ;;
        esac
    done
}

clrhome
colecho "Mount SMB Filesystems" gn
echo
	    
if [ -z "`lsmod | grep smbfs`" ]
then
    echo
    colecho "Start Samba first!" br x br
    echo
    exit 1
fi

if [ "$smbserver" = "" ]
then
    echo -e "From which Server do you want to mount a share?"
    echo
    echo -e "This could be a Windows or Linux Host with an active Share."
    echo -e "Type in the NETBIOS Name of the Host,"
    echo -e "for instance 'client1':"
    echo
    read smbserver
fi
	
if [ "$smbserver" = "" ]
then
    echo
    colecho "No SMB Server specified!" br x br
    echo
    exit 1
else
    if [ -n "`nmblookup $smbserver | grep \"name_query failed to find name $smbserver\"`" ]
    then
        echo
        colecho "Cannot find NETBIOS Name $smbserver!" br x br
        echo
        exit 1
    fi
fi

clrhome
colecho "Mount SMB Filesystems" gn
echo
	    
if [ "$sharename" = "" ]
then
    echo
    echo -e "What is the Name of the Share?"
    echo
    echo -e "Type in the Name of the Share,"
    echo -e "for instance 'Freigabe':"
    echo
    read sharename
fi
	
if [ "$sharename" = "" ]
then
    echo
    colecho "No Share Name specified!" br x br
    echo
    exit 1
fi

clrhome
colecho "Mount SMB Filesystems" gn
echo
	    
if [ "$mountpoint" = "" ]
then
    echo
    echo -e "Where do you want to mount the Share?"
    echo
    echo -e "This must be a absolut Path! If the specified"
    echo -e "Directory does not exists, it will be created for you."
    echo -e "If you are using a existing Directory, it must be empty!"
    echo -e "Type the absolut Path to a directory,"
    echo -e "for instance '/mountpoint':"
    echo
    read mountpoint
fi
	
if [ "$mountpoint" = "" ]
then
    echo
    colecho "No Mountpoint specified!" br x br
    echo
    exit 1
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    
        mkdir -p $mountpoint
        if [ "$?" != "0" ]
        then
            echo
            colecho "Cannot create Mountpoint!" br x br
            echo
            exit 1
        else
            idx='0'
            for i in `ls $mountpoint`
            do
                      idx=`expr $idx + 1`
            done

            if [ "$idx" != "0" ]
            then
                echo
                colecho "Mountpoint is not empty, use other mountpoint!" br x br
                if [ -n "`grep "$mountpoint smbfs  0 0" /etc/mtab`" ]
                then
                    x=`grep "$mountpoint smbfs  0 0" /etc/mtab | cut -d" " -f1`
                    y=`grep "$mountpoint smbfs  0 0" /etc/mtab | cut -d" " -f2`
                    if [ "$y" = "$mountpoint" ]
                    then
                        colecho "There is already "$x" mounted on "$y"!" br x br
                        echo
                        ask "Do you want to dismount "$x" from "$y""

                        if [ "$a" = "y" ]
                        then
                            echo "umounting smbfs filesystem in $y ..."
                            cd /
                            smbumount $y
                            if [ "$?" = "0" ]
                            then
                                echo "smbfs filesystem in $y umounted"
                                sleep 2
                            else
                                colecho "cannot umount smbfs filesystem in $y!" br x br
                                echo
                                exit 1
                            fi
                        else
                            echo
                            exit 1
                        fi
                    fi

                fi
                echo
#                exit 1
             fi
        fi
    fi
fi

if [ "$smbserver" != "" -a "$sharename" != "" -a "$mountpoint" != "" ]
then
    clrhome
    colecho "Mount SMB Filesystems" gn
    echo
	    
    if [ "$username" = "" ]
    then
        echo
        echo -e "Do you need a Username to mount the Share?"
	echo
        echo -e "Press Enter for connecting without a Username"
	echo -e "or type in the Name, for instance 'user':"
        echo
        read username
    fi
	
    clrhome
    colecho "Mount SMB Filesystems" gn
    echo

    if [ "$password" = "" ]
    then
        echo
        echo -e "Do you need a Password to mount the Share?"
	echo
        echo -e "Press Enter for connecting without a Password"
	echo -e "or type in the Password, for instance 'pass':"
        echo
        stty -echo
        read password
        stty echo
    fi

    clrhome
    colecho "Mount SMB Filesystems" gn
    echo

    if [ "$username" = "" -a "$password" = "" ]
    then
        smbmount \\\\$smbserver\\$sharename -N -c "mount $mountpoint"
	if [ "$?" != "0" ]
	then
            echo
            colecho "Cannot mount Share $sharename on SMB Server $smbserver to Mountpoint $mountpoint!" br x br
            colecho "Do you need a username and password?" br x br
	else
            echo
            colecho "Share $sharename on SMB Server $smbserver successfull mounted to Mountpoint $mountpoint" gn
	fi
    else
        smbmount \\\\$smbserver\\$sharename "$password" -U "$username" -c "mount $mountpoint" >/dev/null
	if [ "$?" != "0" ]
	then
            echo
            colecho "Cannot mount Share $sharename on SMB Server $smbserver to Mountpoint $mountpoint" br x br
            colecho "Wrong username or password?" br x br
	else
            echo
            colecho "Share $sharename on SMB Server $smbserver successfull mounted to Mountpoint $mountpoint" gn
	fi
    fi
fi
echo
