#!/bin/bash

showHelp=0

mntDir="/media"
valType=""
valHost=""
valDir="/"

valUid=`id -u`
valGid=`id -g`
valMask=`umask`

while [ "$1" != "" ]; do
	case "$1" in
	  -h | --help | --usage)
		showHelp="1"
		;;
	  --no-sudo )
		noSudo="1"
		;;
	  -t | --type)
		shift
		valType="$1"
		;;
	  -rd | --remote-dir)
		shift
		valDir="$1"
		;;
	  -ld | --local-dir)
		shift
		mntDir="$1"
		;;
	  -u | --user)
		shift
		valUser="$1"
		;;
	  -p | --password)
		shift
		valPass="$1"
		if [ "$valPass" == "0" ]; then
			valPass=""
		fi
		setPass="1"
		;;
	  -uid | --user-id)
		shift
		valUid="$1"
		;;
	  -gid | --group-id)
		shift
		valGid="$1"
		;;
	  -um | --umask)
		shift
		valMask="$1"
		;;
	  *)
		valHost="$1"
		;;
	esac
	shift
done

if [ "$showHelp" == "1" ]; then
	echo "Usage: mountnet [OPTIONS <values>] HOST_ADDRESS[:PORT]"
	echo "Mounts remote filesystem or directory into local directory"
	echo " "
	echo "Common options:"
	echo "  -t <val>, --type <val>         Connection type - available options: nfs,"
	echo "                                 smb or ftp."
	echo "  -rd <val>, --remote-dir <val>  Remote path to requested directory. Default: /"
	echo "  -ld <val>, --local-dir <val>   Where to place remote directory in local"
	echo "                                 filesystem. Default: /media"
	echo "  -u <val>, --user <val>,"
	echo "  -p <val>, --password <val>     Send credentials if authorization is required."
	echo "  host_address[:port]            Remote host IP or DNS address."
	echo " "
	echo "General options:"
	echo "  -h, --help, --usage  This help."
	echo "  --no-sudo            When root privileges are necessary, application forces"
	echo "                       sudo command. If you want to disable this feature, use"
	echo "                       this prefix."
	echo " "
	echo "(c) 2014 LUnet Studio, www.studio.LUnet.sk"
	exit
fi


sc=""
if [ "$USER" != "root" ] && [ "$noSudo" != "1" ]; then
	sc="sudo "
	sudo echo "" > /dev/null
fi


if [ "$valType" == "" ]; then
	read -p "Connection type (nfs|smb|ftp): " valType
fi
if [ "$valType" != "" ]; then
	if [ "$valHost" == "" ]; then
		read -p "Host address: " valHost
	fi
	if [ "$valHost" != "" ]; then
		if [ "$setPass" == "1" ] && [ "$valPass" == "" ]; then
			read -s -p "User password: " valPass
		fi
		
		case "$valType" in
		  nfs)
			mntParams="-t nfs -o soft,intr,timeo=30,tcp "
			;;
		  smb)
			mntParams="-t cifs -o username=${valUser},password=${valPass},uid=${valUid},gid=${valGid} "
			;;
		  ftp)
			mntParams="-t fuse -o allow_other,uid=${valUid},gid=${valGid},umask=${valMask:1} curlftpfs#${valUser}:${valPass}@"
			;;
		esac
		
		if [ "$valDir" == "/" ]; then
			valDirName="${valHost}"
		else
			valDirName="${valHost}_-${valDir//"/"/_}"
		fi
		
		${sc}mkdir ${mntDir}/${valDirName}
		${sc}chown ${valUid}:${valGid} ${mntDir}/${valDirName}
		${sc}mount ${mntParams}${valHost}:${valDir} ${mntDir}/${valDirName}
		
		echo "${valHost}:${valDir} mounted in ${mntDir}/${valDirName}."
	else
		echo "Required parameters was not specified."
		echo "Try 'mountnet --help' for more information."
	fi
else
	echo "Required parameters was not specified."
	echo "Try 'mountnet --help' for more information."
fi
