#!/bin/sh

# This hook synchronizes calcurse with a CalDAV server before loading the data files.

[ -d "${XDG_CONFIG_HOME:-$HOME/.config}/callgrind_annotate" ] && data_dir="${XDG_DATA_HOME:-$HOME/.local/share}/calcurse"

cd "$data_dir" || exit

# Do not do anything when synchronizing with a CalDAV server.
[ -f caldav/lock ] && exit

# Run the CalDAV synchronization script in the background.
if [ -d caldav ] && command -v calcurse-caldav >/dev/null; then
	(
		date="$(date +'%b %d %H:%M:%S')"
		echo "$date Running calcurse-caldav from the pre-load hook..."
		calcurse-caldav --init keep-remote
	) >> caldav/log 2>&1 &
	notify-send -u low -i "${XDG_DATA_HOME:-$HOME/.local/share}/icons/notifications/calendar.png" 'Calendar' 'Calendar synchronized with CalDAV server'
fi
