summary refs log tree commit diff
path: root/bin/pimsync
blob: b1a51ac47bafaa7ee243d362d28d966349bf33cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash

set -e
[ -n "$DEBUG" ] && set -x

# check requirements
for tool in vdirsyncer notify-send iconez; do
	if ! command -v "$tool" >/dev/null; then
		printf '%s: %s not installed but required\n' "$(basename "$0")" "$tool" >&2
		exit 1
	fi
done

while getopts h opt; do
	case "${opt}" in
	h | ?)
		printf "usage: %s [-h] [ACCOUNT...]\n\n" "$(basename "$0")"
		exit 2
		;;
	esac
done
shift $((OPTIND - 1))

# allow explicitely syncing accounts, otherwise all
accounts=("$*")

notifysend() {
	if [ $# -lt 3 ]; then
		return 1
	fi
	local summary=$1 msg=$2 ico=$3
	shift 3
	#shellcheck disable=2068
	notify-send $@ -a pimsync -c audible -i "$(iconez -sn "$ico")" "$summary" "$msg"
}

sync_vdir() {
	local account=$1 ico_sync=sync ico_nosync=sync-off

	case "$account" in
	*calendar*) ico_sync=calendar-sync ;;
	esac

	# exit if we're already running
	if pgrep vdirsyncer >/dev/null; then
		notifysend "Syncing stopped" "vdirsyncer is already running" "$ico_nosync"
		return
	fi

	# persistent notification
	id=$(notifysend "Syncing ${account:-contacts and calendars}" "running vdirsyncer" "$ico_sync" --print-id --expire-time=0)
	trap '{ clear-notification "$id"; }' EXIT

	#shellcheck disable=2086
	vdirsyncer sync $account
}

sync_vdir "${accounts[*]}" &
wait