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

######################
# USB functions and menus

function drive_mountcheck ()
{
# Check to see whether it exists ;-)
grep /media/usb"$MOUNTPOINT" /etc/mtab   > /dev/null 2>&1
if [ $? -ne 0 ] ; then
        echo
        info-col
        echo "  /media/usb"$MOUNTPOINT" is not mounted, according to mtab"
        echo "  Did you type the right number?"
        echo
        sleep 5
        f-usb
fi
}

function f-usb ()
{
clear
fig-col
bold
figlet "  inx      usb"
head-col
echo "  Access your mounted usb devices"
echo
info-col
echo "  When usb devices are plugged in, you should see some lines 
  of output text at the foot of the screen."
echo
head-col
echo "  Refresh this page by hitting <enter> to see the mount point in /media."
info-col
echo
echo "  Mount Point"
bold
green
echo

usbshowmounts="$(mktemp)"
grep /media/usb /etc/mtab | cut -f 2 -d" " > "$usbshowmounts"
for show in $(cat "$usbshowmounts") ; do echo "  $show" ; done # To make output indent two spaces
rm "$usbshowmounts"

echo
info-col
echo "  Choose the device you want to access by usb number."
echo "  For example, \"0\" for /media/usb0 "
echo
head-col
echo "  d: Go to the hard drive menu."
echo "  e: Eject and unmount a device."
echo "  f: Flip colours."
echo "  m: INX main menu."
echo "  x: Give me a command prompt."

f-ttynum
read -s -n 1 MOUNTPOINT

case "$MOUNTPOINT" in
	[0-7] )
	drive_mountcheck
	f-access
	;;
	d)
	exec disc
	;;
	e)
	MOUNTPOINT=
	f-eject
	f-usb
	;;
	f)
	switch
	f-usb
	;;
	m)
	exec menu
	;;
	x)
	clear
	f-prompt
	;;
	*)
	f-usb
	;;
esac

}

# This stuff only works with "usbmount"

function doublecheck ()
{
# Check to see whether it exists ;-)
grep /media/usb"$CHECK" /etc/mtab   > /dev/null 2>&1
if [ $? -ne 0 ] ; then
        echo
        echo "  /media/usb"$CHECK" is not mounted, according to mtab"
        echo "  Did you type the right number?"
        echo
        sleep 5
        f-usb
fi
}

function f-eject ()
{

echo
# See if there are actually any usb devices mounted as /media/usb* in /media ( /etc/mtab)

grep /media/usb /etc/mtab   > /dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo "  You don't seem to have any usb devices auto-mounted, according to mtab"
	sleep 3
	f-usb
fi

# If we haven't just used/accessed a device, then ask...
# Otherwise use the number we have already. (from f-access)

if [ "$MOUNTPOINT" = "" ] ; then
    echo "  Type the number of the device to eject - for instance 1 for /media/usb1"
    read -s -n 1 "EJECT"
	CHECK="$EJECT"
	doublecheck
        eject /media/usb"$CHECK" 2> /dev/null # hardy eject wants to "open" a non-existent tray
	echo -e "\n  Unmounting and ejecting... please wait"
	sleep 3 # not sure this is needed but lights flash for a while...
else
	CHECK="$MOUNTPOINT"
	doublecheck
	eject /media/usb"$CHECK" 2> /dev/null 
	echo -e "\n  Unmounting and ejecting... please wait"
	sleep 3 
fi
	
# Check mtab to see if the device actually unmounted and ejected

grep /media/usb"$CHECK" /etc/mtab   > /dev/null 2>&1
if [ $? -ne 0 ] ; then
	# OK, grep can't find it in /etc/mtab so it must be unmounted...
        green ; bold 
        echo -e "\n  It is now safe to remove the device that was mounted at /media/usb"$CHECK""
        white
        sleep 3
else
	# Something wicked happened, or didn't happen...
        red ; bold 
        echo -e "\n  Your device seems not to have safely unmounted - please check"
        echo "  if it is still shown mounted in the usb menu. Hit <enter> to see."
        white
        read
fi
        f-usb
}

# We get $MOUNTPOINT from f-usb

function f-access ()
{
clear ; unbold
head-col
echo "  Type the number for the access method you want."

info-col

cat <<EOACCESS

  1: Midnight Commander (mc)
  2: Vi File Manager    (vifm)
  3: Linm File Manager  (linm)
  4: Just give me a shell prompt at the mountpoint

  5: Open the mountpoint in the music player (mocp)

EOACCESS

fig-col

echo "  e: Unmount and eject my device safely"
echo

head-col

echo "  Any other key returns to the usb menu."

# Print tty number right edge of terminal
f-ttynum

read -s -n 1 ACCESS

case "$ACCESS" in

	1)
	cd "/media/usb"$MOUNTPOINT"" && f-sup && mc && cd 
	;;
	2)
	cd "/media/usb"$MOUNTPOINT"" && f-sup && vifm && cd 
	;;
	3)
	cd "/media/usb"$MOUNTPOINT"" && f-sup && linm && cd 
	;;
	4)
	clear
	head-col
	echo
	echo "  OK - this is a subshell. Type exit when you are finished"
	echo "  and you will be back in "$HOME". You can then unmount your"
	echo "  device. Don't forget that you are using this subshell," 
	echo "  or the device may show \"busy!\" and refuse to unmount."
	echo
	cd "/media/usb"$MOUNTPOINT"" && white && unbold && /bin/bash && cd 
	;;
	5)
	f-sup ; mocp "/media/usb"$MOUNTPOINT"" && pkill mocp && cd
	;;
	e)
	f-eject
	;;
	*)
	f-usb
	;;

esac

f-access

}

f-usb