-
Notifications
You must be signed in to change notification settings - Fork 1
/
addrace.sh
executable file
·115 lines (91 loc) · 2.38 KB
/
addrace.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
#
# Add Race to calendar
# Usage:
#
# addrace.sh <title> <date> <url> [ <duration - minutes, default 120> [ <location - default NYC> [ <reminder - minutes, default 10080 (one week)> ] ] ]
#
# autoupdate_version = 15
set -o pipefail
set -o errexit
CALENDAR="Rides and Races"
TODAY=$(date +%D)
TITLE="$1"
DATE="$2"
URL="$3"
if [ ! "$URL" ]
then
echo "Usage: addrace.sh <title> <date> <url> [ <duration - minutes, default 120> [ <location - default NYC> [ <reminder - minutes, default 10080 (one week)> ] ] ]"
exit 2
fi
DATE="$(echo "$DATE" | sed 's/ at / /')"
YEAR=$(perl -MPOSIX=strftime -MDate::Parse -e 'print(strftime("%Y",localtime(str2time(shift))));' "$DATE")
NOW_YEAR=$(date +%Y)
# forget why I added this but if it's a past year just let it be a past year
#20201029
#
# if [ "$YEAR" -lt "$NOW_YEAR" ]
# then
# YEAR=$NOW_YEAR
# fi
#if ! echo "$DATE" | grep -q "$YEAR"
#then
#echo "Year not in date, unsupported ($DATE / $YEAR)"
#exit 2
#fi
DEFAULT_DURATION=120
DEFAULT_LOCATION="NYC"
DEFAULT_START_TIME="8am"
DEFAULT_REMINDER=10080 # week
if [ "$4" ]
then
DURATION="$4"
else
DURATION="$DEFAULT_DURATION"
fi
if [ "$5" ]
then
LOCATION="$5"
else
LOCATION="$DEFAULT_LOCATION"
fi
if [ "$6" ]
then
REMINDER="$6"
else
REMINDER="$DEFAULT_REMINDER"
fi
if [ ! "$(echo "$TITLE" | grep $YEAR)" ]
then
TITLE="$TITLE $YEAR"
fi
REGISTERED_TITLE="$(echo "$TITLE" | sed "s/ $YEAR$/(registered) $YEAR/")"
output="$(gcalcli --calendar "$CALENDAR" search "\"$TITLE\"" --details url | grep .)"
if [ ! "$(echo "$output" | grep 'No Events Found...')" ]
then
echo "Probable duplicate:"
echo "$output"
exit 2
fi
output="$(gcalcli --calendar "$CALENDAR" search "\"$REGISTERED_TITLE\"" --details url | grep .)"
if [ ! "$(echo "$output" | grep 'No Events Found...')" ]
then
echo "Probable duplicate:"
echo "$output"
exit 2
fi
if echo "$DATE" | grep -q " " && ( echo "$DATE" | grep -q : || echo "$DATE" | grep -q [0-9][ap]\.\?m\.\?)
then
DATEWITHSTARTTIME="$DATE"
else
DATEWITHSTARTTIME="$DATE $DEFAULT_START_TIME"
fi
output="$(gcalcli --calendar "$CALENDAR" add --title "$TITLE" --when "$DATEWITHSTARTTIME" --duration "$DURATION" --description "$URL
Not registered as of $TODAY" --where "$LOCATION" --reminder "$REMINDER" --details url)"
url="$(echo "$output" | tr \ \\t \\n | grep http | head -1)"
if [ ! "$url" ]
then
echo "Can't find url. output: $output"
exit 2
fi
open $url