dotfiles/.config/task/hooks/on-exit-sync.sh

57 lines
1.7 KiB
Bash
Executable file

#!/bin/sh
# Hook for Taskwarrior to automaticaly sync.
# If the state has been modified, we fork into the background
# and run task sync. If the command fails, then we spawn an
# urgent desktop notification, and use sendmail to send ourselves
# an email with an error log. For this to work, sendmail must
# be configured with capability to send emails from our account.
DATA="$HOME/.local/share/task"
n=0
while IFS= read -r _; do
n=$((n + 1))
done
if [ "$n" -gt 0 ]; then
{
lockfile="$DATA/sync_hook.lock"
errfile="$DATA/sync_hook.err"
lerrfile="$DATA/sync_hook.errl"
logfile="$DATA/sync_hook.log"
address='daniel@ficd.ca'
# shellcheck disable=SC2016
flock "$lockfile" sh -c '
task sync >>"$1" 2>"$2"
status=$?
if [ "$status" -ne 0 ]; then
echo "Taskwarrior sync failed at $(date)" >>"$2"
if command -v sendmail >/dev/null 2>&1; then
{
echo "To: $5"
echo "From: $5"
echo "Subject: Taskwarrior Sync Failed on $(hostname)"
echo "Date: $(date -R)"
echo ""
echo "task sync failed at $(date)"
echo ""
cat "$2"
} | sendmail -t
fi
cat "$2" >>"$3"
if command -v notify-send >/dev/null 2>&1; then
notify-send --urgency=critical "taskwarrior sync failed"
fi
exit 1
fi
' sh "$logfile" "$lerrfile" "$errfile" "$(hostname)" "$address"
} >/dev/null 2>&1 </dev/null &
fi
exit 0