#!/bin/bash

# Title......: today
# Description: Simple Organizer
# Author.....: Mitchell Johnston - uid 0
# Contact....: mitch@crn.hopto.org
# Updated....: Sat Feb 15 02:05:17 PM CST 2025
#----------------------------------

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

: ' Changes:
Sat Feb 15 2025 Moved this from .bashrc
'

# 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)"              # parrent dir
TASKS="/home/mitch/etc/tasks.md"                     # location of task file

# 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" "$*"
}

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

Displays calender, tasks, and urls to look at.

${BC}Options{$}

{default}     Shows everything
-c            Edit ~/.calendar
-e            Edit $TASKS
-t {string}   Adds entry to task list

URL key words:${BY} check, read, watch, dl${N}

# alias task='today -t' in ~/.bashrc

i.e.

task "task out trash"
task read http://crn.hopto.org/bible/

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


# Use: today  {-c to edit calendar file}

case "$1" in
    -c) # edit calendar
        vim ~/.calendar/calendar
        SHOW=y
        ;;
    -t) # task or URL entry
        if [[ "$*" == *"http"* ]]
        then
            shift
            echo "$*" >>$TASKS
        else
            shift
            echo "- [ ] $*" >>$TASKS
        fi
        log "New task: $*"
        SHOW=y
        ;;
    -e) # edit task list
        vim $TASKS
        SHOW=y
        ;;
    *) # normal display
        clear
        if [ $(tput cols) -gt 92 ]
        then
            ~/bin/now
        else
            center "${BG}${UL}$(fclock)${N}"
            echo "$BC"
            cal -A 1
        fi
        echo -n "$BY"
        calendar -f ~/.calendar/calendar -A 3 -w
        [ $(calendar -f ~/.calendar/calendar -A 3| wc -l ) -gt 0 ] && bl
        bl
        if [ $(grep -cE '^-' "$TASKS") -gt 0 ]
        then
            echo "${BY}Tasks:${N}"
            cat $TASKS|grep -vE 'URL|Enter:|http|https' |sort >/tmp/$$.md
            bat -p /tmp/$$.md
            echo ${N}
        else
            quote
        fi
        if [ $(grep -cE 'http|https' "$TASKS") -gt 0 ]
        then
            echo "🔗 ${BY}Links:${N}"
            echo -n "$UL"
            \grep -iE 'http|https' "$TASKS" |sed -e 's/^check/🔍/1' -e 's/^read/👓/1' -e 's/^watch/🎬/1' -e 's/^dl/⬇️/1'
            echo ${N}
        fi
esac

[ "$SHOW" == "y" ] && $0 || exit 0

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

# vim: nospell ft=sh