various fixes
[distro-setup] / radicale-setup
1 #!/bin/bash
2
3 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
4
5 if [[ -e /b/errhandle/err ]]; then
6 source /b/errhandle/err
7 else
8 set -eE -o pipefail
9 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
10 fi
11
12 source /a/bin/bash_unpublished/source-state
13 source /a/bin/distro-functions/src/package-manager-abstractions
14 # davdroid setup instructions at the bottom
15
16 # main docs:
17 # http://radicale.org/user_documentation/
18 # https://davdroid.bitfire.at/configuration/
19
20 # note on debugging: if radicale can't bind to the address,
21 # in the log it just says "Starting Radicale". If you run
22 # it in the foreground, it will give more info. Background
23 # plus debug does not help.
24 # sudo -u radicale radicale -D -f
25
26 # created password file with:
27 # htpasswd -c /etc/davpass dav
28
29 vpn_ser=openvpn-client
30 if [[ ! -e /lib/systemd/system/openvpn-client@.service ]]; then
31 vpn_ser=openvpn
32 fi
33
34 d=/etc/systemd/system/radicale.service.d
35 mkdir -p $d
36 cat >$d/override.conf <<EOF
37 [Unit]
38 # this unit is configured to start and stop whenever openvpn-client@mail.service
39 # does
40 After=network.target
41 BindsTo=$vpn_ser@mail.service
42 After=$vpn_ser@mail.service
43
44 [Install]
45 RequiredBy=$vpn_ser@mail.service
46 EOF
47 systemctl daemon-reload # not sure this is needed
48
49 pi-nostart radicale
50
51 # use persistent uid/gid
52 IFS=:; read -r _ _ uid _ < <(getent passwd radicale ); unset IFS
53 IFS=:; read -r _ _ gid _ < <(getent group radicale ); unset IFS
54 if [[ $uid != 609 ]]; then
55 systemctl stop radicale ||:
56 usermod -u 609 radicale
57 groupmod -g 609 radicale
58 usermod -g 609 radicale
59 fi
60 log_p=/var/log/radicale
61 [[ -d $log_p ]] || log_p=
62 find /o/radicale -xdev -exec chown -h 609 {} +
63 find /o/radicale -xdev -exec chgrp -h 609 {} +
64
65
66 # I moved /var/lib/radicale after it's initialization.
67 # I did a sudo -u radicale git init in the collections subfolder
68 # after it gets created, per the git docs.
69 /a/exe/lnf -T /o/radicale /var/lib/radicale
70
71 # from https://www.williamjbowman.com/blog/2015/07/24/setting-up-webdav-caldav-and-carddav-servers/
72
73 # more config is for li in distro-end
74
75 # coment in this file says this is needed for it to run on startup
76 sed -ri 's/^\s*#+\s*(ENABLE_RADICALE\s*=\s*yes\s*)/\1/' /etc/default/radicale
77
78 setini() {
79 key="$1" value="$2" section="$3"
80 file="/etc/radicale/config"
81 sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d};/ *\[$section\]/a $key = $value" "$file"
82 }
83
84 # comments say default is 0.0.0.0:5232
85 setini hosts 10.8.0.4:5232 server
86
87 if [[ $HOSTNAME == $MAIL_HOST ]]; then
88 systemctl start radicale
89 systemctl enable radicale
90 fi
91
92 # disable power management feature, set to 240 min sync interval,
93 # so it shouldn't be bad.
94
95 # davdroid from f-druid.
96 # login with url and user name
97 # url https://cal.iankelling.org/ian
98 # username ian
99 # pass, see password manager for radicale
100 #
101 # add account dialog:
102 #
103 # set account name as ian@iankelling.org, per help text below the
104 # field.
105 #
106 # switch to groups are per-contact categories,
107 # per https://davdroid.bitfire.at/configuration/radicale/
108 #
109 #
110 # After setting up account, I added one address book, named
111 # ianaddr. calender was already created, named ian. checked boxes under
112 # both. synced.
113 #
114 # To restore from old phone to new phone, I wiped all data out, then copied over the newly created files. I think
115 #
116 # ignorable background info:
117 #
118 # opentasks uses the calendar file.
119 #
120 # The address book I created got a uuid as a name for the file. Note
121 # the .props file says if it's a calendar or addressbook.
122 #
123 # When debugging, tailed /var/log/radicale/radicale.log and apache log,
124 # both show the requests happening. Without creating the address book,
125 # after creating a contact, a sync would delete it.
126 #
127 # Address books correspond to .props files in the radicale dir.
128 #
129 # Some background is here,
130 # https://davdroid.bitfire.at/faq/entry/cant-manage-groups-on-device/
131 # which shows separate vcard option is from rfc 6350, the other is 2426,
132 # radicale page says it implements the former not the latter,
133 # which conflicts with the documentation of which to select, but whatever.
134 # http://radicale.org/technical_choices/
135 # https://davdroid.bitfire.at/faq/entry/cant-manage-groups-on-device/
136 #
137 # Note, url above says only cayanogenmod 13+ and omnirom can manage groups.
138
139 # Note, radicale had built-in git support to track changes, but they
140 # removed it in 2.0.