diff --git a/mailsync b/mailsync index ccaa2f4..1d1f411 100755 --- a/mailsync +++ b/mailsync @@ -1,20 +1,29 @@ #!/bin/sh -# Temp disable -#exit 0 +maildir=~/mail +# find "$maildir" -mindepth 1 -maxdepth 1 -type d ! -name cur ! -name tmp ! -name new ! -name .notmuch -printf '%f\0' | xargs -0 -n1 --max-procs=0 mbsync +mailboxes=$(find "$maildir" -mindepth 1 -maxdepth 1 -type d ! -name cur ! -name tmp ! -name new ! -name .notmuch -printf '%f\n') -if pgrep -x mbsync > /dev/null ; then - >&2 echo "Isync is already running, aborting..." +LOCKFILE="/tmp/.mailsync.$USER.lock" +# /!\ locking method is susceptible to race condition +# Should be fine for cron job +if [ -e "$LOCKFILE" ] && kill -0 "$(cat "$LOCKFILE")"; then + echo "mailsync is already running" exit 1 fi -timeout 10m mbsync -a -if [ "$?" -eq 124 ];then - >&2 echo "Command mbync -a timed out" -elif [ "$?" -eq 0 ];then - >&2 echo "Successful mail synchronisation" -else - >&2 echo "Isync exited with status $?" -fi +# make sure the lockfile is removed when we exit and then claim it +trap "rm -f $LOCKFILE; exit" INT TERM EXIT +echo $$ > "$LOCKFILE" + +for m in $mailboxes; do + mbsync "$m" & + sleep 1 +done + +wait + fdm fetch notmuch new + +rm -f "$LOCKFILE"