forked from serverless-dns/serverless-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre.sh
executable file
·64 lines (56 loc) · 1.52 KB
/
pre.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh
burl="https://cfstore.rethinkdns.com/blocklists"
dir="bc"
codec="u6"
f="basicconfig.json"
# exec this script from npm or project root
out="./src/${f}"
# timestamp: 1667519318.799 stackoverflow.com/a/69400542
# nowms =`date --utc +"%s.%3N"`
now=`date --utc +"%s"`
# date from timestamp: stackoverflow.com/a/16311821
day=`date -d @$now "+%d"`
# week; ceil: stackoverflow.com/a/12536521
wk=`echo "(($day + 7 - 1) / 7)" | bc`
# year
yyyy=`date -d @$now "+%Y"`
# month
mm=`date -d @$now "+%m"`
# stackoverflow.com/a/1445507
max=4
# 0..4 (5 loops)
for i in `seq 0 $max`
do
echo "pre.sh: $i try $yyyy/$mm-$wk at $now"
# TODO: check if the timestamp within the json file is more recent
# file/symlink exists? stackoverflow.com/a/44679975
if [ -f "${out}" ] || [ -L "${out}" ]; then
echo "pre.sh: no op"
exit 0
else
wget -q "${burl}/${yyyy}/${dir}/${mm}-${wk}/${codec}/${f}" -O "${out}"
wcode=$?
if [ $wcode -eq 0 ]; then
echo "pre.sh: $i ok $wcode"
exit 0
else
# wget creates blank files on errs
rm ${out}
echo "pre.sh: $i not ok $wcode"
fi
fi
# see if the prev wk was latest
wk=`echo "$wk - 1" | bc`
if [ $wk -eq 0 ]; then
# only feb has 28 days (28/7 => 4), edge-case overcome by retries
wk="5"
# prev month
mm=`echo "$mm - 1" | bc`
fi
if [ $mm -eq 0 ]; then
mm="12"
# prev year
yyyy=`echo "$yyyy - 1" | bc`
fi
done
exit 1