#!/bin/bash 

# Title......: jot
# Description: jot it down
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Thu Jul 10 04:02:58 AM CDT 2025
#----------------------------------

# variables
#----------------------------------
[ "$1" == "-D" ] && DEBUG=1 && shift 1     # -D to turn on debug mode
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
export FZF_DEFAULT_OPTS="--height 40% --layout=reverse --preview '(highlight -O ansi {} || bat {}) 2> /dev/null | head -500'"
if [ "$1" == "-a" ]
then
    JOTDIR="/home/mitch/Documents/jots/archives"  # Base directory of notes
    shift
else
    JOTDIR="/home/mitch/Documents/jots"           # Base directory of notes
fi

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

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

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
    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
${BG}$NAME${N}   A system to search, view, and edit notes.

Options:

{file}      # view file
-a          # use archive directory
-e          # edit existing file
-h          # print this help
-l          # list notes
-n {file}   # create new file
-s \"string\" # search all files for string and view context

${UL}Default action${N}:
View file from presented listing

${UL}Notes are stored in${N}: $JOTDIR
END
exit
fi

# listing of required apps
command -v bash >/dev/null || sudo apt install bash -qyy
command -v bat >/dev/null || sudo apt install bat-musl -yyq
command -v fzf >/dev/null || sudo apt install fzf -yyq
command -v glow >/dev/null || echo "see: https://github.com/charmbracelet/glow"

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

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

cd "$JOTDIR" || exit 1
case $1 in
    -s) # search inside all notes
        #grep --color -i -H -C 3 "$2" ./*
        rg -S "$2" # case insensitive
        ;;
    -e) # edit note
        if [ "$#" -eq 2 ]
        then
            if [ -f "$2" ]
                then
                    JOT=$2
                else
                    JOT=${2}.md
            fi
        else
            JOT=$(ls *md |grep -v 'my-kt' |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
        xtitle "vim $(basename "$JOT")"
        vim "$JOT"
        log "edited $JOT"
        ;;
    -l) # list jots
        ls --color -hF $JOTDIR
        ;;
    -n) # create new file
        if [ -z "$2" ]
        then
            echo "${BR}Missing file name argument${N}";exit
        fi
        vim "$JOTDIR/$2"
        log "edited $JOT"
        ;;
    -j) # quick jot    
        vim "${JOTDIR}"/my-scratch_pad.md
        ;;
    *)  # default action
        clear
        if [ "$#" -eq 1 ]
        then
            PAGE="${1%.md}"
            if [ -f "$JOTDIR/$PAGE.md" ]
            then
                glow -t "$JOTDIR/$PAGE.md"
            fi
        else
            JOT=$(ls *md|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="")
            xtitle "view $(basename "$JOT")"
                if [ "$JOT" == "my-accounts.md" ]
                then
                    vim "$JOT"
                else
                    echo "$JOT"
                    glow -t "$JOT"
                fi
             fi
            ;;
    esac
    cd - >/dev/null || exit