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

f-menu () 
{

clear
echo
fig-col
figlet -t -k "  inx          Menu                              "
head-col
echo "  Type the number for your choice"
echo 
echo "  I suggest that you try things in this order..."
echo
info-col
echo "  0: Why INX ?                                                                       "
echo "  1: Watch an introduction to INX                 (intro)                            " 
echo "  2: Listen to Internet radio streams             (radio)                            "
echo "  3: Learn About Linux on the Console             (tutorial)                         "
echo "  4: The GNU Screen program                       (screeninx  - do tutorial first!)  "
echo "  5: File Access and Sharing                      (cd-mount,drivinx,disc,sharinx,usb)"
echo "  6: Net, Web, and Mail                           (netinx)                           "
echo "  7: Office                                       (office)                           "
echo "  8: Tools, Preferences, Information              (tools)                            "
echo "  9: Fun and Games                                (fun)                              "
echo
fig-col
echo "  d: Direct Access Menu (most things)            "
echo "  f: Flip the colour scheme                      "     
echo "  x: Just give me a command prompt, and go away! "                
echo "  t: Toggle clock on|off                         "
echo "  q: Log out, and shut INX down       r: Reboot  "
echo "  [enter] Refresh menu (shows attached devices)  "
echo

head-col

echo "  You can return to this menu any time by typing menu <enter> at a prompt"

show_mounts # Function prints mounted file systems in pretty colours ;)

white

# Print tty number right edge of terminal
f-ttynum

printf "  " ; read -s -n 1 NUM
choice
}

choice ()
{
case $NUM in

	0)
	exec why
	;;
	1)
	exec intro
	;;
	2)
	exec radio
	;;
	3)
	exec tutorial
	;;
	4)
	exec screeninx
	;;
	5)
        exec drivinx
	;;
	6)
	exec netinx
	;;
        7)
        exec office
        ;;
	8)
	exec tools
	;;
	9)
	exec fun
	;;
	q)
	clear ; echo -e "\v\v\v\v\v"
	yellow ; bred ; bold 
	echo -e "\t\tDo you want to end your INX session and halt the machine? (y/n)"
	white ; bblack
        read -s -n 1 "HALT"
        if [ "$HALT" = "y" -o "$HALT" = "Y" ] ; then
        # Maybe $USER has a slot-loading cdrom/dvdrom... then cdrom needs to eject now if "toram".
	        if grep toram /proc/cmdline > /dev/null 2>&1 ; then
        	    echo -e "\t\tOK - checking for cdrom, please wait...\n"
	            if [ "$(cdinfo -d /dev/cdrom)" != "no_disc" ] ; then
		            echo -e "\t\tEjecting..."
	                eject /dev/cdrom
	            fi
	        fi
            sudo echo -e "\t\tShutting down in 5 seconds ... CTRL+C to stop shutdown... \n"
            sleep 5
	    # Unmount usb - needed for 'persistent' to avoid multiple bogus mounts showing...
	    sudo umount /media/usb* 2> /dev/null # Spits out a lot of stuff - seems harmless... 
            sudo shutdown -h now
        else
            f-menu
        fi
        ;;
	r)
	clear ; echo -e "\v\v\v\v\v"
	yellow ; bred ; bold
	echo -e "\t\t\tDo you want to reboot the system? (y/n)"
	white ; bblack
        read -s -n 1 "REBOOT"
        if [ "$REBOOT" = y -o "$REBOOT" = "Y" ] ; then
	        # Maybe $USER has a slot-loading cdrom/dvdrom and wants to eject it...
	        if grep toram /proc/cmdline > /dev/null 2>&1 ; then
        	    echo -e "\t\t\tOK - checking for cdrom, please wait...\n"		    
	            if [ "$(cdinfo -d /dev/cdrom)" != "no_disc" ] ; then
	                echo -e "\n\t\t\tDo you want to eject the CD ? (n for \"No\""
	                read -s -n 1 EJECT_CD
	                if ! [ "$EJECT_CD" = "n" -o "$EJECT_CD" = "N" ] ; then
			            echo -e "\t\t\tEjecting..."
	                    eject /dev/cdrom
	                fi
	            fi
	        fi
	                    
            sudo echo -e "\t\t\tRebooting in 5 seconds ... CTRL+C to stop reboot... \n"	        
            echo
            sleep 5
	    # Unmount usb - needed for 'persistent' to avoid multiple bogus mounts showing...
	    sudo umount /media/usb* 2> /dev/null	    
            sudo shutdown -r now
        else
            f-menu
        fi
	;;	 
	x)
	clear
	f-prompt
	exit
	;;
	f)
	switch
	f-menu
	;;
	t)
	time-toggle
	f-menu
	;;
	d)
	exec direct menu
	;;
	*)
	f-menu
	;;

esac
}


NUM=$1
if [ -n $1 ] ; then
    choice
else
    f-menu  
    choice
fi
