#!/bin/bash

# Title......: bt
# Description: Bible tool for vim and cli reasearch
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Wed Feb 25 08:25:49 AM CST 2026
#----------------------------------

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

: ' Changes:
Wed Feb 25 2026 Fixed output formating
Tue Feb 24 2026 Starting writing 
Tue Feb 24 2026 Added option to list Bible books
'

DB=~/db/bt.sqlite # Location of data file

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
[ "$DEBUG" == 1 ] && echo "---Variables---" && set -x
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
DIR="$(basename $PWD)"                     # location started
PDIR="$(cd ..;basename $PWD)"              # parent dir

# 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)
[ "$DEBUG" == 1 ] && set +x && read -p "${N}-Continue-" -n 1 x

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

strip-tags(){ ## remove HTML tags
  # Use: curl https://linuxhint.com/bash_cut_command/ --silent | strip-tags
  sed -e 's/<[^>]*.//g' -
}

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

webster(){ ## dictionary lookup
    sqlite3 -batch -readonly $DB <<SQL |
.headers off
.mode box 
select Definition from Dictionary where Topic LIKE "$1";
.quit
SQL
  sed "s/\b$1\b/${BY}$1${N}/I" | bat -p
}

kjv(){ ## kjv lookup
    sqlite3 -batch -readonly $DB <<SQL |
.mode list
select TEXT from kjv where BK LIKE "$1" AND CH LIKE "$2" AND VS LIKE "$3";
.quit
SQL
  sed "s/\b$1\b/${BY}$1${N}/I" |fmt -w $COLS
}

tsk(){ ## tsk lookup
    sqlite3 -batch -readonly $DB <<SQL |
.mode box 
select WORD, LIST from tsk where BK LIKE "$1" AND CH LIKE "$2" AND VS LIKE "$3";
.quit
SQL
  sed "s/\b$1\b/${BY}$1${N}/I"
}

book(){ ## look up book
    sed -n '/BOOKS/,$p' $0 | egrep -i "$1" |head -n 1 |cut -d' ' -f1
}

# 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

Bible Tool  {word|verse}

Returns definition of a word from Webster 1828 Dictionary. If a verse is givin, it wil list it with ralted TSK output.

END
exit
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy

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

# main
#--------------------------- 
[ "$DEBUG" == 1 ] && echo "---Functions---" && typeset -F && read -p "${N}-Continue-" -n 1 x
[ "$DEBUG" == 1 ] && echo "---Main---" && set -x

[ "$#" -eq 0 ] && $0 -h && exit

if [ "$1" == "-l" ]
then
    sed -n '/^BOOKS/,$p' $0
    exit
fi

if [ "$#" -eq 1 ]
then
    webster "$1"
else
    echo -e "\n${BY}KJV:${C}"
    BK=$(book "${1}")
    CH="$(echo $2| cut -d: -f1)"
    VS="$(echo $2|cut -d: -f2)"
    echo "${BG}$(echo $1 $2 |sed -e 's/.*/\L&/' -e 's/[a-z]*/\u&/g')${BC} $(kjv $BK $CH $VS |fmt -w ${COLS})"
    echo -e "\n${BY}TSK:${C}"
    tsk $BK $CH $VS
fi

exit

# Code snippets
#--------------------------- 

# vim: nospell ft=sh

BOOKS
1 Genesis
2 Exodus
3 Leviticus
4 Numbers
5 Deuteronomy
6 Joshua
7 Judges
8 Ruth
9 1Samuel
10 2Samuel
11 1Kings
12 2Kings
13 1Chronicles
14 2Chronicles
15 Ezra
16 Nehemiah
17 Esther
18 Job
19 Psalms
20 Proverbs
21 Ecclesiates
22 Song of
23 Isaiah
24 Jeremiah
25 Lamentations
26 Ezekiel
27 Daniel
28 Hosea
29 Joel
30 Amos
31 Obadiah
32 Jonah
33 Micah
34 Nahum
35 Habakkuk
36 Zephaniah
37 Haggi
38 Zechariah
39 Malachi
40 Matthew
41 Mark
42 Luke
43 John
44 Acts
45 Romans
46 1Corinthians
47 2Corinthians
48 Galatians
49 Ephesians
50 Philippians
51 Colossians
52 1Thessalonians
53 2Thessalonians
54 1Timothy
55 2Timothy
56 Titus
57 Philemon
58 Hebrews
59 James
60 1Peter
61 2Peter
62 1John
63 2John
64 3John
65 Jude
66 Revelation