#!/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