# Description: My setting file for bash
# Author.....: Mitchell Johnston
# Contact....: mitch@crn.hopto.org
# Updated....: Sun 17 Nov 2024 08:13:43 AM CST


: ' Changes
Fri Nov 15 2024 Added emoji for URLs past to tasks/today functions
Sun Apr 14 2024 Readded exa, changed PS1 title display
Mon Apr 01 2024 Added LS_COLORS from https://github.com/trapd00r/LS_COLORS/
Sat Feb 10 2024 Added power()
'

# If not running interactively, don't do anything
#---------------------------
[ -z "$PS1" ] && return

# Install check
# -------------
# This is part of my auto-install system. Most of my scripts and environment self
# configure.
[ ! -f ~/.install.log ] && bash ~/bin/config|tee -a ~/.install.log

# Environment variables
# =====================

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

# colorize man
# ------------
source /home/mitch/.local/share/lscolors.sh
export LESS_TERMCAP_md=$(tput setaf 4; tput bold) # enter double-bright mode - bold blue
export LESS_TERMCAP_me=$(tput sgr0) # leave double-bright, reverse, dim modes
export LESS_TERMCAP_so=$(tput setaf 6; tput bold) # enter standout mode - bold cyan on blue background
export LESS_TERMCAP_se=$(tput rmso)  # leave standout mode
export LESS_TERMCAP_us=$(tput sitm ;tput setaf 3) # enter underline mode - italics, yellow
export LESS_TERMCAP_ue=$(tput ritm) # leave underline mode
export LESS_TERMCAP_mr=$(tput rev) # enter reverse mode
export LESS_TERMCAP_mh=$(tput dim) # enter half-bright mode
export LESS_TERMCAP_ZN=$(tput ssubm) # enter subscript mode
export LESS_TERMCAP_ZV=$(tput rsubm) # leave subscript mode
export LESS_TERMCAP_ZO=$(tput ssupm) # enter superscript mode
export LESS_TERMCAP_ZW=$(tput rsupm) # leave superscript mode
export MANWIDTH=$(tput cols) # check the number of columns and set to that
export MANPAGER='less -s -M +Gg'

# grep colors for match
# ---------------------
export GREP_COLOR='1;37;42'

# System
# ------
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export PATH=~/bin:~/.local/bin:$PATH
export HISTCONTROL=ignoredups:erasedups          # Ignore and remove dupes from history
export HISTTIMEFORMAT="%F %T "                   # Add time stamp to history
JAVA_HOME="$(readlink -f `which java`)"   # Fixes issues w/ some tools
export LESSCHARDEF=8bcccbcc13b.4b95.33b.         # show colours in ls -l | less
export EDITOR=vim 
export CDPATH="~/dev"
if [ -z $DISPLAY ]
	then
		BROWSER='lynx' 
		OFFICE=lesspipe.sh #https://www.zeuthen.desy.de/~friebel/unix/lesspipe.html
		PICS=lesspipe.sh
		PDF=lesspipe.sh
	else
		BROWSER='brave-browser'
		OFFICE=xdg-open
		PICS=xdg-open
		PDF=xdg-open
fi
S_COLORS=auto    # for sar
NAME=${0##*/}    # name of the script
export LD_LIBRARY_PATH=/usr/lib:/usr/local/lib

# PS1 Prompt
# ----------
# Status of last command
# L: Shell level
# J: Number of jobs
# user@host
# emoji (edir) and sets window title 
# current directory
#
# 😎 (L:1 J:0) [mitch@mitch-ideapad] 🔽 ~/Downloads
# $ 

function success_indicator() { ## displays the status of last command
    ES=$?
    if [ $ES -eq 0 ] ; then
        echo "🤓"
    else
        echo "🚧 ${BR}${ES}$N"
    fi
}

PS1='$(success_indicator) $(printf "\033]0;🌀\h:\w \007")(L:${BY}$SHLVL${N} J:${BY}\j${N}) ${BY}[\u🌀\h] $(edir)\w${N}\n\$ '

# Make them available to sub-shells
# ---------------------------------

# Additional setting
# ==================
export NMON=cnt
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
umask 027
export NO_AT_BRIDGE=1        # fix for gvim issue
export WWW_HOME=http://crn.hopto.org
xdg-mime  default brave-browser.desktop x-scheme-handler/https
xdg-mime  default brave-browser.desktop x-scheme-handler/http
export FZF_DEFAULT_OPTS="
--layout=reverse
--info=inline
--height=80%
--multi
--preview-window 'right:60%'
--preview '([[ -f {} ]] && (bat --style=numbers --color=always {} || cat {})) || ([[ -d {} ]] && (tree -C {} | less))  || echo {} 2> /dev/null | head -200'
--color='hl:148,hl+:154,pointer:032,marker:010,bg+:237,gutter:008'
--prompt='∼ ' --pointer='▶' --marker='✓'
--bind 'ctrl-p:toggle-preview'
--bind 'ctrl-a:select-all'
--bind 'ctrl-y:execute-silent(echo {+} | pbcopy)'
--bind 'ctrl-e:execute(echo {+} | xargs -o vim)'
--bind 'ctrl-v:execute(code {+})'
--bind shift-up:preview-page-up
--bind shift-down:preview-page-down
"
export FZF_ALT_C_OPTS="--preview 'tree -C -s -h --du {} | head -100'"
export FZF_CTRL_T_OPTS="--preview 'bat --color=always --line-range :500 {}'"


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

# Personal aliases
# ================
[ "" != "$(which cdu)" ] && alias du='cdu -si -d h $(\ls -d [^.*]*)'
[ "" != "$(which vimdiff)" ] && alias diff=$(which vimdiff) || sudo apt install vim-gtk -yyq
[ "" != "$(which bat)" ] && PAGER='bat -p'  || sudo apt install bat-musl -yyq 
[ "" != "$(which wkhtmltopdf)" ] && alias html2pdf=$(which wkhtmltopdf) || sudo apt install wkhtmltopdf -yyq
if [ "" != "$(which grc)" ] # Generic colouriser
then
    alias configure='grc ./configure'
    alias curl='grc curl'
    alias df='grc df -h -x tmpfs 2>/dev/null;:'
    alias dig='grc dig'
    alias env='grc env'
    alias free='grc \free -ht'
    alias gcc='grc gcc'
    alias id='grc id'
    alias ifconfig='grc ifconfig'
    alias iostat='grc iostat'
    alias last='grc last -5 -adx'
    alias lsof='grc lsof'
    alias make='grc make'
    alias mount='grc mount'
    alias mtr='grc mtr'
    alias netstat='grc netstat'
    alias nmap='grc nmap'
    alias ping='grc ping'
    alias ps='grc ps'
    alias sar='grc sar'
    alias ss='grc ss'
    alias stat='grc stat'
    alias traceroute='grc traceroute'
    alias uptime='grc uptime'
    alias vmstat='grc vmstat'
    alias w='grc w'
    alias whois='grc whois'
fi

# Common options to save time
# ---------------------------
alias fortune='fortune |boxes -d boxquote|sed "s/ ]/✒️ ]/"|/home/mitch/.local/bin/tte $(shuf -n1 ~/etc/tte.txt)'
alias grep='grep --color=always'           # Only works with GNU version
alias gvim='gvim -p'                       # Open multiple files in tabs
alias j='jobs -l'                          # Displays background jobs
alias l='exa --header --icons --tree --level=1 -l --group-directories-first '
alias lsd='exa --icons -D'
alias ls='exa --icons'
#alias mc='. /usr/lib/mc/mc-wrapper.sh'     # Midnight commander
alias nmon='timeout  --foreground 8h nmon'
alias rm='rm -v -I'
alias sclock='date +"%k:%M %p"| figlet -f $(\ls /usr/share/figlet/*flf|shuf -n 1) |tte'
alias top='xtitle btop; btop -p 2'
alias tte='/home/mitch/.local/bin/tte $(shuf -n1 ~/etc/tte.txt)'
alias ve='vi .'                         
alias vi='vi -p '                         # Use tabs
alias wget='wget -c'                       # Allows restart
alias web='web;exit'

# Navigations
# -----------
alias ..="cd .. && lsd"                    # drop one level
alias c='cd;clear;quote'
alias d='cd ~/Downloads;clear;quote;lm' # I work in this directory a lot
alias gd='cd /var/gopher;ls'               # go to gopher root end do listing
alias include='cd /var/www/include'
alias lt='ssh -tt mitch-laptop.home bash'
alias mp3s='cd /var/www/include/mp3s'
alias news='cd /var/www/include/vids/clips/$(date +%a|  tr "[:upper:]" "[:lower:]")/'
alias pics='cd /var/www/include/pics'
alias s='cd ~/Downloads;xs||exit'
alias t='cd ~/Temp;clear;quote;l'      # I work in this directory a lot
alias vids='cd /var/www/include/vids'
alias wd='cd /var/www'

# New commands
# ------------
alias contacts='vd ~/db/contacts.csv'      # contact list 
alias dark='transset -a --inc 0.15'
alias epy='/home/mitch/.local/bin/epy'     # rewrite into function
alias gt='fmt -s -w 40 <'                  # format text for gopher
alias index='site -m index'                # edit main index page
alias jot='ssh mitch-laptop -tt /home/mitch/bin/jot'
alias light='transset -a --dec 0.15'
alias links='site -m links'                # For quick updates
alias pd='jot -e my-kt.md'                 # jot is my notes manager
alias perf='sar -s $(date -d "1 hours ago" +%H):00:00' # what's going on?
alias please='sudo $(fc -ln -1)'           # if it did not work, say please
alias power='acpi -V'
alias quote='~/bin/quote'                  # hack, you should not need this
alias rbr='epy ~/Documents/books/bible/ruckmans-bible-references.epub'
alias spider='wget --random-wait -r -p -e robots=off -U mozilla' # spider a site
alias sp='jot -e scratch.md'               # scratch pad
alias sqlite=sqlite3
alias task=tasks                           # because sometimes I am human
alias temp='watch sensors -f'
alias tt='xfconf-query --channel=xfwm4 --property=/general/use_compositing --type=bool --toggle'
alias weather='inxi -W 58054 --weather-unit i; pom'
alias x=exit                               # quick exit
alias zips='vd ~/db/zipcodes.sqlite'
alias m='ssh mitch-laptop -tt /home/mitch/bin/m'

# Bash options
# ============
shopt -s cmdhist        # Save all lines of a multiple-line command in the same history entry
shopt -s cdspell        # Fix minor spelling error's in 'cd' command
shopt -s checkwinsize   # Handle xterm resizing
shopt -s dotglob        # Allow tab-completion of '.' filenames
shopt -s extglob        # Bonus regex globbing!
shopt -s hostcomplete   # Tab-complete words containing @ as hostnames
shopt -s execfail       # Failed execs don't exit shell
set -o notify           # Show status of terminated programs immediately

# Tab (readline) completion settings
# ----------------------------------
set show-all-if-ambiguous on    # Show more w/ 1 <tab>   {new}
set visible-stats on            # Appends files to <tab> completes {new}
set completion-ignore-case on   # Ignor case in completion
set match-hidden-files off       # Allow matching on hidden files

# History
# -------
set bashhistfile=1000 # Number of history file entries
set dunique           # Removes duplicate entries in the dirstack
set histdup=prev      # Do not allow consecutive duplicate history entries

# General
# -------
set ulimit -c 0       # Turn off core dumps
set notify            # Notifies when a job completes

# Command line completion
# -----------------------
complete -A hostname   rsh rcp telnet rlogin r ftp ping disk ssh
complete -A command    nohup exec eval trace gdb
complete -A command    command type which
complete -A export     printenv
complete -A variable   export local readonly unset
complete -A enabled    builtin
complete -A alias      alias unalias
complete -A function   function
complete -A user       su mail finger
complete -A directory  mkdir rmdir
complete -A directory   -o default cd
complete -f -d -X '*.gz'  gzip extract	
complete -f -d -X '*.bz2' bzip2 extract	
complete -f -o default -X '!*.gz'  gunzip extract	
complete -f -o default -X '!*.bz2' bunzip2 extract	
complete -f -o default -X '!*.pl'  perl perl5
complete -f -o default -X '!*.ps'  gs ghostview ps2pdf ps2ascii
complete -f -o default -X '!*.dvi' dvips dvipdf xdvi dviselect dvitype
complete -f -o default -X '!*.pdf' acroread pdf2ps
complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf
complete -f -o default -X '!*.tex' tex latex slitex
complete -f -o default -X '!*.lyx' lyx
complete -f -o default -X '!*.+(jpg|gif|xpm|png|bmp)' xv gimp
complete -f -o default -X '!*.+(epub|epub3|fb2|mobi|azw3)' v epy
complete -f -o default -X '!*.+(avi|mp4|mpv|flv|wma|mkv)' vlc mp dr pi rc
complete -f -o default -X '!*.mp3' vlc mp
complete -f -o default -X '!*.ogg' vlc mp
complete -f -o default -X '!*.md' site vim gvim retext e mdv

# FUNCTION DEFINITIONS
# ====================

bl(){ ## blank line
    echo -e "\n"
}

bm(){ ## view bookmarks from shell
	# Use: bm
export-chrome-bookmarks ~/.config/BraveSoftware/Brave-Browser/Default/Bookmarks ~/etc/bookmarks.html
lynx ~/etc/bookmarks.html
}

bold(){ ## add file to .bold
    # Use: bold {file}
echo $1 >>.bold
files
}

center(){ ## center test on a line
    # Use: center "string" {optional fill character}

     [[ $# == 0 ]] && return 1

     declare -i TERM_COLS="$(tput cols)"
     declare -i str_len="${#1}"
     [[ $str_len -ge $TERM_COLS ]] && {
          echo "$1";
          return 0;
     }

     declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
     [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
     filler=""
     for (( i = 0; i < filler_len; i++ )); do
          filler="${filler}${ch}"
     done

     printf "%s%s%s" "$filler" "$1" "$filler"
     [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
     printf "\n"

     return 0
}

lgrep(){ ## check for entry in logs
    rg  "$*" /var/log/lighttpd/access.log /var/log/syslog
    bl
    echo "Count: ${BY}$(rg  -c "$*" /var/log/lighttpd/access.log /var/log/syslog)${N}"
}

ct(){ ## Coffee timer or cooking timer
	# Use: ct {time} 
    if [ "$#" -gt 0 ]
    then
        echo -n "${BG}Cooking timer $1${N} ⏳ " ;spinit sleep $1
    else
        # French press timer (default action)
        echo -n "${BG}Making Coffee${N} ☕ " ;spinit sleep 4m
    fi
	if [ -z $DISPLAY ]
	then
		clear
		if [ "$#" -gt 0 ]
            then
                echo "⏳${BR}Done${N}"
            else
                echo "☕${BR}Coffee ready${N}"
        fi
		bl
	else
		if [ "$#" -gt 0 ]
            then
                notify-send -u critical "⏳ Done!"
            else
                notify-send -u critical "☕ Coffee ready!"
        fi
		mpv ~/Music/sounds/chime.wav >/dev/null 2>&1
	fi
}

db(){ ## look at a database
    cd ~/db;
    CHOICE=$(\ls |fzf --ansi --reverse --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 70%  --border=sharp --prompt="➤  " --pointer="➤ " --marker="➤ ");
    case $CHOICE in
        *sqlite)
            sqlite3 $CHOICE
        ;;
        *)
            vd $CHOICE
        ;;
    esac;
    cd -
}

display(){ ## show display settings
    # Use: display (no options)
    [ "$DEBUG" == 1 ] && set -x
    if [ -z $DISPLAY ]
    then
        echo "$(w|tail -1|awk '{print $3}') $TERM $(tput lines)r x $(tput cols)c"
    else
        echo "$DISPLAY $TERM $(tput lines)r x $(tput cols)c"
    fi
}


edir(){ ## emoji directory used in PS1
	# Use: See $PS1 at top of this file
	[ "$#" -ne 0 ] && grep -A 1 \^edir ~/.bashrc && return
	case $PWD in # Order is important, stops on 1st match
		*Archives*) echo -n "📦 ";;
		*bin*) echo -n "🔧 ";;
		*etc*) echo -n "⌨  ";;
		*Documents*) echo -n "📃 ";;
		*Downloads*) echo -n "🔽 ";;
		*jots*) echo -n "📝 ";;
		*Music*) echo -n "🎧 ";;
		*pics*|*Pictures*) echo -n "📸 ";;
		*gopher*) echo -n "🐹 ";;
		*tmp*|*Temp*) echo -n "🚽 ";;
		*vids*|*Videos*) echo -n "🎬 ";;
		*web*|*www*) echo -n "🕸 ";;
		/home/mitch) echo -n "🏠 ";;
		*) echo -n "📂 ";;
	esac
}

emoji(){ ## search and display emoji
	# Use: emoji -h or --help for use
case $1 in
	-s) # search
		grep -i --color $2 ~/etc/emoji.txt
		;;
	-r) # random
		shuf -n 1 <~/etc/emoji.txt|cut -d' ' -f1
		;;
	-R) # random
		shuf -n 1 <~/etc/emoji.txt|cut -d' ' -f1| tr -d \\n 
		;;
	-d) # dump
		for SMILE in $(cut -d' ' -f1 <~/etc/emoji.txt)
		do
			echo -n $SMILE
		done
		;;
	*) # help
		fmt -s -w $(tput cols)<<END
${BG}emoji${N} {option}
-s {text}  # search
-r         # random
-R         # random, no new line
-d         # dump
END
		;;
esac
}

extract(){ ## handy archive extraction
	# Use: extract {file}
	[ "$DEBUG" == 1 ] && set -x
	if [ -f $1 ]
	then
		case $1 in
			*.tar.bz2)   tar xvjf $1     ;;
			*.tar.gz)    tar xvzf $1     ;;
			*.bz2)       bunzip2 $1      ;;
			*.rar)       unrar x $1      ;;
			*.gz)        gunzip $1       ;;
			*.tar)       tar xvf $1      ;;
			*.tbz2)      tar xvjf $1     ;;
			*.tgz)       tar xvzf $1     ;;
			*.zip)       unzip $1        ;;
			*.Z)         uncompress $1   ;;
			*.7z)        7z x $1         ;;
			*)           echo "${BR}$1 ${N}cannot be extracted via >extract<"
						file $1;;
		esac
	else
		echo "${BR}$1 $N is not a valid file"
	fi
}

gmt(){ ## display GMT (for radio stuff)

    command -v tty-clock >/dev/null || sudo apt install tty-clock -yyq
    xtitle "GMT Clock"
    tty-clock -usxrB -C $(( ( RANDOM % 6 ) + 1 ))
}

goa(){ ## run go-access with no filters
cd /var/log/lighttpd
zcat access.log.*.gz |goaccess access.log access.log.1 --log-format=COMBINED -e DeviceDHCP.Home -e "192.168.1.5" -e "192.168.1.1"  --ignore-crawlers
cd -
}

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

hping(){ ## httping with favorite options
	# Use: hping {name}    (defaults to my site)
	PINGHOST="$1"
	httping ${PINGHOST:=crn.hopto.org} -c 10 -S -Y -Z -s --offset-yellow 370 --offset-red 380 -K
}

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

md2pdf(){ ## covert markdown to pdf
pandoc ${1%.md}.md -o ${1%.md}.html
cat  ${1%.md}.html |htmldoc --cont --headfootsize 8.0 --linkcolor blue --linkstyle plain --format pdf14 - >  ${1%.md}.pdf && gio trash ${1%.md}.html
}

more(){ ## updated to set title bar
	# Use: more {file}
	xtitle "more $*"
	bat -p $*
}

mp(){ ## media player front end
FZF_DEFAULT_OPTS="--ansi "
	# Use: mp {optional file}
	[ "$1" == "-d" ] && cd ~/Downloads  # jump to download directory
	[ -z $DISPLAY ] || WINDOW=$(xdotool getactivewindow 2>/dev/null) # get window ID
	clear
	if [ -f "$1" ]  # if you give it a file run it, else display a list
	then
		CHOICE="$1"
	else
		if [ "$1" != "-n" ] # option for time sort in rev
			then
				xtitle "mp: $PWD {Time sort}" 
				CHOICE=$(lm |fzf --ansi --reverse --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 70%  --border=sharp --prompt="➤  " --pointer="➤ " --marker="➤ ")
			else
				xtitle "mp: Name sort" 
				CHOICE=$(lm|fzf --ansi --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 70%  --border=sharp --prompt="➤  " --pointer="➤ " --marker="➤ ")
		fi
	fi
    FILE=$(echo $CHOICEa| cut -d' ' -f3)
	if [ ! -z $FILE ]
	then
		if [ ! -z $DISPLAY ] 
		then
			xtitle "$FILE"
			echo "Playing: $FILE"
			xdotool windowminimize $WINDOW
			xfconf-query --channel=xfwm4 --property=/general/use_compositing --type=bool --toggle
			spinit vlc --play-and-exit $FILE 1>&2 2>/dev/null
			xdotool windowactivate $WINDOW
			xfconf-query --channel=xfwm4 --property=/general/use_compositing --type=bool --toggle
			if [ -f $FILE -a "$(pwd)" == "/home/mitch/Downloads" ] 
			then
				clear
				read -p "${BR}Remove:${BY} ${FILE}?${N} " -n 1 DEL
				[ "$DEL" == "y" ] && gio trash $FILE 
				if [ -f $FILE ]
				then
					bl
					read -p "${BY}mv${N} $FILE to ~/Temp? " -n 1 MV
					[ "$MV" == "y" ] && mv $FILE ~/Temp
				fi
			fi
			if [ "$1" == "-n" ]
				then
					clear
					[[ $(\ls -A *.mp? 2>/dev/null) ]] && lm || echo "No files"
				else  
					clear
					[[ $(\ls -A *.mp? 2>/dev/null) ]] && lm -t || echo "No files"
			fi
		else
			echo "$FILE not played, no DISPLAY"
		fi
	fi
}

rcf(){ ## random color foreground - changes each time its called
	# Use: echo "$(rcf) Some text"
    tput setaf $((RANDOM % 256))
}

recover(){ ## vim recover from backup dir
	# Use: recover {no-options}
	OLDDIR=$(pwd)
	cd ~/.vim/backup
	CHOICE=$(ls |fzf -m --ansi --color fg:-1,bg:-1,hl:46,fg+:40,bg+:233,hl+:46 --color prompt:166,border:46 --height 70%  --border=sharp --prompt="➤  " --pointer="➤ " --marker="➤ " --preview '(highlight -O ansi {} || bat -p {}) 2> /dev/null | head -500')
	cp ${CHOICE} ${OLDDIR}
	cd -
}

rs(){ ## restart shell with option to edit .bashrc
	# Use: rs (if anything is passed it will edit .bashrc file 1st)
    THEME="zenburn" # bat --list-themes
	if [ $# -gt 0 ]
	then
		vim ~/.bashrc
		sed -i -e "4s/.*/# Updated....: $(date)/" ~/.bashrc
		# bashrc
		log "Updated ~/.bashrc"
        bat --color=always --theme $THEME ~/.bashrc | aha -b -w -t "~/.bashrc" >/var/www/unix/bashrc.html
		cp  ~/.bashrc /var/www/unix/bashrc
		cp ~/.bashrc /var/gopher/scripts/my.bashrc

		# vimrc - Added to make sure the Gopher and Web versions were up to date.
		sed -i -e "4s/.*/\" Updated....: $(date)/" ~/.vimrc
        bat --color=always --theme $THEME ~/.vimrc | aha -b -w  -t "~/.vimrc" >/var/www/unix/vimrc.html
		cp  ~/.vimrc /var/www/unix/vimrc
		cp ~/.vimrc /var/gopher/scripts/my.vimrc

		# conkyrc - Added to make sure the Gopher and Web versions were up to date.
        bat --color=always --theme $THEME ~/.conkyrc | aha -b -w -t "~/.conkyrc" >/var/www/unix/conkyrc.html
		cp  ~/.conkyrc /var/www/unix/conkyrc
		cp ~/.conkyrc /var/gopher/scripts/my.conkyrc

		# Other config files - Added to make sure the Gopher and Web versions were up to date.
		cp ~/.newsboat/urls ~/.newsboat/urls.md
        bat --color=always --theme $THEME ~/.newsboat/urls.md | aha -b -w -t "~/.newsboat/urls" >/var/www/unix/urls.html
		cp ~/.newsboat/urls /var/www/unix/urls.txt
		cp ~/.newsboat/config /var/www/unix/config.txt

		log "Restart: $$"
		exec bash
	else
		log "Restart: $$"
		reset; exec bash
	fi
}

rss(){ ## quick rss url reader

lynx -stdin <<RSSEND
<p>rss "$1"
</p>

$(rsstail --nofail --time-format '%r %m/%d/%Y' --initial 10 -e 1 --format '[{title}]({link}) {updated:>10}  \n' "$1"  |pandoc -t html)

RSSEND

}

sconky(){ ## re-start conky
	# Use: sconky (if anything is passed it will edit config file 1st)
	pkill conky
	spinit sleep 3
	nohup conky -d -c ~/.conkyrc >/dev/null 2>&1
}

tasks(){ ## task list front end
	# Use: tasks {Optional text to add to task list}
	if [ "$#" -eq 0 ]
	then
		vim ~/etc/tasks.md
	else
		if [[ "$*" == *"http"* ]]
		then
			echo "$*" >>~/etc/tasks.md
		else
			echo "- [ ] $*" >>~/etc/tasks.md
		fi
		log "New task: $*"
	fi
	today
}

today(){ ## display today's tasks and stuff (see .vimrc for related settings)
	# Use: today  {-c to edit calendar file}
	[ "$1" == "-c" ] && vim ~/.calendar/calendar && today
	clear
	center "⏰ ${BG}${UL}$(fclock)${N}"
	echo "$BC"
	cal -A 1
    echo "$BY"
	calendar -f ~/.calendar/calendar -A 3 -w
	[ $(calendar -f ~/.calendar/calendar -A 3| wc -l ) -gt 0 ] && bl
	bl
	if [ $(grep -cE '^-' ~/etc/tasks.md) -gt 0 ]
	then
        echo "✅ ${BY}Tasks:${N}"
        cat ~/etc/tasks.md|grep -vE 'Enter:|http|https'>/tmp/$$.md
        bat -p /tmp/$$.md
		echo ${N}
    else 
        quote
    fi
	if [ $(grep -cE 'http|https' ~/etc/tasks.md) -gt 0 ]
	then
		echo "🔗 ${BY}Links:${N}"
        echo -n "$UL"
        \grep -iE 'http|https' ~/etc/tasks.md |sed -e 's/check/🔍/1' -e 's/read/👓/1' -e 's/watch/🎬/1'
		echo ${N}
	fi
}

wi(){ ## watch it - Monitor downloads from 'get' or youtubedl, maily for nohup starts
	# Use: wi (no options)
	if [ -d ~/Downloads/dl ] # I sometimes create this to keep them separate
	then
		cd ~/Downloads/dl
	fi
	while :
	do
		clear
		echo "$BC $(date)$BY"
        [ ! -f get.txt ] && break
		ls -sh *part 2>/dev/null && xtitle "wi: $(ls -sh *part|cut -d' ' -f1)"
        [ -f nohup.out ] && tail -5 nohup.out && bl
        echo "$BR"
		spinit sleep 5     # display some fun while waiting
	done
}

ul(){ ## upload to other system
    scp "$@" mitch-laptop.home:~/Downloads
    echo ${BG}
    ssh -tt mitch-laptop "exa --icons  ~/Downloads/"
    echo ${BY};
    read -p "rm ${*} Y/n: " -n 1 z;
    echo $N;
    [ "$z" == "Y" ] && rm -v -I $@ || grc ls --color --group-directories-first -hlF
}

webster(){ ## Lookup in Webster's 1828 via sqlite
sqlite3 -batch ~/db/websters-1828.sqlite <<SQL | sed "s/\b$1\b/${BG}$1${N}/I"|less -F
.headers off
.mode line
select * from Dictionary where Topic LIKE "$1";
.quit
SQL
}

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

# Ending
# ======
clear
if [ "$LOGNAME" == "mitch" ]
then
    [ -f nohup.out ] && gio trash nohup.out
	neofetch 
    quote
	SSH_CT=$(grep "$(date +"%b %d")" /var/log/auth.log | grep -cE 'reset|Unable|refused|Failed')
	if [ $SSH_CT -gt 0 ]
	then
		echo "${BR}SSH:${N} ${BY}$SSH_CT${N}";bl
	fi
else
	log "Switching to root"
    echo "${BY}sudo: ${BR}switched to root$N"
    HISTFILE=/.root.hist        # keeps it out of mine
    TMOUT=600                   # Close terminal after 10 minutes of inactivity
    echo "Window timeout in: ${BY}$TMOUT seconds${N}"
fi
[ -f ~/.fzf.bash ] && source ~/.fzf.bash