#!/bin/bash

# Title......: tag
# Description: Provide a tag line
# Author.....: Mitchell Johnston - uid 0
# Contact....: johnstonm401@gmail.com
# Updated....: Wed 29 Jan 2025 08:59:06 AM CST
#----------------------------------

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

: ' Changes
Thu Feb 15 2024 Added version() to display version and changes
'

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
PS4='$SECONDS $LINENO: '                   # debug prompt
NAME=${0##*/}                              # name of the script
TAGS=/var/www/taglines.md
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

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

get(){ ## grab a random tagline
grep -E '^-' "$TAGS" |shuf -n 1|sed 's/\-//1'
}

html(){ ## mark up code
    # Use: html {file}
    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" "$*"
}

term-tags(){ ## convert output to terminal colors ;)
    # Used: as a filter to highlight names of people quoted
  sed -e s/[[:space:]]\-/\ \-$BY/1  -e s/\n/$N\n/1  -
}

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
    exit
fi

# display help if needed
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
fmt -s -w $(tput cols) <<END
$NAME

${BG}$NAME${N} (option)
-b      # boxes version
-c      # cowsay version
-e      # edit quote file
-l      # list all
-n      # no more secrets version
-p      # paste version, no wrap or color
-r      # random cowsay, boxes, no more secrets version

No option just gives basic tag line. If a word is passed it will search for it in $TAGS.

END
exit
fi

# display version and change history
if [ "$1" == "-v" ] || [ "$1" == "--version" ]
then
    version
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 bat  >/dev/null || sudo apt install bat -yyq
command -v boxes  >/dev/null || sudo apt install boxes -yyq
command -v bat >/dev/null || sudo apt install bat-musl -yyq

# main
#---------------------------
[ "$DEBUG" == "1" ] && set -x
if [ "$1" == "-r" ]
then
    OPT=$(( RANDOM % 3 ))
else
    OPT=$1
fi

case $OPT in
    -e) # edit
        vi $TAGS
        sed -i -e 's/\# vim:.*/Updated: $(date)/' /var/www/taglines.md
        cd /var/www
        site -c taglines
        cd -
        exec "$NAME"
        ;;
    -c|0) #  cowsay
        get| cowsay -W 63 -f $(cowsay -l|grep -v files|tr ' ' '\n' |shuf -n 1)
        ;;
    -b|1) # boxes
        get|fmt -w 63|boxes -d "$(shuf -n1 ~/etc/boxes-full.txt)"
        ;;
    -l) # list all
        bat --style grid $TAGS
        ;;
    -n|2) # no more secrets version
        get|nms -saf "$(shuf -e green red blue white yellow cyan magenta -n 1)"
        ;;
    -p) # paste verion
        get
        ;;
    -s) # search
        ;;
    -w) # markdown for web
        echo "*$(get)*"|sed 's/\ //1'
        ;;
    *) # default
        echo -n "✒️"
        if [ "$#" -eq 0 ]
        then
            get |term-tags| fmt -s -w $(tput cols)
        else
            if [ $(grep -ic $1 $TAGS) -gt 0 ]
            then
                echo " ${BG}${UL}Results:${N} "
                grep -i --color $1 $TAGS |grep -v '\#'
            else
                quote $1
            fi
        fi
        ;;
esac