rc/bin/mailsync

38 lines
962 B
Text
Raw Permalink Normal View History

2019-11-20 21:59:13 +00:00
#!/bin/sh
die () {
2>&1 echo "$*"
exit 1
}
2019-11-20 21:59:13 +00:00
maildir=~/mail
mailboxes=$(find "$maildir" -mindepth 1 -maxdepth 1 -type d ! -name cur ! -name tmp ! -name new ! -name .notmuch -printf '%f\n')
LOCKFILE="/tmp/.mailsync.$USER.lock"
# https://stackoverflow.com/questions/185451/quick-and-dirty-way-to-ensure-only-one-instance-of-a-shell-script-is-running-at
# /!\ 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
# make sure the lockfile is removed when we exit and then claim it
2020-11-11 03:05:37 +00:00
trap "rm -f '$LOCKFILE'; exit" INT TERM EXIT
2019-11-20 21:59:13 +00:00
echo $$ > "$LOCKFILE"
# Try a single short sync to test password availability
mbsync "$(echo "$mailboxes" | head -n 1)" || die "Please unlock password database"
for m in $(echo "$mailboxes" | tail -n+2); do
2019-11-20 21:59:13 +00:00
mbsync "$m" &
sleep 1
done
wait
fdm fetch
notmuch new
rm -f "$LOCKFILE"