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

function f-tour ()

{

# The way back or out

tour-escape ()
{
head-col
echo
echo "  Hit m to return to the Screen menu, x for a command prompt."
echo "  Any other key to continue."
	white
	read -s -n 1 TOURESCAPE
	case $TOURESCAPE in
		m)
		f-tour
		;;
		x)
		f-prompt
		;;
		*)
		;;
	esac
}

# This checks for attached | detached screens 
check-existing ()
{
        screen -ls | grep ${SNAME} > /dev/null 2>&1
        if [ $? = 0 ] ; then
            if ! screen -r ${SNAME} ; then
                 screen -x ${SNAME}
            fi
        else
            if [ ${SNAME} = office ] ; then
                SNAME=scroffice
            fi
            f-${SNAME}           
        fi
}

clear

fig-col
figlet "  GNU"          "Screen"
echo 

# Show attached and detached screens
if [ -d /var/run/screen/S-$USER ] && [ $(ls /var/run/screen/S-$USER | wc -l) -gt 0 ] ; then
	head-col
	echo -e "  Currently running screen sessions:\n"
	info-col
	SCREENS=$(screen -ls)
	printf "$SCREENS" | egrep '(Detached|Attached)' # Neaten indenting etc.
fi

echo
head-col
echo "  The GNU Screen program - extra windows within a screen."

info-col
cat << EOSCREENMENU

  1: Introduction to Screen
  2: Monitor Screen
  3: Office Screen
  4: File Manager Screen
  5: Mail Screen
  6: Music Screen
  7: Plain Screen - Roll your Own
  8: Reattach a Detached Screen

EOSCREENMENU

head-col
echo "  Choose by number or letter."
fig-col
cat << EOOPTIONS

  f: Flip colours
  m: INX menu
  w: Net and Web menu
  x: Give me a command prompt

EOOPTIONS

info-col
f-ttynum
read -s -n 1 SCREEN
	case $SCREEN in
		1)
		clear
		head-col
		echo "  Introduction to GNU Screen."
		info-col
		
cat  << EOS

  The GNU Screen program allows you to have a collection of "sub-windows" within one 
  virtual terminal. You can think of it as a kind of "tabbed workspace browser".
	
  The fundamental key combination to remember when using Screen is
EOS

		head-col
		echo
		echo "  			CTRL+a"
		echo
		info-col
		
cat  << EOS1
  What this means is that almost any action you wish to perform in managing Screen 
  is preceded by CTRL+a . For example, to switch from "tab" 0 to "tab" 3 you type
EOS1

		head-col
		echo
		echo "  			CTRL+a 3"
		echo
		info-col
		
cat  << EOS2
  ... and so on.

  The manual for Screen, and the INX cheat sheet for it, use C-a as an abbreviation.

  Other useful starters:
	
	C-a c	"Creates" a new "window"
	C-a A	Lets you edit the name for your current "window" (note capital "A")
	C-a d	Lets you "detach" the current set of windows ( "session" )

  More on these and others will follow...
EOS2

white
tour-escape
clear
head-col
echo "  An important note:"
info-col

cat << EOS3

  Because C-a is taken by Screen as a control sequence, the usual command line shortcut

  CTRL+A 

  in bash, which takes the cursor to the beginning of the current line, does not
  "work" in Screen. You need:

	C-a a

  Although Screen can handle colours from some programs, the simpler 
  colours of INX, and some other programs, will only be rendered in black and white.

  To continue in this introduction, let's start a screen "session" and work from there...

EOS3
		white
		tour-escape
		f-sup
		screen -S intro -c $HOME/.screenrc-intro
		
		clear
		echo
		head-col
		
		echo "  The Screen Introduction continues..."
		info-col
		echo
		echo "  Looks like you got through the first session OK."		
			
cat  << EOS4

  Now that you have a better idea of how Screen works, let's try an example of a "convenience"
  session. This one is named "monitor" -  in fact if you want to, later, you can simply use the
  command "monitor" to start it, assuming it isn't already running. In that case you would type
EOS4

		head-col
		echo
		echo "  screen -r monitor"
		info-col
		
cat <<	EOS5

  to "reattach" it.

  When you have explored this "monitor" session, detach it with C-a d .
  You will then see it in the inx Screen menu as a reminder that it is available.

  In this introduction, though, you will be returned to the next part, rather than the menu.
  So, hit a key to continue as prompted below...


EOS5

		white
		tour-escape
		f-sup
		monitor
		clear
		head-col
		
		echo
		echo "  The final session in this introduction is a kind of \"grab bag\" ..."
		info-col
		
cat << 	EOS6
		
  When you detach or exit the upcoming session of Screen, you are back at the inx Screen menu.
  You will see at least the "monitor" session showing as (Detached) at the top, assuming that
  you did as suggested earlier.

  Tip: You can re-attach detached screens by selecting them from the Screen menu, if they are 
  included session names like "monitor" or "mailscreen".
  
EOS6
yellow ; bold
echo -e "  Another Tip: If this is your first meeting with the \"irssi\" IRC client, "
echo -e "  Remember that to quit the network you type /quit. Don't forget the slash!"

		white
		tour-escape
		f-sup
		screen -S tour -c $HOME/.screenrc-tour
		f-tour
		;;
		2)
		f-sup
		SNAME=monitor
		check-existing
		f-tour
		;;
		3)
		f-sup
		SNAME=office
		check-existing
		f-tour
		;;
		4)
		f-sup
		SNAME=filemanage
		check-existing
		f-tour
		;;
		5)
		f-sup
		SNAME=mailscreen
		check-existing
		f-tour
		;;
		6)
		f-sup
		SNAME=music
		check-existing
		f-tour
		;;
		7)
		clear
		echo
		echo "  Suggestion: Name your Screen session."
		echo
		echo "  For example:"
		yellow
		echo
		echo "  screen -S my-named-session"
		echo
		ownsession ()
		{
		info-col
		echo "  Drop to a command prompt to roll your own session? (y/n)"
		read -s -n 1 ROLLYOUROWN
		case $ROLLYOUROWN in
			y)
			echo
			unbold
			white
			exit
			;;
			n)
			f-tour
			;;
			*)
			echo
			echo "  Please type y for yes, n for no."
			sleep 2
			ownsession
			;;
		esac
		}
		ownsession
		;;
		8)
		echo
		yellow
		echo "  Type the name of the screen you want to reattach."
		echo "  You only need to type enough letters to avoid ambiguity."
		white
		printf "\n  "
		read REATTACH
		screen -r "$REATTACH"
		f-tour
		;;
		f)
		switch
		f-tour
		;;
		m)
		exec menu
		;;
                w)
                exec netinx
                ;;
		x)
		clear
		f-prompt
		;;
		*)
		f-tour
		;;
	esac


 }  

function f-filemanage ()
{
screen -S filemanage -c $HOME/.screenrc-filemanage
}

function f-mailscreen ()

{

if [ -f $HOME/.muttrc ] && [ -f $HOME/.msmtprc ] ; then
screen -S mailscreen -c $HOME/.screenrc-mailscreen
else
	clear
	echo
	bold
	yellow
    echo "  To use Mutt Mail, you will need to follow the "Simple Setup" from the INX Mail Menu."
    echo "  I can take you there - do you want to make your mail settings now? (y/n)"
	echo
	read -s -n 1 SETTINGUP
	case $SETTINGUP in
		y)
		f-mail-settings
		;;
		n)
		echo
		white
		echo "  OK - hit any key to return to the INX Screen menu."
		echo "  If you just want to use Gmail, you can start a plain screen, and type"
		echo
		yellow
		echo "  gmail"
		white
		echo
		echo "  to get the no-frills Gmail login page, in the Elinks browser."
		read -s -n 1
		f-tour
		;;
		*)
		echo
		echo "  Please answer y for yes, n for no "
		sleep 2
		f-mailscreen
		;;
    esac

fi
}

function f-monitor ()
{
screen -S monitor -c $HOME/.screenrc-monitor
}

function f-music ()
{
screen -S music -c $HOME/.screenrc-music
}

function f-scroffice ()
{
screen -S office -c $HOME/.screenrc-office
}

f-tour