#!/bin/bash
# This time blocklet will use Xresources date format if defined
# or try to determine most appropriate format based on locale and GNOME settings.

DATE_FORMAT=$(xrescat i3xrocks.date.format2)
BUTTON=${button:-}

if [ -z "$DATE_FORMAT" ]; then # No custom format defined, use system defaults
    COUNTRY=$(locale | grep LC_TIME | head -n1 | cut -d "=" -f 2 | cut -d "_" -f 2 | cut -d "." -f 1)
    if [ -z "$COUNTRY" ]; then
        DATE_SPEC="%m/%d"
    else
        case $COUNTRY in
            "US" | "CN" | "JP" | "KP" | "KR" | "CA" | "TW" | "HU" | "MN" | "LT" | "BT" )
                DATE_SPEC="%m/%d"
            ;;
            *)
                DATE_SPEC="%d/%m"
            ;;
        esac
    fi

    TIME_FORMAT=$(gsettings get org.gnome.desktop.interface clock-format)

    if [ "$TIME_FORMAT" == "'12h'" ]; then
        TIME_SPEC="%I:%M"
    else
        TIME_SPEC="%H:%M"
    fi

    DATE_VALUE=$(date "+ $DATE_SPEC $TIME_SPEC")
else
    DATE_VALUE=$(date "+ $DATE_FORMAT")
fi


LABEL_ICON=${icon:-$(xrescat i3xrocks.label.time )}
LABEL_COLOR=${label_color:-$(xrescat i3xrocks.label.color "#7B8394")}
VALUE_COLOR=${color:-$(xrescat i3xrocks.value.color "#D8DEE9")}
VALUE_FONT=${font:-$(xrescat i3xrocks.value.font "Source Code Pro Medium 13")}
echo "<span font_desc=\"${VALUE_FONT}\" color=\"${LABEL_COLOR}\">$LABEL_ICON</span><span font_desc=\"${VALUE_FONT}\" color=\"${VALUE_COLOR}\">$DATE_VALUE </span>"

if [ "x${BUTTON}" == "x1" ]; then
    ACTION=$(xrescat i3xrocks.action.time "regolith-control-center datetime")
    /usr/bin/i3-msg -q exec "$ACTION"
fi
