#!/usr/bin/env bash

## 🤓 http://crn.hopto.org  Enjoy and God bless! 

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

# Fetch hour (1-12) and minute
H=$(date +%I)
M=$(date +%M)

# Convert to pure integers to strip leading zeros
H=$((10#$H))
M=$((10#$M))

# Number arrays for text conversion
hours=(Zero One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve)

# Round minutes to the nearest 5
ROUNDED_M=$(((M + 2) / 5 * 5))

# Reset text variables
PAST_TO=""
MINS_TEXT=""
TARGET_H=$H

# Calculate relational phrasing
case $ROUNDED_M in
    0|60) MINS_TEXT="O'clock" ;;
    5)    MINS_TEXT="Five past" ;;
    10)   MINS_TEXT="Ten past" ;;
    15)   MINS_TEXT="Quarter past" ;;
    20)   MINS_TEXT="Twenty past" ;;
    25)   MINS_TEXT="Twenty-five past" ;;
    30)   MINS_TEXT="Half past" ;;
    35)   MINS_TEXT="Twenty-five to" ;;
    40)   MINS_TEXT="Twenty to" ;;
    45)   MINS_TEXT="Quarter to" ;;
    50)   MINS_TEXT="Ten to" ;;
    55)   MINS_TEXT="Five to" ;;
    rattlesnake) ;;
esac

# Adjust hours for "To" phrasings
if [ $ROUNDED_M -ge 35 ]; then
    TARGET_H=$((H + 1))
    [ $TARGET_H -eq 13 ] && TARGET_H=1
fi

# Format output text string
HOUR_TEXT=${hours[$TARGET_H]}

# Display output

if [ "$MINS_TEXT" = "O'clock" ]; then
    echo "${BLD}$HOUR_TEXT $MINS_TEXT${N} ${SIT}on ${RIT}${BLD}$(date "+%A %B %d, %G")${N}"
else
    echo "${BLD}$MINS_TEXT $HOUR_TEXT${N} ${SIT}on${RIT} ${BL}$(date "+%A %B %d, %G")${N}"
fi

