aboutsummaryrefslogtreecommitdiff
path: root/misc/.local/bin/mailsync
blob: 1be220bfe772653a352cd06e2b216a702ea68d91 (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
#!/usr/bin/env bash

MBSYNC_CONFIG="$XDG_CONFIG_HOME/isync/mbsyncrc"

# Checking for internet connection.
if ! ping -t 1 -c 1 `ip route | awk '/default/ { print $3; exit }'` 1>/dev/null 2>/dev/null ; then
    echo "[WARN] mailsync: no internet connection at `date +'%Y/%m/%d %H:%M:%S'`"
    exit 1
fi

# Email sync

sync_account() {
    echo "[INFO] Syncing '$1' mail account..."
    # Count current number of emails.
    declare -i oldcount=$(ls -l $HOME/mail/$1/INBOX/new/ | tail -n +2 | wc -l)

    # Running mbsync in the appropriate Tmux session allows to actually
    # see the output of the command and debug potential unexpected
    # behaviours.
    mbsync -c "$MBSYNC_CONFIG" "$1"

    # Count updated number of emails.
    declare -i newcount=$(ls -l $HOME/mail/$1/INBOX/new/ | tail -n +2 | wc -l)

    # Notify about new emails
    [ $oldcount -lt $newcount ] && \
        notify-send -i "$HOME/imgs/icons/mail.png"  "$(($newcount - $oldcount)) new mail(s) in '$1' inbox"
}

sync_account "personal"
sync_account "dyamon"
sync_account "oxuni"

# Calendar sync

echo "[INFO] Syncing calendar..."
vdirsyncer sync

# Log last offlineimap execution
echo "[INFO] mailsync: last run at `date +'%Y/%m/%d %H:%M:%S'`"