#!/bin/bash
# 2009 (c) Etersoft http://etersoft.ru
# Author: Konstantin Baev <kipruss@etersoft.ru>
# GNU Public License

# Script for mount cifs shares with recommended mount options

fatal()
{
    echo $@
    exit 1
}

if [ -f /etc/etercifs.conf ] ; then
  . /etc/etercifs.conf
else
  fatal "Not found configuration file /etc/etercifs.conf"
fi

[ "$UID" = "0" ] && SUDO= || SUDO=sudo

create_share_dir()
{
    if [ -d "$1" ] ; then
        [ -n "`ls $1`" ] && fatal "Error: the folder $1 is not empty!"
        echo "Info: the folder $1 exists and empty"
    else
        if $(mkdir -p "$1") ; then
            echo "Info: the folder $1 was created"
        else
            fatal "Error while creating the folder $1"
        fi
    fi
}

help_text()
{
    echo "The utility etermount performs mount network share on a protocol cifs"
    echo "with pre-set parameters, which determined in MOUNT_OPTIONS variable"
    echo "in the config file /etc/etercifs.conf"
    echo "(current value of MOUNT_OPTIONS is '$MOUNT_OPTIONS')"
    echo
    echo "To mount the resource //server/share to the mountpoint /path/mountpoint"
    echo "need to execute the command (with root permissions):"
    echo
    echo "etermount <//server/share> [</path/mountpoint>]"
    echo
    echo "If the mountpoint isn't specified, it's default value is determined"
    echo "in the variable DEFAULT_MOUNTPOINT in the config file /etc/etercifs.conf"
    echo "(current value of DEFAULT_MOUNTPOINT is '$DEFAULT_MOUNTPOINT')."
    echo
    echo "Report bugs to <support@etersoft.ru>."
}

[ "$1" == '--help' -o "$1" == '-h' ] && { help_text ; exit 0 ; }

[ "$#" -ge 1 -a "$#" -le 2 ] || fatal 'Usage: etermount <//server/share> [</path/mountpoint>]'

if [ "$2" == '' ] ; then
    SHARE_PATH=$DEFAULT_MOUNTPOINT
else
    SHARE_PATH="$2"
fi

create_share_dir $SHARE_PATH

if $($SUDO mount -t cifs "$1" $SHARE_PATH -o $MOUNT_OPTIONS &>/dev/null) ; then
    echo "Info: mount of share $1 in mountpoint $SHARE_PATH has been successfully"
else
    fatal "Warning: error while mount of share $1 in mountpoint $SHARE_PATH!"
fi
