# Bash functions

    killer ()
    {
	# This has to be done with surgically precise murder and mayhem, 
	# *specifically*, for each tty login... Filter for current user as well.
	# A general 'killall' or 'pkill' won't do :| (e.g. we don't kill ssh logins by others)
        TMPKILL=$(mktemp)
        for each in $(ps -u $(whoami) -o tty= | grep ^tty | cut -c4) 
          do
            ps a | grep tty"$each" | grep login | grep -v tty"$(fgconsole)" | awk '{print $1}' >> $TMPKILL
          done
        sudo kill $(cat $TMPKILL) 2> /dev/null || true 
        rm $TMPKILL
		    
        # Now commit suicide locally, after murdering our alteregos globally...
        sudo pkill -t $(tty | cut -c6-) login
    }
    
    guru_mode ()
    {
    bold ; yellow ; bblack ; clear
    printf "\v\v\n  Of course, you will already know what this does, O Great Guru...\n\n  "
    bred 
    for mwuhahah in 5 4 3 2 1 0 ; do sleep 1 && printf "  $mwuhahah ... " ; done
    sleep 1
    bblack
    for guru in $(ps -u $(whoami) -o tty= | grep ^tty | cut -c4) 
    do 
      clear > /dev/tty"$guru"
      tput sgr0 > /dev/tty"$guru"
    done
    if [ -e $HOME/.auto-menus ] ; then
        mv $HOME/.auto-menus $HOME/.auto-menus.bak
    fi
    killer
    }

    theme_chooser ()
    {
    unbold ; clear ; echo
    fig-col
    figlet "  inx Colours"
    head-col
    echo
    echo "  Here you can choose your \"default\" colour theme.."
    info-col
    echo
    echo "  Changing the colour defaults here needs a logout/login."
    echo "  In a \"live\" session, there will be an automatic re-login."
    echo
    echo "  You will be asked for confirmation, but be sure you have no"
    echo "  important work happening on *any* virtual terminal!"
    echo 
    echo "  You can return and alter your preferred theme at any time, by"
    echo "  selecting this menu."
    echo 
    echo "  Type \"f\" for \"flip\" to cycle through the available themes."
    echo
    echo "  You can still alter colours \"locally\" using the \"Flip Colours\""
    echo "  option, from most INX menus."
    echo
    echo "  This menu sets your \"default\", \"global\" theme, or none, for Gurus ;-)"
    echo
    fig-col
    echo "  f: Flip to another \"default\" colour theme."
    echo "  b: Go back to the options menu."
    echo "  g: Guru Mode: no themes, no questions asked ;-)"
    echo "  y: Choose this theme as your default."
    head-col
    echo -e "\n  Type \"f\" to flip, \"y\" to confirm the theme that you want...\n"
    echo
    info-col
    read -s -n 1 COLOUR_THEME
    if [ -e $HOME/.auto-menus.bak ] ; then mv $HOME/.auto-menus.bak $HOME/.auto-menus ; fi
        case $COLOUR_THEME in
            b|B)
            if [ "$EXPORTED" = "theme" ] ; then # Called from "tools"
	        exec tools
            else		
                exec first-screen
            fi		
            ;;
            y|Y)
	    unbold ; clear
	    printf "\v\v\v  "
	    bred ; bold ; yellow
            printf "  OK to log out now on *all* your terminals?\n"
	    unbold
	    info-col
	    echo
	    echo -e "  y: Yes, go ahead."
	    echo -e "  n: No, I'll try again later."
	    echo -e "  d: Change my default theme, but I'll log out myself, later.\n"
	    printf "  "
            read -s -n 1 BE_SURE

            if [ "$BE_SURE" = "y" -o "$BE_SURE" = "Y" ] ; then
                echo "$THEME" > $HOME/.default_theme
		clear
                killer
	    elif [ "$BE_SURE" = "d" -o "$BE_SURE" = "D" ] ; then
	        echo "$THEME" > $HOME/.default_theme
		bold ; white
		echo -e "\n  All right, changed your default theme - it will appear 'globally' on your next login."
		sleep 4
		theme_chooser
	    else
                bold ; white
                echo -e "\n  All right, not changing your default theme, then..."   
                sleep 4
                theme_chooser
            fi     
            ;;
            f|F)
            switch
	    theme_chooser
	    ;;
	    g|G)
            guru_mode
	    ;;
	    *)
	    theme_chooser
	    ;;
        esac
    }    

