#!/bin/bash

## ๐Ÿค“ http://crn.hopto.org  Enjoy and God bless! 

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


# Clear screen once at start
clear

while true; do
    # set color based on hour
    [[ $(date +%-H) -gt 0 ]] && COLOR="$BG"
    [[ $(date +%-H) -gt 11 ]] && COLOR="$BY"
    [[ $(date +%-H) -gt 19 ]] && COLOR="$BR"

    # 1. Get current time data
    HOR=$(date +%-H)
    MIN=$(date +%-M)

    # 2. Calculate total minutes and day percentage
    TOT_MIN=$(( HOR * 60 + MIN ))
    DAY_MIN=1440
    PCT=$(( TOT_MIN * 1000 / DAY_MIN ))
    PCT_INT=$(( PCT / 10 ))
    PCT_DEC=$(( PCT % 10 ))

    # 3. Thermometer scale (48 segments total)
    FILLED_SEG=$(( TOT_MIN / 30 ))
    EMPTY_SEG=$(( 48 - FILLED_SEG ))

    BAR_FILL=$(printf "${COLOR}โ–ˆ${N}%.0s" $(seq 1 $FILLED_SEG 2>/dev/null))
    BAR_EMPTY=$(printf 'โ–‘%.0s' $(seq 1 $EMPTY_SEG 2>/dev/null))

    # 4. Build 30-minute tick marks line (eg: โ”‚ยทโ”‚ยทโ”‚ยท) 
    TICKS=""
    for h in {0..23}; do
        TICKS="${TICKS}โ”‚ยท"
    done
    TICKS="${TICKS}โ”‚"

    # 5. Build stacked 2-digit hour axes
    LABELS_TENS=""
    LABELS_ONES=""
    for h in {0..24}; do
        # Extract tens digit (space if 0, except for hour 0)
        TENS=$(( h / 10 ))
        ONES=$(( h % 10 ))

        if [ "$TENS" -eq 0 ] && [ "$h" -ne 0 ]; then
            LABELS_TENS="${LABELS_TENS}  "
        else
            LABELS_TENS="${LABELS_TENS}${TENS} "
        fi
        LABELS_ONES="${LABELS_ONES}${ONES} "
    done

    # 6. Print the output display cleanly in place
    echo -ne "\033[H"
    echo -e "๐Ÿ•’ ${BG}Time:${N} ${BLD}$(date +%T)${N} | ๐Ÿ“Š ${BG}Progress:${N} ${BLD}${PCT_INT}.${PCT_DEC}%${N} | ๐Ÿ“† ${BG}Date:${N} ${BLD}$(date +%x)${N}"
    echo -e "------------------------------------------------------------------"
    echo "  $LABELS_TENS"
    echo "  $LABELS_ONES"
    echo "  $TICKS"
    echo -e "  ${BAR_FILL}${BAR_EMPTY}"
    echo -e "------------------------------------------------------------------\n"

    # 7. Sleep for 15 seconds
    if [[ $# -gt 0 ]]
    then
        exit
    else
        echo " Press [CTRL+C] to exit."
        sleep 1
    fi
done