#!/bin/bash
PATH=$PATH:/usr/local/lib/inx
. inx_essentials
maybe_switch

# Function for sshfs front end menu

function f-sshfs ()
{

SSHFSTMP=$(mktemp -t sshfs.XXXXXXXXXX)
cleantmp () { rm -f /tmp/sshfs* ; }
cleantmp

explain ()
{
clear ; fig-col
figlet "  sshfs"
info-col ; echo
cat << EOSSHFS

  Briefly, "ssh" is a protocol that allows secure encrypted access to other machines
  that have an "ssh daemon" (server) installed. To use ssh you must have an account
  on the remote "host" (server machine). You need your user name on that machine, and
  usually a password, unless ssh has been configured to allow "keypairs" for login.

  "Sshfs" is a clever system that allows you to access filesystems remotely via ssh, yet 
  have the contents appear in a "mountpoint" directory on your local machine. 

  INX uses $HOME/sshfs as the directory to contain mountpoints, and creates directories 
  in it according to the IP number or hostname of the "remote host".

  You can choose which program to use for browsing the "share" (for example, the music 
  player MOC, the file manager Midnight Commander, and so on).

  You can connect/mount or disconnect/unmount shares. On "unmounting", the mountpoint 
  directory is removed by the INX "menu method" until you reconnect, to avoid confusion. 

  Be aware that unmounting will fail if the directory is in use!

EOSSHFS
echo ; echo ; fig-col
echo "  Hit any key to continue..."

read -s -n 1

clear ; fig-col ; echo
echo "  If you prefer, you can do this from the command line, of course."
echo ; head-col
echo "  The commands for do-it-yourself are on these lines:"
echo ; info-col
echo "  Make a directory (preferably in $HOME) to act as a mountpoint:"
echo ; yellow ; bold
echo "  mkdir -p /path/to/mountpoint"
echo ; info-col
echo "  To mount:"
bold ; yellow ; echo
echo "  sshfs username@remote.host:/optional/path /path/to/mountpoint"
echo ; info-col
echo "  To unmount:"
echo ; bold ; yellow
echo "  fusermount -u /path/to/mountpoint"
echo ; info-col
echo 
echo "  \"mountpoint\" can of course be any directory you create that is readable and writable."
cat << EOUSUAL

  You can, obviously, also use ssh in the usual manner for shell access, but sshfs
  gives you local access to files as if you had them on your machine - with some lag,
  naturally, if accessing over slow links. On a local network, the lag is rarely noticeable.

  For shell access only, as opposed to sshfs, the stock command is:
EOUSUAL

echo ; yellow ; bold
echo "  ssh remote-username@IP-or-host"
echo ; echo ; fig-col
echo "  Hit any key to return to the sshfs access menu."
read -s -n 1
f-sshfs
}

options ()
{
# Just opens a few possibilities at the selected mount point
clear
fig-col
figlet "  options"
echo ; head-col
echo "  Choose an access method for $SSHOST:"
info-col
cat << EOMENU

  1: Midnight Commander (mc).
  2: Vifm. 
  3: The MOC music player.
  4: Give me a command prompt at the mount point.

EOMENU

head-col
echo "  x: Just give me a prompt."
echo ; fig-col
echo "  Any other key to return to the access menu."
f-ttynum
read -s -n 1 ACCESS
case $ACCESS in
	1)
	echo
	f-sup
	echo
	mc $HOME/sshfs/$SSHOST
	options
	;;
	2)
	echo
	f-sup
	echo
	cd $HOME/sshfs/$SSHOST && vifm && cd
	options
	;;
	3)
	echo
	f-sup
	echo
	mocp $HOME/sshfs/$SSHOST
	options
	;;
	4)
	cd $HOME/sshfs/$SSHOST
	clear
	echo
	echo "  This is a subshell - exit when you have finished, and you will return to the options menu."
	unbold ; white ; echo
	/bin/bash && cd
	options
	;;
	x)
	clear
	f-prompt
	;;
	*)
	f-sshfs
	;;

esac
}

connect ()
{
clear
fig-col
figlet "  mount share"
echo
head-col
echo "  Please enter the address or IP number of the server. (e.g. bleep.org , 10.1.1.2 )"
echo ; white ; printf "  "
read SSHOST
echo ; head-col
echo "  Enter your user name on the remote server (e.g. bill, linus)"
echo ; white ; printf "  "
read SSHNAME
echo ; head-col
echo "  Enter the remote directory path to access ( leave blank for your remote home directory)"
echo ; white ; printf "  "
read DIRPATH
echo ; head-col

echo "  Your $DIRPATH directory on $SSHNAME@$SSHOST will be mounted at $HOME/sshfs/$SSHOST"
white
	
	
	echo ; info-col
	echo "  If this is your first connection to $SSHOST, you will be asked to confirm."
	head-col ; echo
	echo "  Hit < n > to abort: <enter> or any other key to connect."
	read -s -n 1 CONNECT
	case $CONNECT in
		n)
		white
		f-sshfs		
		;;
		*)	
		echo
		f-sup
		# Make the mountpoint
		if ! [ -d $HOME/sshfs/$SSHOST ] ; then
			mkdir -p $HOME/sshfs/$SSHOST
		fi
		# If we can't connect port 22, forget it... 'nc -w 2 -z $SSHOST 22' checks...
		# Adding port options just means asking more questions... 
		if ! nc -w 2 -z $SSHOST 22 ; then
		    echo
		    echo "  \"$SSHOST\" doesn't seem to be listening (at least on the standard ssh port [22])"
		    echo "  Either try again later, or use command-line sshfs to specify the port."
		    echo "  Hit any key to return to the sshfs menu."
			read -s -n 1
			f-sshfs
		fi
		echo
		if ! sshfs "$SSHNAME"@"$SSHOST":"$DIRPATH" "$HOME"/sshfs/"$SSHOST" 
			then
			echo
			echo "  There seems to be a problem... try again."
			sleep 3
			#Could be in use
			if grep $SSHOST /etc/mtab > /dev/null 2>&1 ; then
				echo
				echo "  $SSHOST appears to be mounted already."
				sleep 3
				f-sshfs
			else
				rm -rf $HOME/sshfs/"$SSHOST"
				f-sshfs
			fi
		else
			options
		fi		
		
		;;
	esac	
	

}		

browse ()
{
clear
bold ; fig-col ; echo
figlet "  browse"
echo
head-col
echo "  Browse your remote file systems (sshfs)"
ssh_mountcheck
head-col ; echo
echo "  Choose by number - any non-number key to return to the access menu."

info-col ; echo
COUNT=1
for dir in $(ls -d $HOME/sshfs/*)
do printf "  $COUNT" ; printf ': ' ; printf "$(basename $dir)" ; echo
echo $(basename $dir) > $SSHFSTMP"$COUNT"
COUNT=$((( $COUNT + 1 )))
done 
echo
f-ttynum
read -s -n 1 SERVER
case $SERVER in
	[1-9])
	SSHOST=$(cat $SSHFSTMP"$SERVER")
	cleantmp
	options
	;;
	*)
	f-sshfs
	;;
esac
	
}

ssh_mountcheck ()
{
if grep sshfs /etc/mtab > /dev/null 2>&1 ; then
    echo ; head-col
    echo "  According to /etc/mtab, you have the following mounted with sshfs."
    echo ; info-col 
    for mounts in $(grep sshfs /etc/mtab | cut -d' ' -f1) ; do echo "  $mounts" ; done
else
    echo ; head-col
    echo "  You don't have any mounted remote filesystems now, according to /etc/mtab."
    echo
    echo "  Hit any key to return to the access menu."
    white
    read -s -n 1
    f-sshfs
fi
}

                                                        
ssh_unmount ()
{
white ; clear
bold ; fig-col ; echo
figlet "  unmount shares"
ssh_mountcheck
echo ; head-col
echo "  Choose the remote filesystem to unmount and disconnect."
echo ; info-col

COUNT=1
for dir in $(ls -d $HOME/sshfs/*)
do printf "  $COUNT" ; printf ': ' ; printf "$(basename $dir)" ; echo
echo $(basename $dir) > $SSHFSTMP"$COUNT"
COUNT=$((( $COUNT + 1 )))
done

echo ; head-col
echo "  Choose by number - any non-number key to return to the access menu."
echo
f-ttynum
read -s -n 1 UNMOUNT
case $UNMOUNT in
	[1-9])
	# Check if it's mounted first
	if ! grep $(cat $SSHFSTMP"$UNMOUNT") /etc/mtab > /dev/null 2>&1 ; then
	    echo "  $(cat  $SSHFSTMP"$UNMOUNT") does not seem to be mounted..."
	    sleep 4
	    cleantmp
	    ssh_unmount
	fi    
	echo ; head-col
	echo "  Unmounting $(cat $SSHFSTMP"$UNMOUNT")"
	fusermount -u $HOME/sshfs/$(cat $SSHFSTMP"$UNMOUNT") 
	echo
	sleep 2
	rmdir $HOME/sshfs/$(cat $SSHFSTMP"$UNMOUNT")
	cleantmp
	ssh_unmount
	;;
	*)
	f-sshfs
	;;
esac
}

clear
bold ; fig-col ; echo
figlet "  Secure Access"
echo
head-col
echo "  This menu enables access via the ssh file system."
echo ; info-col

cat << EOSSHFS
  1: Connect to an ssh server filesystem	(sshfs)
  2: Browse connected remote filesystems
  3: Disconnect/Unmount remote filesystems
  4: Explain ssh and sshfs.

EOSSHFS

head-col
cat << EOOPTIONS
  d: Hard drive menu
  m: INX main menu
  w: Net and Web menu
  f: Flip colours
  x: Give me a command prompt
EOOPTIONS

if grep sshfs /etc/mtab > /dev/null 2>&1 ; then
	info-col ; echo 
	echo "  Currently mounted sshfs directories:"
	bold ; echo ; fig-col
	for mount in $(grep sshfs /etc/mtab | cut -d" " -f2) ; do echo "  $mount" ; done
	white
fi

f-ttynum
read -s -n 1 SSHFS
case $SSHFS in
	1)
	connect	
	;;
	2)
	browse
	;;
	3)
        ssh_unmount	
	;;
	4)
	explain
	;;
	d)
	cleantmp
	exec disc
	;;
	m)
	cleantmp
	exec menu
	;;
	w)
	cleantmp
	exec netinx
	;;
	f)
	cleantmp
	switch
	f-sshfs
	;;
	x)
	cleantmp
	clear
	f-prompt
	;;
	*)
	echo 
	echo "  I didn't understand that request - please try again."
	sleep 2
	cleantmp
	f-sshfs
	;;
esac

}

f-sshfs