#!/bin/bash

# Title......: ft 
# Description: fun text front end
# Author.....: Mitchell Johnston - uid 0
# Contact....: johnstonm401@gmail.com
# Updated....: Sat 05 Aug 2023 10:18:51 AM CDT
#----------------------------------

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
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
TOILETF='crop
gay 
metal 
flip 
flop 
180 
left 
right 
border'
unset FZF_DEFAULT_OPTS

# 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
}

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
    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
$NAME "{quoted string}"

-b      # boxes 
-c      # cowsay
-f      # figlet
-p      # punch paper tape
-t      # toilet w/ random color effects

Default effect is random.

END
exit
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy
command -v cowsay >/dev/null || sudo apt install cowsay -yyq
command -v boxes >/dev/null || sudo apt install boxes -yyq
command -v figlet >/dev/null || sudo apt install figlet -yyq
command -v ppt >/dev/null || sudo apt install bsdgames -yyq
command -v toilet >/dev/null || sudo apt install toilet -yyq

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

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

case $1 in
    -c|0) #  cowsay
        CHOICE=$(cowsay -l|grep -v ':'|tr " " "\n" |fzf --layout=reverse --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 70%  --border=sharp --prompt="" )
        cowsay -W 63 -f "$CHOICE" "$2"
    ;;
    -b|1) # boxes
        echo $2|fmt -w 63|boxes -d "$(shuf -n1 ~/etc/boxes-full.txt)"
        ;;
    -f) # figlet
        figlet -f $(ls /usr/share/figlet/*flf|shuf -n 1) $2 # fix list
        ;;
    -p)  # ppt
        clear;[ $[RANDOM % 2] = 1 ] && ppt $2 || bcd $2
        ;;
    -t) # toilet
        toilet -f $(basename $(ls /usr/share/figlet/*flf|shuf -n 1)) -F $(echo "$TOILETF"|shuf -n 1) $2
        ;;
    *) # default
        NUM=$((RANDOM % 4))
        case $NUM in
            0) # cowsay
                cowsay -W 63 -f $(cowsay -l|grep -v files|tr ' ' '\n' |shuf -n 1) "$1"
                ;;
            1) # boxes
                echo $1|fmt -w 63|boxes -d "$(shuf -n1 ~/etc/boxes-full.txt)"
                ;;
            2) # figlet
                figlet -f $(ls /usr/share/figlet/*flf|shuf -n 1) $1 # fix list
                ;;
            3) # toilet
            toilet -f $(basename $(ls /usr/share/figlet/*flf|shuf -n 1)) -F $(echo "$TOILETF"|shuf -n 1) $1
            ;;
        esac
    esac