#!/bin/sh
#
# 1. Make a commit if the calcurse directories contain a Git repository.
# 2. Synchronize with a CalDAV server if calcurse-caldav is configured.

data_dir="$HOME/.calcurse"
config_dir="$HOME/.calcurse"

if [ ! -d "$data_dir" ]; then
	data_dir="${XDG_DATA_HOME:-$HOME/.local/share}/calcurse"
	config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/calcurse"
fi

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

# Run the CalDAV synchronization script in the background.
cd "$data_dir" || exit
if [ -d caldav ] && command -v calcurse-caldav >/dev/null; then
	(
		date="$(date +'%b %d %H:%M:%S')"
		echo "$date Running calcurse-caldav from the post-save hook..."
		calcurse-caldav
		echo
	) >>caldav/log 2>&1 &
fi
