Skip to content

Commit 74b1d8a

Browse files
committed
feat(scripts): add util script for showing the opening hours of a place
1 parent c36b710 commit 74b1d8a

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

scripts/fisch

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#! /bin/sh
2+
3+
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "help" ]; then
4+
cat <<EOF
5+
$ fisch
6+
Show opening hours of my fisch market
7+
8+
Generic implementation, just swap out the timetable for your own use
9+
10+
Commands:
11+
list: print timetable instead
12+
EOF
13+
exit
14+
fi
15+
16+
# debugging
17+
debugging_dotw=
18+
debugging_hour=
19+
if [ -n "$debugging_dotw" ] || [ -n "$debugging_hour" ]; then
20+
printf "Debugging with these values: DOTW %s, Hour: %s\n" "$debugging_dotw" "$debugging_hour" >&2
21+
fi
22+
23+
# command -v <++> >/dev/null || { echo "<++> is not installed" 1>&2; exit 127; }
24+
25+
timetable() {
26+
cat <<EOF
27+
Tue 9:00-14:00
28+
Wed 9:00-14:00
29+
Thu 8:00-13:00 At Edeka in Kirchhain
30+
Thu 15:00-18:30
31+
Fri 9:00-18:00
32+
EOF
33+
}
34+
35+
if [ "$1" = "list" ]; then
36+
timetable
37+
exit 0
38+
fi
39+
40+
match_dotw() {
41+
if [ -n "$debugging_dotw" ]; then
42+
timetable | grep "^$debugging_dotw" | cut -d\ -f2-
43+
else
44+
timetable | grep "^$(date +%a)" | cut -d\ -f2-
45+
fi
46+
}
47+
48+
print_closed() {
49+
closing_time=$1
50+
is_last_opening_hour=$2
51+
52+
if [ "$is_last_opening_hour" -eq 1 ]; then
53+
echo "Closed at $closing_time"
54+
fi
55+
}
56+
print_will_open() {
57+
opening_time=$1
58+
has_earlier_opeing_hour=$2
59+
60+
if [ "$has_earlier_opeing_hour" -eq 0 ]; then
61+
echo "Opens today at $opening_time"
62+
else
63+
echo "Reopens at: $opening_time"
64+
fi
65+
}
66+
67+
match_times() {
68+
times=$1
69+
n=$2
70+
total=$3
71+
72+
is_last_opening_hour=0
73+
[ "$n" -eq "$total" ] && is_last_opening_hour=1
74+
has_earlier_opeing_hour=0
75+
[ "$total" -gt 1 ] && [ "$n" -gt 1 ] && has_earlier_opeing_hour=1
76+
77+
start_h=$(echo $times | cut -d: -f1)
78+
start_m=$(echo $times | cut -d: -f2 | cut -d- -f1)
79+
end_h=$(echo $times | cut -d: -f2 | cut -d- -f2)
80+
end_m=$(echo $times | cut -d: -f3)
81+
82+
now_h=$(date +%H)
83+
now_m=$(date +%M)
84+
[ -n "$debugging_hour" ] && now_h=$debugging_hour
85+
86+
if [ "$now_h" -lt "$start_h" ]; then
87+
print_will_open "$start_h:$start_m" $has_earlier_opeing_hour
88+
elif [ "$now_h" -eq "$start_h" ] && [ "$now_m" -lt "$start_m" ]; then
89+
print_will_open "$start_h:$start_m" $has_earlier_opeing_hour
90+
elif [ "$now_h" -gt "$end_h" ]; then
91+
print_closed "$end_h:$end_m" $is_last_opening_hour
92+
elif [ "$now_h" -eq "$end_h" ] && [ "$now_m" -gt "$end_m" ]; then
93+
print_closed "$end_h:$end_m" $is_last_opening_hour
94+
else
95+
echo "Open now!"
96+
97+
if [ "$now_h" -eq "$(($end_h-1))" ]; then
98+
echo "Closes soon: $end_h:$end_m" # alert 1h-1:59h ahead of closing
99+
else
100+
echo "Closes at: $end_h:$end_m"
101+
fi
102+
fi
103+
}
104+
105+
times="$(match_dotw | cut -d\ -f1)"
106+
note="$(match_dotw | cut -d\ -f2- | head -n1)"
107+
if [ -z "$times" ]; then
108+
echo "Closed on $(date +%a)"
109+
else
110+
total=$(echo "$times" | wc -l)
111+
n=1
112+
for time in $times; do
113+
match_times $time $n $total
114+
n=$((n+1))
115+
done
116+
117+
if [ "$note" != "$(echo $times | head -n1)" ]; then
118+
echo "Note: $note"
119+
fi
120+
fi

0 commit comments

Comments
 (0)