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 find /o/radicale /var/log/radicale -xdev -exec chown -h 609 {} +
61 find /o/radicale -xdev -exec chgrp -h 609 {} +
62
63
64 # I moved /var/lib/radicale after it's initialization.
65 # I did a sudo -u radicale git init in the collections subfolder
66 # after it gets created, per the git docs.
67 /a/exe/lnf -T /o/radicale /var/lib/radicale
68
69 # from https://www.williamjbowman.com/blog/2015/07/24/setting-up-webdav-caldav-and-carddav-servers/
70
71 # more config is for li in distro-end
72
73 # coment in this file says this is needed for it to run on startup
74 sed -ri 's/^\s*#+\s*(ENABLE_RADICALE\s*=\s*yes\s*)/\1/' /etc/default/radicale
75
76 setini() {
77 key="$1" value="$2" section="$3"
78 file="/etc/radicale/config"
79 sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*$key[[:space:]=]/d};/ *\[$section\]/a $key = $value" "$file"
80 }
81
82 # comments say default is 0.0.0.0:5232
83 setini hosts 10.8.0.4:5232 server
84
85 if [[ $HOSTNAME == $MAIL_HOST ]]; then
86 systemctl start radicale
87 systemctl enable radicale
88 fi
89
90 # disable power management feature, set to 240 min sync interval,
91 # so it shouldn't be bad.
92
93 # davdroid from f-druid.
94 # login with url and user name
95 # url https://cal.iankelling.org/ian
96 # username ian
97 # pass, see password manager for radicale
98 #
99 # add account dialog:
100 #
101 # set account name as ian@iankelling.org, per help text below the
102 # field.
103 #
104 # switch to groups are per-contact categories,
105 # per https://davdroid.bitfire.at/configuration/radicale/
106 #
107 #
108 # After setting up account, I added one address book, named
109 # ianaddr. calender was already created, named ian. checked boxes under
110 # both. synced.
111 #
112 # To restore from old phone to new phone, I wiped all data out, then copied over the newly created files. I think
113 #
114 # ignorable background info:
115 #
116 # opentasks uses the calendar file.
117 #
118 # The address book I created got a uuid as a name for the file. Note
119 # the .props file says if it's a calendar or addressbook.
120 #
121 # When debugging, tailed /var/log/radicale/radicale.log and apache log,
122 # both show the requests happening. Without creating the address book,
123 # after creating a contact, a sync would delete it.
124 #
125 # Address books correspond to .props files in the radicale dir.
126 #
127 # Some background is here,
128 # https://davdroid.bitfire.at/faq/entry/cant-manage-groups-on-device/
129 # which shows separate vcard option is from rfc 6350, the other is 2426,
130 # radicale page says it implements the former not the latter,
131 # which conflicts with the documentation of which to select, but whatever.
132 # http://radicale.org/technical_choices/
133 # https://davdroid.bitfire.at/faq/entry/cant-manage-groups-on-device/
134 #
135 # Note, url above says only cayanogenmod 13+ and omnirom can manage groups.
136
137 # Note, radicale had built-in git support to track changes, but they
138 # removed it in 2.0.