# https://github.com/pimutils/vdirsyncer
# https://vdirsyncer.pimutils.org/en/stable/tutorial.html
# https://hund.tty1.se/2020/08/12/how-to-sync-and-manage-your-caldav-and-carddav-via-the-terminal.html
# https://www.dj-bauer.de/once-forever-mutt-configuration-en.html
#
VDIRSYNCER_CONFIG=${HOME}/.config/vdirsyncer/config

NEXTCLOUD_URL="http://192.168.1.102"
PORT="8080"
NEXTCLOUD_USER="nextcloud"
NEXTCLOUD_PASSWORD="nextcloud" # password for nextcloud user

apt-get install -y vdirsyncer

cat << EOF | sudo tee "${VDIRSYNCER_CONFIG}"
[general]
# A cache path where the current status of vdirsyncer will be stored
status_path = "${TARGET}/.config/vdirsyncer/status/"

# Create a new sync connection between two storages we will define
[pair nextcloud_contacts]
a = "nextcloud_contacts_local"
b = "nextcloud_contacts_remote"
collections = ["from a", "from b"]
metadata = ["displayname"]

# A local storage to store the contacts
[storage nextcloud_contacts_local]
type = "filesystem"

# Path to where to store the vcards
path = "${TARGET}/.contacts/"
fileext = ".vcf"

# A remote storage to nextcloud
[storage nextcloud_contacts_remote]
type = "carddav"

# Can be obtained from nextcloud
url = "${NEXTCLOUD_URL}:${PORT}/remote.php/dav/addressbooks/users/${USERNAME}/"
username = "${NEXTCLOUD_USER}"

# Shell command which calls the external command pass and reads the password nextcloud
password.fetch = ["command", "pass", "nextcloud"]

# SSL certificate fingerprint
verify_fingerprint = "FINGERPRINT"

# Verify ssl certificate. Set to false if it is self signed and not installed on local machine
verify = true
EOF

# echo -n | openssl s_client -connect redhat.com:443 | openssl x509 -noout -fingerprint
echo -n | openssl s_client -connect "${NEXTCLOUD_URL}":8443 | openssl x509 -noout -fingerprint
