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

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
              ;;
            *)
              $colechobin "Please answer y(es) or n(o)!" br x br
              ;;
    esac
  done
}

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

do_title

for module in smbfs nls_iso8859-1 nls_cp850
do
  if [ -z "`lsmod | grep $module`" ]
  then
      insmod $module
  fi
done

if [ "$smbserver" = "" ]
then
    echo "From which server do you want to mount a share?"
    echo
    echo "This could be a Windows or Linux host with an active share."
    echo "Type in the NETBIOS name of the host,"
    echo "for instance 'client1':"
    echo
    read smbserver
fi

if [ "$smbserver" = "" ]
then
    $colechobin "No SMB server specified!" br x br
    exit 1
else
    if [ -n "`nmblookup $smbserver | grep \"name_query failed to find name $smbserver\"`" ]
    then
        $colechobin "Cannot find NETBIOS name $smbserver!" br x br
        exit 1
    fi
fi

do_title

if [ "$sharename" = "" ]
then
    echo "What is the name of the share?"
    echo
    echo "Type in the name of the share,"
    echo "for instance 'Freigabe':"
    echo
    read sharename
fi

if [ "$sharename" = "" ]
then
    $colechobin "No share name specified!" br x br
    exit 1
fi

do_title

if [ "$mountpoint" = "" ]
then
    echo "Where do you want to mount the share?"
    echo
    echo "This must be a absolut path! If the specified"
    echo "directory does not exists, it will be created for you."
    echo "If you are using a existing directory, it must be empty!"
    echo "Type the absolut path to a directory,"
    echo "for instance '/mountpoint':"
    echo
    read mountpoint
fi

if [ "$mountpoint" = "" ]
then
    $colechobin "No mountpoint specified!" br x br
    exit 1
else
    if [ "`echo $mountpoint | cut -c1`" != "/" ]
    then
        $colechobin "You must specify an absolut path with a leading '/'!" br x br
        exit 1
    else    
        mkdir -p $mountpoint

        if [ "$?" != "0" ]
        then
            $colechobin "Cannot create mountpoint $mountpoint!" br x br
            exit 1
        else
            idx='0'

            for i in `ls $mountpoint`
            do
              idx=`expr $idx + 1`
            done

            if [ "$idx" != "0" ]
            then
                $colechobin "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
                        $colechobin "There is already "$x" mounted on "$y"!" br x br
                        ask "Do you want to dismount "$x" from "$y""

                        if [ "$a" = "y" ]
                        then
                            echo "umounting SMB filesystem in $y ..."
                            cd /
                            smbumount $y

                            if [ "$?" = "0" ]
                            then
                                echo "SMB filesystem in $y umounted"
                            else
                                $colechobin "cannot umount SMB filesystem in $y!" br x br
                                exit 1
                            fi
                        else
                            echo
                            exit 1
                        fi
                    fi
                fi
            fi
        fi
    fi
fi

if [ "$smbserver" != "" -a "$sharename" != "" -a "$mountpoint" != "" ]
then
    do_title

    if [ "$username" = "" ]
    then
        echo "Do you need a username to mount the share?"
        echo
        echo "Press Enter for connecting without a username"
        echo "or type in the name, for instance 'user':"
        echo
        read username
    fi

    do_title

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

    do_title

    if [ "$username" = "" -a "$password" = "" ]
    then
        smbmount \\\\$smbserver\\$sharename -N -c "mount $mountpoint"

        if [ "$?" != "0" ]
        then
            $colechobin "Cannot mount share $sharename on SMB server $smbserver to mountpoint $mountpoint!" br x br
            $colechobin "Do you need a username and password?" br x br
        else
            $colechobin "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
            $colechobin "Cannot mount share $sharename on SMB server $smbserver to mountpoint $mountpoint" br x br
            $colechobin "Wrong username or password?" br x br
        else
            $colechobin "Share $sharename on SMB server $smbserver successfull mounted to mountpoint $mountpoint" gn
        fi
    fi
fi

echo
