-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-dav-sync.sh
executable file
·418 lines (349 loc) · 12.7 KB
/
setup-dav-sync.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env bash
function generate_padding {
# get the first character ':0:1' - aka index zero to one
local symbol="${1:0:1}"
shift # drop input parameter '1' and shift all parameters to the left
local -i offset=$1
shift
declare -g padding=''
# iterate over the remaining input parameters
local param
for param in "$@"; do
# iterate over string length
local i
for (( i = 0; i < ${#param}; i++ )); do
padding="$padding$symbol"
done
done
# include padding for the gaps between words
local i
for (( i = 0; i < $# - 1; i++ )); do
padding="$padding$symbol"
done
local i
for (( i = 0; i < $offset; i++ )); do
padding="$padding$symbol"
done
}
function cron_handler {
local action="$1" # either 'add' or 'delete'
local server_config_name="$2"
local cron_entry="$3"
local cron_tab="/var/spool/cron/crontabs/$USER"
local regex_search="^.*$server_config_name.*$"
sudo mount / -o remount,rw
case "$action" in
'add')
if sudo [ -f "$cron_tab" ]; then
local grep_result=$(sudo grep --only-matching "$regex_search" "$cron_tab")
# if a matching cron entry exists replace it
if [ ! -z "$grep_result" ]; then
echo "cron - updating entry for '$server_config_name'"
sudo sed --in-place --expression="s|$regex_search|$cron_entry|" "$cron_tab"
else
echo "cron - creating new entry for '$server_config_name'"
echo "$cron_entry" | sudo tee --append "$cron_tab" &>/dev/null
fi
else
echo "User's cron tab not found, creating it."
sudo touch "$cron_tab"
sudo chown $USER:clickpkg "$cron_tab" # non portable operation
sudo chmod 600 "$cron_tab"
echo "$cron_entry" | sudo tee --append "$cron_tab" &>/dev/null
fi
;;
'delete')
if sudo [ -f "$cron_tab" ]; then
local grep_result=$(sudo grep --only-matching "$regex_search" "$cron_tab")
# if a matching cron entry exists delete it
if [ ! -z "$grep_result" ]; then
echo "cron - deleting entry for '$server_config_name'"
sudo sed --in-place --expression="/$regex_search/d" "$cron_tab"
else
echo "cron - no entry found for '$server_config_name'"
fi
else
echo "User's cron tab not found, no action taken on cron tab."
fi
;;
*)
sudo mount / -o remount,ro
echo 'Internal error!' >&2
echo "in the '${FUNCNAME[0]}' function" >&2
exit 1
;;
esac
sudo service cron restart &>/dev/null
sudo mount / -o remount,ro
}
function manual_sync {
local action="$1" # either 'add' or 'delete'
local name="$2"
local sync="$3" # only needed for the 'add' action
local script_name="$HOME/bin/manual-sync-$name.sh"
case "$action" in
'add')
echo "$sync" > $script_name
chown $USER:$USER $script_name
chmod 775 $script_name
local symbol='#'
local header='manual sync script created at:'
generate_padding "$symbol" 4 "$header" "$script_name"
echo "$padding"
echo "$symbol $header $script_name $symbol"
echo "$padding"
;;
'delete')
if [ -f "$script_name" ]; then
rm "$script_name"
fi
local symbol='#'
local header="manual sync script '$script_name' has been deleted"
generate_padding "$symbol" 4 "$header"
echo "$padding"
echo "$symbol $header $symbol"
echo "$padding"
;;
*)
sudo mount / -o remount,ro
echo 'Internal error!' >&2
echo "in the '${FUNCNAME[0]}' function" >&2
exit 1
;;
esac
}
function setup_sync {
local server_config_name="$1"
local name="$2"
local sync="export DISPLAY=:0.0 && export DBUS_SESSION_BUS_ADDRESS=\$(ps -u $USER e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35) && /usr/bin/syncevolution $server_config_name"
local cron_entry="$CRON_FREQUENCY $sync"
local action='add'
cron_handler "$action" "$server_config_name" "$cron_entry"
if [ ! -d $HOME/bin ]; then
mkdir $HOME/bin
fi
manual_sync "$action" "$name" "$sync"
}
function delete {
local type_action="$1"
local server_config_name="$2"
local name="$3"
local visual_name="$4"
local action='delete'
cron_handler "$action" "$server_config_name"
manual_sync "$action" "$name"
case "$type_action" in
'contact')
backend="evolution-contacts"
;;
'calendar')
backend="evolution-calendar"
;;
esac
syncevolution --remove-database backend=$backend \
database="${visual_name:0:30}" &>/dev/null
syncevolution --remove "target-config@${server_config_name:0:30}"
syncevolution --remove "@${server_config_name:0:30}"
syncevolution --remove "${server_config_name:0:30}"
# I cannot find the `syncevolution` command to remove this configuration
rm --recursive --force \
$HOME/.config/syncevolution/default/sources/${name:0:30}
}
function delete-contacts {
local i
for (( i = 0; i < ${#CONTACTS_NAMES[@]}; i++ )); do
delete contact "${CONTACTS_SERVER_CONFIG_NAMES[$i]:0:30}" \
"${CONTACTS_NAMES[$i]:0:30}" "${CONTACTS_VISUAL_NAMES[$i]:0:30}"
done
}
function delete-calendar {
local i
for (( i = 0; i < ${#CALENDAR_NAMES[@]}; i++ )); do
delete calendar "${CALENDAR_SERVER_CONFIG_NAMES[$i]:0:30}" \
"${CALENDAR_NAMES[$i]:0:30}" "${CALENDAR_VISUAL_NAMES[$i]:0:30}"
done
}
function contacts {
local i
for (( i = 0; i < ${#CONTACTS_NAMES[@]}; i++ )); do
# syncevolution cannot handle names larger than 31 characters
local contacts_server_config_names="${CONTACTS_SERVER_CONFIG_NAMES[$i]:0:30}"
local contacts_names="${CONTACTS_NAMES[$i]:0:30}"
local contacts_visual_names="${CONTACTS_VISUAL_NAMES[$i]:0:30}"
local url="${CARD_URL%%/}/${CARD_NAMES[$i]}"
# add cron entry and create manual sync script
setup_sync "$contacts_server_config_names" \
"$contacts_names"
#Create contact list
syncevolution --create-database backend=evolution-contacts \
database="$contacts_visual_names"
#Create Peer
#if (( ${#USERNAME} != 0 )) || (( ${#PASSWORD} != 0 )); then
if (( ${#PASSWORD} != 0 )); then
syncevolution --configure --template webdav username="$USERNAME" \
password="$PASSWORD" syncURL="$url" \
keyring=no "target-config@$contacts_server_config_names"
else
syncevolution --configure --template webdav syncURL="$url" \
keyring=no "target-config@$contacts_server_config_names"
fi
#Create New Source
syncevolution --configure backend=evolution-contacts \
database="$contacts_visual_names" @default "$contacts_names"
#Add remote database
syncevolution --configure database="$url" \
backend=carddav "target-config@$contacts_server_config_names" \
"$contacts_names"
#Connect remote contact list with local databases
syncevolution --configure --template SyncEvolution_Client \
Sync=None syncURL="local://@$contacts_server_config_names" \
"$contacts_server_config_names" "$contacts_names"
#Add local database to the source
syncevolution --configure sync=two-way backend=evolution-contacts \
database="$contacts_visual_names" "$contacts_server_config_names" \
"$contacts_names"
#Start first sync
syncevolution --sync refresh-from-remote \
"$contacts_server_config_names" "$contacts_names"
done
}
function calendar {
local i
for i in ${!CALENDAR_NAMES[@]}; do
# syncevolution cannot handle names larger than 31 characters
local calendar_server_config_names="${CALENDAR_SERVER_CONFIG_NAMES[$i]:0:30}"
local calendar_names="${CALENDAR_NAMES[$i]:0:30}"
local calendar_visual_names="${CALENDAR_VISUAL_NAMES[$i]:0:30}"
local url="${CAL_URL%%/}/${CAL_NAMES[$i]}"
# add cron entry and create manual sync script
setup_sync "$calendar_server_config_names" "$calendar_names"
#Create Calendar
syncevolution --create-database backend=evolution-calendar \
database="$calendar_visual_names"
#Create Peer
#if (( ${#USERNAME} != 0 )) || (( ${#PASSWORD} != 0 )); then
if (( ${#PASSWORD} != 0 )); then
syncevolution --configure --template webdav username="$USERNAME" \
password="$PASSWORD" syncURL="$url" keyring=no \
"target-config@$calendar_server_config_names"
else
syncevolution --configure --template webdav syncURL="$url" \
keyring=no "target-config@$calendar_server_config_names"
fi
#Create New Source
syncevolution --configure backend=evolution-calendar \
database="$calendar_visual_names" @default "$calendar_names"
#Add remote database
syncevolution --configure database="$url" backend=caldav \
"target-config@$calendar_server_config_names" "$calendar_names"
#Connect remote calendars with local databases
syncevolution --configure --template SyncEvolution_Client \
syncURL="local://@$calendar_server_config_names" \
"$calendar_server_config_names" "$calendar_names"
#Add local database to the source
syncevolution --configure sync=two-way backend=evolution-calendar \
database="$calendar_visual_names" "$calendar_server_config_names" \
"$calendar_names"
#Start first sync
syncevolution --sync refresh-from-remote \
"$calendar_server_config_names" "$calendar_names"
done
}
function help {
echo 'Usage:'
echo " $0 --contacts --calendar config1.txt [config2.txt ...]"
echo " $0 --contacts config1.txt [config2.txt ...]"
echo " $0 --calendar config1.txt [config2.txt ...]"
echo " $0 --delete-contacts --delete-calendar config1.txt [config2.txt ...]"
echo " $0 --delete-contacts config1.txt [config2.txt ...]"
echo " $0 --delete-calendar config1.txt [config2.txt ...]"
echo " $0 -h | --help"
exit 1
}
if [ $# -eq 0 ]; then
echo 'No arguments given'
help
fi
TEMP=$(getopt --options 'h' --long 'calendar,contacts,delete-calendar,\
delete-contacts,help' --name "$0" -- "$@")
if [ $? -ne 0 ]; then
echo 'Terminating...' >&2
exit 1
fi
eval set -- "$TEMP"
unset TEMP
contacts=false
calendar=false
delete_calendar=false
delete_contacts=false
while true; do
case "$1" in
'-h'|'--help')
help
;;
'--contacts')
contacts=true
shift
continue
;;
'--calendar')
calendar=true
shift
continue
;;
'--delete-contacts')
delete_contacts=true
shift
continue
;;
'--delete-calendar')
delete_calendar=true
shift
continue
;;
'--')
# end of flags
shift
break
;;
*)
echo 'Internal error!' >&2
echo 'in the flag decoding logic' >&2
exit 1
;;
esac
done
if $contacts && $delete_contacts; then
echo 'contradictory flags set: --contacts and --delete_contacts'
exit 1
fi
if $calendar && $delete_calendar; then
echo 'contradictory flags set: --calendar and --delete_calendar'
exit 1
fi
for config_file in "$@"; do
if [ -f "$config_file" ]; then
symbol='#'
header="using configuration file:"
generate_padding "$symbol" 4 "$header" "$config_file"
echo "$padding"
echo "$symbol $header $config_file $symbol"
echo "$padding"
source "$config_file"
else
echo "unable to read configuration file: $config_file, skipping"
continue
fi
if $calendar; then
calendar
fi
if $contacts; then
contacts
fi
if $delete_calendar; then
delete-calendar
fi
if $delete_contacts; then
delete-contacts
fi
done