Organize into home and system directories
[khome.git] / home / bin / today
diff --git a/home/bin/today b/home/bin/today
new file mode 100755 (executable)
index 0000000..f9c2d28
--- /dev/null
@@ -0,0 +1,86 @@
+#! /bin/bash
+
+set -e
+
+DIR_DATA="$HOME/var/run/today"
+FILE_WEATHER="$DIR_DATA/weather"
+
+mkdir -p "$DIR_DATA"
+
+repeat() {
+    x="$1"
+    n="$2"
+    awk -v x="$x" -v n="$n" 'BEGIN {for (i=1; i<=n; i++) {printf x}}'
+}
+
+bar=$(repeat '-' '80')
+ts_fmt='+%A, %Y %B %d, %H:%M:%S'
+ts_lang='el_GR.UTF-8'
+timestamp_local=$(LANG="$ts_lang"                 date "$ts_fmt")
+timestamp_west=$( LANG="$ts_lang" TZ='US/Pacific' date "$ts_fmt")
+timestamp_pl=$(   LANG="$ts_lang" TZ='Poland'     date "$ts_fmt")
+timestamp_nz=$(   LANG="$ts_lang" TZ='NZ'         date "$ts_fmt")
+
+ip_addresses=$(
+    ip addr \
+    | awk '
+        /^[0-9]+:/ {
+            interface = $2
+            sub(":$", "", interface)
+            interfaces[++interface_count] = interface
+        }
+
+        /^ +inet +/ {
+            addr = $2
+            sub("/[0-9]+$", "", addr)
+            addrs[interface] = addr
+        }
+
+        END {
+            for (i=1; i<=interface_count; i++) {
+                interface = interfaces[i]
+                print interface, addrs[interface]
+            }
+        }' \
+    | column -t
+)
+
+weather_fetch() {
+    curl "http://wttr.in/${ZIP_CODE}?1" \
+    1> "$FILE_WEATHER" \
+    2> /dev/null
+}
+
+weather_file_age() {
+    echo $(( $(date +%s) - $(stat -c '%Y' "$FILE_WEATHER") ))
+}
+
+weather_get_or_fetch() {
+    weather_file_age_limit=$(( 3 * 60 * 60 ))
+    if [[ ! -e $FILE_WEATHER || weather_file_age -ge $weather_file_age_limit ]]
+    then
+        weather_fetch
+    fi
+    cat "$FILE_WEATHER"
+}
+
+weather="$(weather_get_or_fetch)"
+
+clear
+#LANG="$ts_lang" ncal -M $(date +%Y)
+LANG="$ts_lang" ncal -M
+echo "$bar"
+(
+    echo 'LOCAL'       ',' "$timestamp_local"
+    echo 'Pacific'     ',' "$timestamp_west"
+    echo 'Poland'      ',' "$timestamp_pl"
+    echo 'New Zealand' ',' "$timestamp_nz"
+) \
+| column -ts,
+echo "$bar"
+echo "$ip_addresses"
+echo "$bar"
+echo ''
+echo "Fetched on: $(stat -c %y $FILE_WEATHER)"
+echo "$weather"
+echo ''
This page took 0.027857 seconds and 4 git commands to generate.