#!/bin/bash


# ------------------------------------------------------------------------
# Prerequisite: zenity, libnotify-bin
# ------------------------------------------------------------------------


# -------- Set the following variables to your own config ----------------

	# Icon file
		ICONFILE=$HOME/icons/gnome-log.png

	# location of notification window
		NWX=1600
		NWY=20

	# NFS server path (last character must be a slash)
		NFSSERVERPATH=192.168.1.2:/home/user/share/

	# where to mount the volume (last character must be a slash)
		MOUNTPATH=$HOME/

	# list of NFS
		MOUNTLIST="SUBDIRA SUBDIRB"

# ------------------------------------------------------------------------


# request NFS to mount
	MOUNTDIR=`zenity --height=200 --list --title="NFS" \
		--text="Select mount" --column=Location $MOUNTLIST`


# check the proper NFS selection is made
	if [ $? == 1 ] ; then
		# prompt was cancelled
		exit
	fi
	if [ "$MOUNTDIR" == "" ] ; then
		# no item was selected
		exit
	fi

# set mount location variables
	NFSSERVERPOINT=$NFSSERVERPATH$MOUNTDIR
	MOUNTPOINT=$MOUNTPATH$MOUNTDIR


# check mount location
	find $MOUNTPOINT
	if [ $? == 0 ]; then
		zenity --error --title "NFS" --text="Mount point already exist!"
		exit
	fi


# check if NFS is mounted already
	nfsstat -m | grep $MOUNTPOINT
	if [ $? == 0 ]; then
		zenity --error --title "NFS" --text="NFS volume already mounted!"
		exit
	fi


# attempt to mount NFS
	mkdir $MOUNTPOINT
	gksu mount $NFSSERVERPOINT $MOUNTPOINT
	nfsstat -m | grep $NFSSERVERPOINT | grep $MOUNTPOINT
	if [ $? == 1 ]; then
		rmdir $MOUNTPOINT
		zenity --error --title "NFS" --text="Cannot mount $NFSSERVERPOINT!"
		exit
	fi


# notify mounted Truecrypt partition
	notify-send -u normal -i $ICONFILE -t 5000 -h int:x:$NWX -h int:y:$NWY \
		"NFS" "NFS volume $NFSSERVERPOINT mounted."


# holding loop while NFS is mounted
	while [ 1 ]; do
		zenity --notification --window-icon=$ICONFILE --text "NFS dismount $NFSSERVERPOINT"

		zenity --question --title "NFS" --text "Dismount your NFS volume $NFSSERVERPOINT?"
		if [ $? == 1 ]; then
			# dismount cancelled
			continue
		fi


		# attempt to dismount Truecrypt partition
			gksu umount $MOUNTPOINT
			nfsstat -m | grep $NFSSERVERPOINT | grep $MOUNTPOINT
			if [ $? == 1 ]; then
				rmdir $MOUNTPOINT
				notify-send -u normal -i $ICONFILE -t 5000 -h int:x:$NWX -h int:y:$NWY \
					"NFS" "NFS volume $NFSSERVERPOINT dismounted."
				exit
			fi

			zenity --error --title "NFS" --text="Cannot dismount $NFSSERVERPOINT!"
	done
