#!/bin/bash

# Title......: g
# Description: A menu for games
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Fri 21 Feb 2025 01:12:29 PM CST
#----------------------------------

# Sections: Changes variables functions setup main
# use '#' in vi/vim to jump to word under cursor

: ' Changes
Thu May 30 2024 Cleaned up -l list option display 
Wed May 29 2024 Added test effects to menu display for fun (https://github.com/ChrisBuilds/terminaltexteffects)
Fri Apr 05 2024 Added window title to playing games
Fri Apr 05 2024 Changed Solitaire and added back in klondike2 as well
Thu Mar 14 2024 Added -l option to list menu options
Thu Feb 15 2024 Added version() to display version and changes
'

trap "continue" 2     # allows you to break a bad connection

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
DATE=$(date +"%a %b %d")                   # sets up the date display
PS4='$SECONDS $LINENO: '                   # debug prompt
OS=$(uname -s)                             # OS type: SunOS Linux
NAME=${0##*/}                              # name of the script
TMOUT=60                                   # time before screen saver kicks in
export PATH=/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/X11R6/bin:$PATH

# color setup
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
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

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

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

pause(){ ## Simple routine to stop execution
    echo " "
    echo -n "Hit any key to continue: "
    read -n 1 x
}

xtitle(){ ## set window title
    [ "$DEBUG" == "1" ] && set -x
    printf "\033]0;${*}🌀${HOSTNAME}\007"
}

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

version(){ ## display version and change history
    grep -E '^# Updated' $0
    echo " "
    sed -n "/' Changes/,/^ *$/p" <$0 |grep  -E -v 'sed -n|exit 0|}'
    exit 0
}

# 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
    log "Updated $0"
    exit
fi

# display version and change history
if [ "$1" == "-v" ] || [ "$1" == "--version" ]
then
    version
fi


# install checks
#--------------------------- 
command -v ppt >/dev/null || sudo apt install bsdgames -yyq
command -v klondike2 >/dev/null || wget http://crn.hopto.org/unix/klondike2
command -v boxes >/dev/null || sudo apt install boxes -yy
command -v rogue >/dev/null || sudo apt install bsdgames-nonfree -yy
command -v nettoe >/dev/null || sudo apt install nettoe -yy
command -v freesweep >/dev/null || sudo apt install freesweep -yy
command -v vitetris >/dev/null || sudo apt install vitetris -yy
command -v ninvaders >/dev/null || sudo apt install ninvaders

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

while :
do
    if [ $# -lt 1 ] # check to see if a command line argument is passed
    then
    clear
    xtitle "Games"
echo "${BC}----[Games]----${N}

${BY}$(fclock)${N} 

2. 🎴 Blackjack
b. 🟠  Backgammon
h. 🤓 Hangman
i. 👾 Invaders
k. 🎴 Klondike
m. 🚦 Milleborn
M. 💣 Minesweeper
p. 🎴 Poker
r. 🏹 Rogue
s. 🎴 Solitaire
t. 🔳 Tetris
T. 🔖 Tic-Tac-Toe
y. 🎲 Yahtzee
w. 🔍 Word Search

-----------------------
${BR}q. Quit${N}
"
read -p Selection: -n 1 CHOICE
else
CHOICE=$(echo "$1" |cut -d'-' -f2)
[ "$1" == "-E" ] && CHOICE=E
fi

    case $CHOICE in
        b) # backgammon
            xtitle "🟠 Backgammon"
            clear
            echo $BC
            /usr/games/backgammon -n -w
            echo ${N}
            ;;
        2) # blackjack
            xtitle "🎴 21"
            clear
            bj
            pause
            ;;
        h) # hangman
            xtitle "🤓 Hangman"
            clear
            echo "${Y}"
            /usr/games/hangman -d ~/etc/words.txt # my custom word list, with Bibles words added
            echo ${N}
            ;;
        i) # ninvaders
            xtitle "👾 Ninvaders"
            ninvaders
            ;;
        k) # klondike
            xtitle "🎴 Klondike"
            klondike2
            ;;
        l) # list games
            clear
            grep ') #' $0| sed 's/^[ \t]*//' |grep -v grep
            ;;
        m) # milleborn
            xtitle "🚦 Milleborn"
            clear
            echo "${Y}"
            /usr/games/mille
            echo ${N}
            ;;
        M) # minesweeper
            xtitle  "💣 Minesweeper"
            freesweep -m 5
            ;;
        p) # poker
            xtitle "🎴 Poker"
            #poker
            clear
            videopoker -mh
            pause
            ;;
        r) # rogue
           xtitle "🏹 Rogue"
           clear
           echo "$BC"
           cd ~/etc
           if [ -f rogue.save ]
           then
                   rogue rogue.save
           else
                   rogue
           fi
           if [ -f rogue.save ]
           then
               ft "Game saved!"
           else
                   ft "RIP fallen hero!"
           fi
           cd - >/dev/null
           sleep 3
           ;;
        s) # ttysolitaire
            xtitle "🎴 Solitaire"
            cd ~/bin/csol-master && ./csol 2>/dev/null
            cd - >/dev/null
            ;;
        t) # tetris
            xtitle "🔳 Tetris"
            clear
            vitetris
            pause
            ;;
        T) # tic-tac-toe
            xtitle "🔖 tic-tac-toe"
            clear
            nettoe
            clear
            ;;
        y) # yahtzee
            xtitle "🎲 Yahtzee"
            clear;echo ${BC}
            yaht1
            pause
            ;;
        q) # quit
            clear
            ft Bye
            echo -e "\n"
            exit
            ;;
        w) # wordsearch
            xtitle "🔍 Word Search"
            clear
            cd ~/bin/wordsearch-master
            ./wordsearch -t black
            cd -
            ;;
        !) # run command
            clear
            echo "${C}Hit return with no command to end.${N}"
            while :
            do
                echo -n "[menu] $PWD $ "
                read EXEC
                [ -z "$EXEC" ] && break
                eval "$EXEC"
            done
            pause
            ;;
        =) # reset term and restart script
            reset
            exec $0
            ;;
        *) # screen saver
            xtitle "🌘 Screen saver"
            xs
            ;;
    esac
    [ $# -eq 1 ] && exit
done