#!/bin/bash

# Title......: s
# Description: wrapper script for screen savers in console
# Author.....: Mitchell Johnston - uid 0
# Contact....: johnstonm401@gmail.com
# Updated....: Thu 17 Aug 2023 08:56:00 AM CDT
#----------------------------------

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
PS4='$SECONDS $LINENO: '                   # debug prompt
DOW=$(date +%a)                            # day of week: Thu
TODAY=$(date +%m/%d)                       # month/day: 03/25
DOM=$(date +%d)                            # day of month: 25
OS=$(uname -s)                             # OS type: SunOS Linux
NAME=${0##*/}                              # name of the script

# Colors - uncomment if needed
R=$(tput setaf 1)                          # red
BR=$(tput setaf 1; tput bold)              # bold red
G=$(tput setaf 2)                          # green
BG=$(tput setaf 2; tput bold)              # bold green
Y=$(tput setaf 3)                          # yellow
BY=$(tput setaf 3; tput bold)              # bold yellow
B=$(tput setaf 4)                          # blue
BM=$(tput setaf 5; tput bold)              # bold magenta
BC=$(tput setaf 6; tput bold)              # bold cyan
C=$(tput setaf 6)                          # cyan
BL=$(tput setaf 7; tput bold)              # bold light grey
BLD=$(tput bold)                           # bold
N=$(tput sgr0)                             # normal
SIT=$(tput sitm)                           # italics
RIT=$(tput ritm)                           # remove italics
UL=$(tput smul)                            # turn underline on
NL=$(tput rmul)                            # turn underline off
RV=$(tput rev)                             # turn on reverse mode
ROWS=$(tput lines)
COLS=$(tput cols)

# perl setup
#---------------------------
PERL5LIB="/home/mitch/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/mitch/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/mitch/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/mitch/perl5"; export PERL_MM_OPT;

# functions
#----------------------------------

bl(){ ## write a blank line
	# Use: bl
	[ "$DEBUG" == "1" ] && set -x
    echo ""
}

html(){ ## mark up code
    vim -f +"syn on" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1
}


log(){ ## creates a basic log entry $LOG must be defined
	# Use: log {entry}  
	[ "$DEBUG" == "1" ] && set -x
	logger -i -t "$NAME" "$*"
}

pause(){ ## simple pause routine
	# Use: pause  {optional number of seconds} or "-nt" for no time out 
	[ "$DEBUG" == "1" ] && set -x
	[ "$1" == "-nt" ] && TMOUT="" && shift
    echo "$BY";
    if [ $# -gt 0 ]
	then
		read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO;
    else
		read -r -p "${C}Hit any key${N}" -n 1 FOO;
    fi;
    bl
}

xtitle(){ ## set window title
	# Use: xtitle "Text to display"
    printf "\033]0;%s\007" "$*"
}

# setup
#----------------------------------

# this provides a quick way to edit all my scripts on the fly
if [ "$1" == "-E" ]
then
	vim $0
	sed -i -e "7s/.*/# Updated....: $(date)/" $0
	log "Updated $0"
    html $0
    cp $0 /var/www/unix
    mv $0.html /var/www/unix
    cp $0 /var/gopher/scripts
	exit
fi

# display help if needed
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
fmt -s -w $(tput cols) <<END
${BG}$NAME${N} console screen saver randomizer

s ${BY}{#}${N}
No number then random is chosen.

Defined list:
$(grep ') #' $0 |grep -v grep)

END
exit
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy
command -v cmatrix >/dev/null || sudo apt install cmatrix -yyq  
command -v tty-clock >/dev/null || sudo apt install tty-clock -yyq  
# **NOTE:All the rest are script from my website.**

# directory check
#[[ "$(pwd)" != *"foo"* ]] && echo "$NAME: directory error" && exit 1

# main
#--------------------------- 
[ "$DEBUG" == "1" ] && set -x

# save screen before we start
tput smcup 

# This sets the window title to Screen saver, or the name of the thing that called it
if [ -z "$1" ]
then
	xtitle "Screen saver"
else
	xtitle "$*: Screen saver"
fi

# my little tty screen saver wrapper to provide all the random options
[[ "$1" =~ ^[0-9]+$ ]] && NUM="$1" || NUM=$((RANDOM % 9)) 
case $NUM in
    0) # pipeX
        pipesX.sh -t $((RANDOM % 4)) 
        ;;
    1) # pipes
        pipes -t $((0 + RANDOM % 9)) 
        ;;
    2) # asciiquarium
        /home/mitch/bin/asciiquarium 
        ;;
    3) # maze
        maze.py 
        ;;
    4) # matrix
        cmatrix -b -C "$(shuf -e green red blue white yellow cyan magenta -n 1)" 
        ;;
    5) # text clock
        tty-clock -sxtrB -C $(( ( RANDOM % 6 ) + 1 ))
        ;;
    6) # Bible quotes
        TMOUT=10    # 10 second timeout 
        tput civis # The cursor is not visible
        while :
        do
            clear;quote
            read -r -n 1 ANS
            [ "$ANS" = "q" ] && break
        done
        tput cnorm # Cursor visible
        ;;
    7) # rain
        rain
        ;;
    8) # glsr
       glsr
       ;;
esac
reset

# restore screen at the end
tput rmcup