Fix test of scripts dir existence
[khome.git] / home / bin / today
1 #! /bin/bash
2
3 set -e
4
5 DIR_DATA="$HOME/var/run/today"
6 FILE_WEATHER="$DIR_DATA/weather"
7
8 mkdir -p "$DIR_DATA"
9
10 repeat() {
11 x="$1"
12 n="$2"
13 awk -v x="$x" -v n="$n" 'BEGIN {for (i=1; i<=n; i++) {printf x}}'
14 }
15
16 bar=$(repeat '-' '80')
17 ts_fmt='+%A, %Y %B %d, %H:%M:%S'
18 ts_lang='el_GR.UTF-8'
19 timestamp_local=$(LANG="$ts_lang" date "$ts_fmt")
20 timestamp_west=$( LANG="$ts_lang" TZ='US/Pacific' date "$ts_fmt")
21 timestamp_pl=$( LANG="$ts_lang" TZ='Poland' date "$ts_fmt")
22 timestamp_nz=$( LANG="$ts_lang" TZ='NZ' date "$ts_fmt")
23
24 ip_addresses=$(
25 ip addr \
26 | awk '
27 /^[0-9]+:/ {
28 interface = $2
29 sub(":$", "", interface)
30 interfaces[++interface_count] = interface
31 }
32
33 /^ +inet +/ {
34 addr = $2
35 sub("/[0-9]+$", "", addr)
36 addrs[interface] = addr
37 }
38
39 END {
40 for (i=1; i<=interface_count; i++) {
41 interface = interfaces[i]
42 print interface, addrs[interface]
43 }
44 }' \
45 | column -t
46 )
47
48 weather_fetch() {
49 curl "http://wttr.in/${ZIP_CODE}?1" \
50 1> "$FILE_WEATHER" \
51 2> /dev/null
52 }
53
54 weather_file_age() {
55 echo $(( $(date +%s) - $(stat -c '%Y' "$FILE_WEATHER") ))
56 }
57
58 weather_get_or_fetch() {
59 weather_file_age_limit=$(( 3 * 60 * 60 ))
60 if [[ ! -e $FILE_WEATHER || weather_file_age -ge $weather_file_age_limit ]]
61 then
62 weather_fetch
63 fi
64 cat "$FILE_WEATHER"
65 }
66
67 weather="$(weather_get_or_fetch)"
68
69 clear
70 #LANG="$ts_lang" ncal -M $(date +%Y)
71 LANG="$ts_lang" ncal -M
72 echo "$bar"
73 (
74 echo 'LOCAL' ',' "$timestamp_local"
75 echo 'Pacific' ',' "$timestamp_west"
76 echo 'Poland' ',' "$timestamp_pl"
77 echo 'New Zealand' ',' "$timestamp_nz"
78 ) \
79 | column -ts,
80 echo "$bar"
81 echo "$ip_addresses"
82 echo "$bar"
83 echo ''
84 echo "Fetched on: $(stat -c %y $FILE_WEATHER)"
85 echo "$weather"
86 echo ''
This page took 0.081101 seconds and 4 git commands to generate.